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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Lab4.h"
using namespace std;
int main()
{
const int NumTemperatures = 24;
int HourlyTemperatures[NumTemperatures];
int highValue = 0;
int lowValue = 0;
double AverageTemp = 0.0;
GetTemperatures(HourlyTemperatures, NumTemperatures);
AverageTemp = ComputeAverageTemp(HourlyTemperatures, NumTemperatures);
highValue = High(HourlyTemperatures, NumTemperatures);
lowValue = Low(HourlyTemperatures, NumTemperatures);
DisplayTemperatures(HourlyTemperatures, NumTemperatures, AverageTemp, highValue, lowValue);
return 0;
}
void GetTemperatures(int Temperatures[], int NumTemperatures)
{
int CurrentTemp = 0;
for (int counter = 0; counter < NumTemperatures; counter++)
{
cout << "Please enter the temperature for " << counter << ":00? ";
while (true)
{
cin >> CurrentTemp;
if (CurrentTemp > -49 && CurrentTemp < 131)
{
break;
}
else
{
cout << "That temperature is not within range\n";
cout << "Please enter a temperature between -50 and 130 degrees...\n";
cout << "Enter new temperature: ";
}
}
Temperatures[counter] = CurrentTemp;
}
}
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
double AverageTemp = 0.0;
int Count = 0;
int Sum = 0;
for (Count = 0; Count != NumTemperatures; Count++)
{
Sum = Sum + Temperatures[Count];
}
AverageTemp = Sum / NumTemperatures;
return AverageTemp;
}
int High(int Temperatures[], int NumTemperatures)
{
int highValue;
highValue = Temperatures[0];
for (int Count = 1; Count < NumTemperatures; Count++)
{
if (Temperatures[Count] > highValue)
highValue = Temperatures[Count];
}
return highValue;
}
int Low(int Temperatures[], int NumTemperatures)
{
int lowValue;
lowValue = Temperatures[0];
for (int Count = 1; Count < NumTemperatures; Count++)
{
if (Temperatures[Count] < lowValue)
lowValue = Temperatures[Count];
}
return lowValue;
}
void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp, int highValue, int lowValue)
{
int Count = 0;
cout << "\n";
cout << "\n";
cout << setw(5) << "Hour" << setw(32) << "Temperature \n";
while (Count < 24)
{
cout << setw(3) << Count << ":00" << setw(24) << Temperatures[Count] << endl;
Count++;
}
void pause();
{
cout << "Hit any key to continue ";
cin.ignore(2);
}
cout << "\n";
cout << " High Temperature: " << setw(13) << highValue << "\n";
cout << " Low Temperature: " << setw(14) << lowValue << "\n";
cout << " Average Temperature: " << setw(8) << AverageTemp << endl;
cout << "\n";
}
|