Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Andrew Gonzalez
- * Lab 4
- * This program computes the average of data sets and to total average for all the inputs.*/
- #include <iostream>
- using namespace std;
- int main()
- {
- /*Variable declarations*/
- int flag = 1;
- int size, repititions;
- double highestAvg = 0;
- double lowestAvg = 0;
- double number, runningTotal, average, finalAverage;
- /*while true loop keeps running thorugh the process as long as there are more averages to compute*/
- while(true){
- /*do while loop to take in user input for size of each data set*/
- do{
- cout << "Please enter the size of the data set (1-10 elements or 0 to quit): ";
- cin >> size;
- if(size < 0 || size > 10)
- cout << endl;
- }while(size < 0 || size > 10);
- /*If statement to break out of the while true when the condition is met*/
- if(size == 0) break;
- /*cin in the data for the current set*/
- cout << "Please enter your data: ";
- for(int i = 0; i < size; i++){
- cin >> number;
- runningTotal += number;
- }
- /*compute the average for current data set*/
- average = runningTotal / size;
- /*if the average is a higher or lower than the current max or min average, replace with the new extreme
- * if it is the first time iterating through the program, the flag will place the current value into the the low and high*/
- if(flag == 1) {
- lowestAvg = average;
- highestAvg = average;
- flag = 2;
- }
- if(average < lowestAvg)
- lowestAvg = average;
- if(average > highestAvg)
- highestAvg = average;
- /*adds the average into the total in preparation for the final
- * also adds one to the number of times the while loop has occurred*/
- finalAverage += average;
- repititions++;
- /*Setting precision to 3 decimal places*/
- cout.setf(ios::fixed);
- cout.precision(3);
- /*output the average for that set of current data.
- * reset the average and the runningTotal variables for the next iteration*/
- cout << "\naverage = " << average << "\n\n";
- average = 0;
- runningTotal = 0;
- }
- /*compute the final average without the highest and lowest averages*/
- finalAverage = finalAverage - highestAvg - lowestAvg;
- repititions = repititions - 2;
- finalAverage = finalAverage / repititions;
- /*output the final average*/
- cout << "\nfinal average = " << finalAverage;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement