/* testarr09.cpp CIS 150 06/19/2008 David Klick This program shows how some of the common array tasks can be performed using functions. */ #include #include #include #include using std::cin; using std::cout; using std::setw; void cont(); void initialize(int a[], int length); void display(int a[], int length); int sum(int a[], int length); double average(int a[], int length); int min(int a[], int length); int max(int a[], int length); int main() { const int ARRAY_SIZE = 10; int nums[ARRAY_SIZE]; // seed the random number generator srand(time(0)); // initialize the array to random numbers initialize(nums, ARRAY_SIZE); // display the array on one line display(nums, ARRAY_SIZE); // add up all the array elements cout << "The sum is " << sum(nums, ARRAY_SIZE) << '\n'; // calculate and display the average cout << "The average is " << average(nums, ARRAY_SIZE) << '\n'; // find the minimum value cout << "The smallest value is " << min(nums, ARRAY_SIZE) << '\n'; // find the maximum value cout << "The largest value is " << max(nums, ARRAY_SIZE) << '\n'; cont(); return 0; } /* void initialize(int a[], int length); Arguments: int a[]: an array of integers int length: the number of elements in the a[] array Returns: nothing This function initializes all of the elements of the a[] array to random integer values between 1 and 100 inclusive. */ void initialize(int a[], int length) { for (int i=0; i maxval) maxval = a[i]; } return maxval; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: The array contains: 49 52 14 37 1 99 49 70 66 4 The sum is 441 The average is 44.1 The smallest value is 1 The largest value is 99 */