void printArr( int a[], int cnt );
int sumArr( int a[], int cnt );
double aveArr( int a[], int cnt );
int minVal( int a[], int cnt );
int maxVal( int a[], int cnt );
int main ()
{
int arr[10];
int count;
for ( count=0 ; count<5 ; count++ )
arr[count] = count*2;
printArr( arr, count );
cout << " sum of all initialized elements in array is: " << sumArr( arr, count ) << endl;
cout << " ave of all initialized elements in array is: " << aveArr( arr, count ) << endl;
cout << " the smallest number in the array is: " << minVal( arr, count ) << endl;
cout << " the largest number in the array is: " << minVal( arr, count ) << endl;
return 0;
}
// print all the initilized values in the array from front to back
void printArr( int a[], int cnt )
{
// your code here
}
// return the sum of the initilized values in the array
int sumArr( int a[], int cnt )
{
// your code here
return 0; // just to make it compile - you change as needed
}
// return the ave of the initilized values in the array
double aveArr( int a[], int cnt )
{
// your code here
return 0; // just to make it compile - you change as needed
}
// return the smallest number in the array
int minVal( int a[], int cnt )
{
// your code here
return 0; // just to make it compile - you change as needed
}
// return the largest number in the array
int maxVal( int a[], int cnt )
{
// your code here
return 0; // just to make it compile - you change as needed
}
Everywhere it says //your code here you're going to have to use some sort of loop to iterate over the array and do... something. What you do is going to depend on which function you're writing. Let's take sumArr for example...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//...main and stuff up here
// return the sum of the initialized values in the array
int sumArr( int a[], int cnt )
{
int sum = 0; //we need to have a variable to hold a running total of the sum, initially zero.
for(int i = 0; //a new variable that will keep track of how many times we've run the loop
i < cnt; //only allow us to execute cnt times
i++) //after each iteration of the loop, add one to this loop counter
{
sum += a[i]; //a[i] is saying the 'i'th element in the array
//using the loop counter is a way to inspect each item one at a time
//so for each item in the array... add it to the running total (sum)
}
return sum; //not returning zero anymore! we're returning the sum we just calculated
}
//...other functions down here
Each of the other functions behaves a little differently. The important takeaway is that they all use this loop pattern to access elements in the array one at a time. What you do with each element will depend on the function you're writing.