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

Problem 2: (10 marks) Write a program in C or C++ that takes a number series...
Problem 2: 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...
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called...
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called temperatures01 that reads a (non-empty) sequence maximum daily temperatures. Your program should first ask for the number of temperatures to read and dynamically allocate an array just big enough to hold the number of temperatures you read. You should then read in the elements of the array using a loop. You then should print out the elements of the array in reverse order (from...
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 :...
Write a program in C++ that generates a random number between 1 and 10 and asks...
Write a program in C++ that generates a random number between 1 and 10 and asks the user to guess it. Your program should continue until the user guesses the correct number. With each guess the user makes the program tells the user if the guess is too high or too low. To generate a random number between 1 and 10 you need the following code: /* initialize random seed: */ srand (time(NULL)); /* generate secret number between 1 and...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT