Question

In: Computer Science

Write a C++ program that takes in a set of daily average temperatures (up to a...

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

Solutions

Expert Solution

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 :)


Related Solutions

Write a program that takes a set of m numbers up to size n and save...
Write a program that takes a set of m numbers up to size n and save them in an array. The program then allows you to search for any number in the array with O(1).
Write a C program that takes a double value and rounds it up or off using...
Write a C program that takes a double value and rounds it up or off using ternary expression in the program. Example, if -2.5 is inputted the value should give -3.0
Average daily temperatures (°C) in a northern city over a period of 60 days are presented...
Average daily temperatures (°C) in a northern city over a period of 60 days are presented in the partly completed grouped frequency distribution table below. Determine the missing values and complete the table. Class Frequency Relative Frequency Cumulative Frequency Cumulative Relative Frequency -20 to < 0.1 to < 0 0.15 15 0 to < 0.55 to < 20 57 20 to < 0.05 Total 60 - -
Q#3 Write a C++ program to read 10 temperatures in Celsius and to store the temperatures...
Q#3 Write a C++ program to read 10 temperatures in Celsius and to store the temperatures in an array of integers, then it should convert the temperatures to Fahrenheit and store the new values rounded to the nearest integer in another array . The program should display both temperatures in a table form.    F = 9/5 x C + 32 where F is temp in Fahrenheit and C temperature in Celsius
Write a program in C that takes the length and the integers to be stored in...
Write a program in C that takes the length and the integers to be stored in an array and shifts array by N positions. Example: Input the number of elements to store in the array (max 10) : 5 Input 5 integers to be stored : Index - 0 : 12 Index - 1 : 29 Index - 2 : 68 Index - 3 : 32 Index - 4 : 97 Input number of shifts : 2 Expected Output :...
The program should be written in C++ with comments Write a program that takes graduation rates...
The program should be written in C++ with comments Write a program that takes graduation rates (per 1000 of the population) for North, South, East, West and Central United States. Input the number from each of the regions in a function returning an int with the graduation rate to the main program. Also figure out which region has the highest graduation rate in another function and display the result from inside that particular function. So your function prototypes should be...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
PART A Write a C program that takes the names and surnames of the students and...
PART A Write a C program that takes the names and surnames of the students and then displays the initial and last name of the name on the screen. For instance, if Onur Uslu is entered as input, the output of our program must be O. Uslu. PART B Write a C program that asks the user to enter a string and then sends it to a function that does the following job and shows the response from the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT