In: Computer Science
Write a C++ program that takes in a set of
daily average temperatures (up to a maximum of 30):
1.Ask the user for a temperature
2.If the user enters a -1 then stop asking for temperatures.
3. After the user is done entering temperatures:
a. Print out the temperatures entered.
b. print out the average, high and low temperatures.
To get average, use:
average = (sum of temps) divided by (count of temps)
to get max or min, either keep track of the max or min
as the user enters a number or you can do this later when
printing the temperatures. Do this by comparing the current
max
value to the number and replacing it if it is bigger (smaller for
min).
for example:
if(array[i] > max)
max = array[i];
you can initialize max to 0 and min to some large number (1000)
for
this exercise but in the future (during exams) you should set it to
the
first element in the list.
NOTE: Use floating point type for all numbers to prevent truncating
the decimal part of the average.
Requirements:
You must enter temperatures into an array and use this array for
calculating the average and print out the temperatures.
Use two loops, one for entering numbers and one for printing the
temperatures.
--------------------------------
EXAMPLE OUTPUT #1
--------------------------------
Enter temperature (enter -1 to quit):
89
Enter temperature (enter -1 to quit):
78
Enter temperature (enter -1 to quit):
65
Enter temperature (enter -1 to quit):
77
Enter temperature (enter -1 to quit):
90
Enter temperature (enter -1 to quit):
79
Enter temperature (enter -1 to quit):
-1
The temperatures are: 89 78 65 77 90 79
The average temperature is: 79.6667
The maximum temperature is: 90
The minimum temperature is: 65
--------------------------------
EXAMPLE OUTPUT #2
--------------------------------
Enter temperature (enter -1 to quit):
20
Enter temperature (enter -1 to quit):
18
Enter temperature (enter -1 to quit):
45
Enter temperature (enter -1 to quit):
34
Enter temperature (enter -1 to quit):
56
Enter temperature (enter -1 to quit):
45
Enter temperature (enter -1 to quit):
37
Enter temperature (enter -1 to quit):
37
Enter temperature (enter -1 to quit):
-1
The temperatures are: 20 18 45 34 56 45 37 37
The average temperature is: 36.5
The maximum temperature is: 56
The minimum temperature is: 18
Code:
#include <iostream>
using namespace std;
int main() {
// array named temp to store entered temperature values
int temp[30];
int i,temperature;
i=0;
// given that we can read temperature upto maximum of 30
// so we have to iterate loop through 30 times
while(i<=30){
// taking temperature form the user
cout << "Enter temperature (enter -1 to quit):"
<<endl;
cin >> temperature;
// if the temperature value is -1
// we have to quit so we breaking the loop
if (temperature == -1){
break;
}
// if not -1 then add that temperature to the array by incrementing
the array index
else{
temp[i++]=temperature;
}
// here i becomes the number if number of temperatures
}
// after reading values
// intialise the values of min to some maximum value amd max to
some minimum value
int max = -100,min = 1000,sum=0;
// loop for printing the temperatures and finding maximum and
minimum temperatures
cout << "The temperatures are: ";
for (int j=0;j<i;j++){
// printing temperature
cout << temp[j] << " ";
// finding the sum of all temperatures
sum = sum+temp[j];
// checking current value of the array is greater than the previous
max
if(temp[j]>max){
// if greater then update the max value
max = temp[j];
}
// checking current value of the array is less than the previous
min
if(temp[j]<min){
// update the min value
min = temp[j];
}
}
// after the end of the loop the values of the max and min are
the
// maximum and minimum temperatures in the given set of
temperatures
cout <<endl;
// finding avg of the temperatures using the sum computed in the
loop
double avg = (double)sum/i;
// printing the values
cout << "The average temperature is: " << avg <<
endl ;
cout << "The maximum temperature is: " << max <<
endl ;
cout << "The minimum temperature is: " << min <<
endl ;
}
Attachments:
Output-1;
Output-2
Any queries comment please
if you like my explanation please upvote my answer
Thank you :)