Question

Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

Change your C++ average temperatures program to:

  • In a non range-based loop
    • Prompt the user for 5 temperatures.
    • Store them in an array called temperatures.
  • Use a Range-Based loop to find the average.
  • Print the average to the console using two decimals of precision.
  • Do not use any magic numbers.

#include<iostream>
using namespace std;
void averageTemperature() // function definition starts here
{
   float avg; // variable declaration
   int num,sum=0,count=0; /* 'count' is the int accumulator for number of temperatures, 'num' is the int accumulator for storing temperature*/
   cout<<"Enter temperatures to calculate the average (enter -999 to stop):";
   while(cin>>num) // reading the integer input from the keyboard and storing in 'num'
   {
       if(num!=-999) // if 'num' is not -999
       {
           sum += num; // performs the sum
           count++; // increments the count of the temperatures
       }
       else // if 'num' is -999
           break; // jumps out of while loop
   }
   avg = (float)sum/count; // calculating the average of temperatures
   cout<<"The average temperature is:"<<avg;   // displaying the average temperature
}
int main()
{
   averageTemperature(); // function call
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

void averageTemperature() // function definition starts here
{
float avg; // variable declaration
int temp[5],sum=0,num; // 'num' is the int accumulator for storing temperature

cout<<"Enter 5 temperatures to calculate the average : ";

for (int i = 0; i < 5; i++) // non range-based loop
{
cin >> temp[i]; // reading the integer input from the keyboard and storing in temp
sum += temp[i]; // performs the sum
}

for (int num : temp) // Range-based loop
sum += num;

avg = (float)sum/5; // calculating the average of temperatures
cout<<"The average temperature is:"<<avg; // displaying the average temperature
}
int main()
{
averageTemperature(); // function call
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers)...

    #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • Question 2: This program continue asking for a new number until the user enters a 0...

    Question 2: This program continue asking for a new number until the user enters a 0 to terminate the program #include <iostream.h> using namespace std; int main(void) {         int x;         int count = 0;   // (1) initialize a counter to 0 to count number of values         int choice = 1; // This is the choice that controls the looping continuation or termination         double sum = 0; // initialize the sum to 0 to make sure the...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • 1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately...

    1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets use -9999) is entered. The program should then correctly report the number of non-negative scores entered and the arithmetic mean (average) of those scores 2. Demonstrate your programs behavior in response to errors on input (that is, show that it faithfully rejects improper input such as alphabetic characters, decimal points, and commas). Here are some...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT