1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX = 20;
void constructArray (int [], int);
void printArray (const int [], int);
void findMaxMin (const int [], int, int&, int&);
int main ()
{
int a [MAX];
int largest, smallest;
int size;
srand (time (NULL));
// To construct some arrays of various sizes
for (int i = 1; i <= 5; i++)
{
size = rand () % 11 + 10;
constructArray (a, size);
printArray (a, size);
findMaxMin (a, size, largest, smallest);
cout << "Largest no = " << largest << endl;
cout << "Smallest no = " << smallest << endl;
cout << "---------------------------" << endl;
}
}
void constructArray (int a [], int size)
{
for (int i = 0; i < size; i++)
a [i] = rand ();
}
void printArray (const int a [], int size)
{
for (int i = 0; i < size; i++)
cout << a [i] << "\t";
cout << endl;
}
void findMaxMin (const int a [], int size,
int& largest, int& smallest)
{
largest = a [0];
smallest = a [0];
for (int i = 1; i < size; i++)
{
if (largest < a [i])
largest = a [i];
if (smallest > a [i])
smallest = a [i];
}
}
|