Write a c++ program that reads in 10 midday temperatures for Port Elizabeth, for 10 consecutive days. Only temperatures higher than 0 and less than 45 are valid (working with integer values for temperatures). It must calculate and display the following: The warmest temperature The average temperature. The number of days that the temperature was higher than 30.
#include <iostream>
using namespace std;
const int MIN_TEMP=0;
const int MAX_TEMP=45;
const int HOT_TEMP=30;
int main() {
int temp, sum=0, hight_temp=0;
int num_valid=0, num_hot=0;
for (int i=0; i<10; ++i) {
cout << "Enter midday temperature for Port Elizabeth for dey " << i+1 << ": ";
cin >> temp;
if (temp < MIN_TEMP || temp > MAX_TEMP) {
continue;
}
num_valid++;
sum += temp;
if (temp > hight_temp) {
hight_temp = temp;
}
if (temp > HOT_TEMP) {
num_hot++;
}
}
cout << endl;
if (num_valid == 0) {
cout << "No valid data entered" << endl;
}
else {
cout << "The warmest temperature: " << hight_temp << endl;
int avg = sum / num_valid;
cout << "The average temperature " << avg << endl;
cout << "The number of hot day: " << num_hot << endl;
}
}
Comments
Leave a comment