1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
using namespace std;
// tempstats.cpp
// purpose: analyze a list of daily temperatures
// input: a list of daily temperatures as integers
// output: some statistics about the temperature list
//
// output:
// highest: 67
// lowest: 41
// average: 52.3
// days above: xxx
// days below: xxx
//
// completed by: David Kim
// on: 02/22/12
//
const int MAX_TEMPS = 1000;
int read_in(int[]);
double get_avg(int[], int);
int get_max(int[], int);
int get_min(int[], int);
int get_num_above(int[], int, double);
int get_num_below(int[], int, double);
void report(int, int, double, int, int);
int main()
{
int temps[MAX_TEMPS];
int num_temps;
int highest, lowest;
double avg;
int days_above;
int days_below;
num_temps = read_in(temps); // read in the data
avg = get_avg(temps, num_temps);
highest = get_max(temps, num_temps);
lowest = get_min(temps, num_temps);
days_above=get_num_above(temps, num_temps, avg);
days_below=get_num_below(temps, num_temps, avg);
report(highest, lowest, avg, days_above, days_below);
return 0;
}
//
// read_in -- read in the temperatures from cin, store in array
// data consists of one integer representing the number of temps
// then that many ints
// returns: the number of temperatures read in
// affects: modifies the array by storing data
// note: if number of temps exceeds MAX_TEMPS, then complain and exit(1)
//
int read_in(int list[MAX_TEMPS])
{
int num;
cin >> num;
if ( num > MAX_TEMPS )
{
cout << "Too many temperatures." << endl;
exit (1);
}
for ( int i = 0; i < num; i++ )
{
cin >> list[i];
}
return num;
}
//
// get_avg -- compute the average of all the temps in the array
// args: list[] the data
// num the number of temps in the array
// rets: the average
// note: if num is 0, then print a message and exit(1)
//
double get_avg(int list[MAX_TEMPS], int num);
int total;
int current_sum = 0;
double average;
{
if ( num == 0 )
{
cout << "You have zero number of temperatures." << endl;
exit (1);
}
for ( int i = 0; i < num; i++)
{
current_sum = current_sum + list[i];
}
total = current_sum;
average = total / num;
return average;
}
|