hightemp - { cout <<...

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 &lt;&lt; &quot;Enter the high temperature for day &quot; &lt;&lt; index + 1 &lt;&lt; &quot;: &quot;; cin &gt;&gt; daily_temp[index]; // input value into array } // Print the values in the array to the screen. cout &lt;&lt; &quot;The array contains high temperatures for &quot; &lt;&lt; num_values &lt;&lt; &quot; days.\n&quot;; cout &lt;&lt; &quot;The values are as follows.\n&quot;; for(index = 0; index &lt;= (num_values - 1); index++) { cout &lt;&lt; &quot;Day &quot; &lt;&lt; index + 1 &lt;&lt; &quot;: &quot; &lt;&lt; daily_temp[index] &lt;&lt; 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 &lt;&lt; &quot;The average high temperature during the &quot; &lt;&lt; num_values &lt;&lt; &quot;-day period was &quot; &lt;&lt; setprecision(2) &lt;&lt; average_high &lt;&lt; &quot; degrees.\n&quot;; return 0; }...
View Full Document

Ask a homework question - tutors are online