vendredi 24 novembre 2017

Find largest and smallest numbers in an array

I'm working on C++ program to store random values in an array of size 1000. Then program will ask user to enter 'X' largest and 'Y' smallest numbers. 'X' and 'Y' are any integer values less than size of array. Program will then output 'X' Largest and 'Y' smallest numbers from array.

Suppose array have 1 2 4 7 9 14 3

X=2

Y=3

Output will be

Largest 2 numbers in array are 14 9

Smallest 3 number in array are 1 2 3

#include<iostream>
using namespace std;
int main()
{
   int size[1000];
   int x, y, max;
    max = 0;
    cout << "How many Largest numbers do you need ?\n";
    cin >> x;
    cout << "How many Smallest numbers do you need ?\n";
    cin >> y;
        for (int i = 0;i <= 999;i++)
        {
            size[i] = rand() % 100;
            cout << "No." << i << " = " << size[i] << endl;
        }
        for (int j = 1;j <= x;j++)
        {

            for (int i = 0;i <= 999;i++)
            {
                if (size[i] > max)
                    max = size[i];
            }
            cout << "LARGEST NUMBERS\n" << max << endl;
        }
}




Aucun commentaire:

Enregistrer un commentaire