///////////////////////////////////////////////////////////////////////////////////// // // Program: Working with an Array, and Doing Calculations with the Elements // // Author: William F. Slater, III // // Description: When run, it requests input of from two to ten // integers from the keyboard, loading each into an array // and then performs calculations on the array elements and prints // (1) the sum, // (2) the average, // (3) the ungrouped median // (4) the product // (5) the smallest value // (6) the largest value // // // Date: August 20, 2003 // // File Name: CSS561Week5-William_Slater.cpp // ///////////////////////////////////////////////////////////////////////////////////// #include #include void main() { // Declare variables -- These are listed alphabetically char answer; // char variable to ask user if they want to repeat a run double array0[10]; // array for storing first array, before sort double array1[10]; // array for numbers double average; // variable for average int ArrayMax; // variable for maximum array size int index1; // variable for array index double largest; // variable for the largest array element value double median; // variable for the median value int next; // variable to reference the next element in the array double numswitch; // variable used to switch numbers double product; // variable used to store all elements multiplied together double smallest; // variable for the smallest array element value double sorted[10]; // array for sorted numbers double sum; // variable used to store all elements added together int top; // variable for top array element answer = 'y'; // Remain in loop as long as user wants to run the program while (answer != 'n') { // Section for Printing Heading cout << endl; cout << endl; cout << "===================================================================" << endl; cout << " **** Welcome to the Array Program ****" << endl; cout << endl; cout << endl; cout << "You will be asked to enter between two and ten numbers." << endl; cout << endl; cout << "This program will perform sort and arithmetic operations, then print:" << endl; cout << endl; cout << "The array you just loaded. " << endl; cout << endl; cout << "And the array before and after the sort - for comparison. " << endl; cout << endl; cout << "The Sum of these numbers." << endl; cout << endl; cout << "The Average of these numbers." << endl; cout << endl; cout << "The Ungrouped Median Value of these numbers." << endl; cout << endl; cout << "The Product of these numbers." << endl; cout << endl; cout << "The Smallest Value of these numbers." << endl; cout << endl; cout << "The Largest Value of these numbers." << endl; cout << endl; cout << "Note 1. You must enter numbers for this to work properly." << endl; cout << "===================================================================" << endl; cout << endl; cout << endl; // Re-initialize variables ArrayMax = 0; index1 = 0; // Remain in loop asking for array elements until all elements are input // request number of elements, set this to ArrayMax cout << endl; cout << "Please enter the number of elements you want for this Array." << endl; cout << endl; cout << "This will be a number between 2 and 10: " ; cin >> ArrayMax; if (ArrayMax > 10 || ArrayMax == 1) { cout << "Error - Array size must be at least 2 and less than or equal to 10. Please try again." << endl; continue; } cout << endl; cout << endl; cout << endl; // User Input Section for Array -- Request Array Elements and Load array1 while (index1 < ArrayMax) { cout << "Enter a value for array element number " << index1 << ": "; cin >> array1[index1]; index1 = index1++; } cout << endl; cout << endl; cout << endl; // Echo back the numbers to show input was received correctly. Also, fill another array. // Re-initialize index1 first. index1 = 0; while (index1 < ArrayMax) { cout << "Array element number " << index1 << " = " << array1[index1] << endl; array0[index1] = array1[index1]; index1 = index1++; } // Do the calculations. Re-initialize variables first. index1 = 0; sum = 0; product = 1; average = 0; // Calculate the sum and product here while (index1 < ArrayMax) { sum = array1[index1] + sum; product = array1[index1] * product; index1 = index1++; } // Sort array so we can find the largest and smallest, and calculate the median. Re-initialize variables first. // After the sort, the first array element will be the smallest value; // and the highest array element will be the largest value. index1 = 0; largest = 0; next = 0; numswitch = 0; smallest = 0; for (index1 = 0; index1 < ArrayMax; index1++) { for (next = index1; next < ArrayMax; next++) { if (array1[index1] > array1[next]) { numswitch = array1[index1]; array1[index1] = array1[next]; array1[next] = numswitch; } } // end of inside For Loop } // end of outside For Loop // set the largest and smallest values smallest = array1[0]; top = ArrayMax - 1; largest = array1[top]; cout << endl; cout << endl; cout << endl; // verification that the Sort Worked. index1 = 0; // reset index1 while ( index1 < ArrayMax) { cout << "The old & new values of Array element no. " << index1 << " old = " << array0[index1] << " new = " <> answer; } // End of big while loop cout << endl; cout << endl; cout << endl; cout << "*** Program is terminating *** " << endl; cout << endl; cout << " **** Thank you for your time and Goodbye! **** " << endl; } // End of main()