(1) Use functions. Put all that stuff that fills
Smith[] with random numbers in a function. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void fill_array_with_random_numbers_in_range( int* a, int n, int min, int max )
{
int k = 0;
while (k < n)
{
int x = rand();
if (x >= min && x <= max)
{
a[k] = x;
k++;
}
}
}
|
This makes life easy in
main():
1 2 3 4 5 6 7
|
int main()
{
int Smith[10];
srand( time( 0 ) ); // (You forgot to randomize your PRNG like this)
fill_array_with_random_numbers_in_range( Smith, 10, 10, 99 );
|
Now you need functions to return the sum of numbers in your array. From that you can calculate the average. And a simple function (or two) to find the largest and smallest.
8 9 10
|
cout << "\t\t\tSum of Numbers : " << sum_of_array( Smith, 10 ) << "\n";
cout << "\t\t\tAverage of Numbers : " << average_of_array( Smith, 10 ) << "\n";
//etc
|
Help with the function prototypes:
1 2 3
|
int sum_of_array( int* a, int n );
double average_of_array( int* a, int n );
//etc
|
Now for your question. How do you find the sum of an array. Suppose I give you:
2,3,5,7,11
What is the sum? Easy: 0+2+3+5+7+11. Use a loop.
To compute the average, you need to know the sum (which you now have a function to compute) and the number of elements in the array, right?
To find the largest number in an array, what do you do?
3, -7, 15, 12, 45, -97, 18
What do you do, in your own head, to find the largest? Make the computer do the same thing.
Hope this helps.