Question

In: Computer Science

Write a C++ program that takes 4 readings of temperature, each reading is between -10 and...

Write a C++ program that takes 4 readings of temperature, each reading is between -10 and 55. The program should: • Computes and print out the average temperature (avgTemp), maximum temperature (maxTemp) and minimum temperature (minTemp) of the day. • The program should also compute and print out the number of temperature readings (numTemp) that exceed 35. • Enforce validation on the user input (using while loop) and use appropriate format for the output. The average temperature should be rounded up the nearest digit.

without using arrays

Solutions

Expert Solution

 Source code of the program is given below.Detailed comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Source code

#include<iostream>
//including cmath header file to use round() function
#include<cmath>
using namespace std;
int main()
{
   //declaring variables to hold various values
   //assigning maxTemp to minimum possible and minTemp to maximum possible values
   float temp,avgTemp,maxTemp=-10,minTemp=55,total=0;
   int numTemp=0,i;
   //prompt user to enter 4 temperatures
   cout<<"Enter four temperature(in between -10 and 55):"<<endl;
   //initialize i with 1
   i=1;
   //loop executes its body as long as i is less than or equals to 4
   while(i<=4)
   {
       //prompt to enter temperature i
       cout<<"Enter temperature "<<i<<": ";
       //reading the temperature
       cin>>temp;
       //checking the temperature is invalid
       if(temp<-10||temp>55)
       {
           //if input is invalid, print error message and force to do next iteration
           //using continue statement without updating i value to its next
           cout<<"Invalid input!"<<endl;
           continue;
       }
       //if user input is valid
       else
       {
           //update total by adding temp into it
           total=total+temp;
           //checking if temp is greater than maxTemp
           if(temp>maxTemp)
           //if yes,set temp as new maxTemp
               maxTemp=temp;
           //checking if temp is less than minTemp
           if(temp<minTemp)
               //if yes,set temp as new minTemp
               minTemp=temp;
           //checking temp greter than 35
           if(temp>35)
               //if yes,increment numTemp by 1
               numTemp++;
           //incrementing i by 1
           i++;          
       }
   }
   //calculating average
   avgTemp=total/4;
   //printing results
   //avgTemp is rounded up to nearest digit using round() function
   cout<<"\nAverage Temperature: "<<round(avgTemp)<<endl;
   cout<<"Maximum Temperature: "<<maxTemp<<endl;
   cout<<"Minimum Temperature: "<<minTemp<<endl;
   cout<<"Number of temperature exceeding 35: "<<numTemp;
   return 0;  
}
Screen shot of the code

Screen shot of the output


Related Solutions

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...
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...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
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
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...
write c++ program that takes the depth ( in kilometer) inside the earth to compute and...
write c++ program that takes the depth ( in kilometer) inside the earth to compute and display the temperature at the depth in degrees celsius and fahrenheit. the relevant formulas are: celsius+ 10 x depth + 20 fahrenheit = 9/5 celsius + 23
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT