hightemp - cout<<"Enter the high temperature for...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// hightemp.cpp // This program averages the high temperatures over a user-defined // number of days. / #include<iostream.h> #include<iomanip.h> #include"apvector.h" # int main() { apvector <int> daily_temp(31,0); // array of daily high temperatures int num_values; // number of days in a row to enter values int index; // index for loop counter and array access double average_high; // calculated average high temperature int total = 0; // used to total temps before averaging do // loop to ask for number of days until valid input is received { cout << "Enter the number of days for which you have data: "; cin >> num_values; if ((num_values < 1) || (num_values > 31)) { cout << "The number of days must be in the range 1 to 31" << endl; } } while ((num_values < 1) || (num_values > 31)); // The following loop gets the high temperatures from the user for as // many days as the user specified in num_values. for(index = 0; index <= (num_values - 1); index++)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { cout << "Enter the high temperature for day " << index + 1 << ": "; cin >> daily_temp[index]; // input value into array } // Print the values in the array to the screen. cout << "The array contains high temperatures for " << num_values << " days.\n"; cout << "The values are as follows.\n"; for(index = 0; index <= (num_values - 1); index++) { cout << "Day " << index + 1 << ": " << daily_temp[index] << endl; total = total + daily_temp[index]; // update total for averaging } // Calculate average by typecasting total and num_values to doubles // before dividing and assigning the result to average_high. average_high = double(total) / double(num_values); // Print the results to the screen. cout.setf(ios::fixed); cout << "The average high temperature during the " << num_values << "-day period was " << setprecision(2) << average_high << " degrees.\n"; return 0; }...
View Full Document

This note was uploaded on 06/20/2011 for the course COMPUTING 379 taught by Professor Dr.anoopsarkar during the Spring '11 term at American College of Computer & Information Sciences.

Ask a homework question - tutors are online