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 - -
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.
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a program in Objective C that takes an integer keyed in from the terminal and...
Write a program in Objective C that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in Eglish. So if the user types 647, the program should display the following: six four seven
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT