Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Build a program that will accomplish these tasks: 1. Read from 0 to 50 double te

ID: 3546820 • Letter: B

Question

Build a program that will accomplish these tasks:


1. Read from 0 to 50 double temperatures from a file, "temperature.txt".

2. Neatly display each temperature on the screen.

3. Compute the average of the temperatures and display the average on the screen.


Additional requirements:

a) The only global item that can be used is the MAXNUM of temps as a #define of 50

b) It is possible that there may not be anything in the file. (That would be a problem. In that case, don't display a report or compute the average. Just display an error message.

c) It is possible that the file may not have 50 valid temperatures in it - It may only have a couple of them - process accordingly.

d) There may be more than 50 valid temperatures in the file. If so, only read in the first 50.

e) Put in any necessary error checking. There may be invalid temperatures in the file. Do not include them, they must be ignored - since it is not possible to ask the file to re-enter a number as it is with a user doing the entry. Valid temperatures are between -40 and 120.

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<fstream>
#define MAXNUM 50
using namespace std;
int main()
{
ifstream infile("temperature.txt");
double temperature_array[MAXNUM];
if(!infile)
{
cout << "unable to open file temperature.txt. so exiting from prorgam " << endl;
return 0;
}
int index=0;
while(!infile.eof())
{
double temp;
infile >> temp;
if(temp>=-40 && temp<=120)
{
temperature_array[index] = temp;
index++;
}
if(index==MAXNUM)
break;
}
infile.close();
cout <<"Temperatures are ";
double sum = 0;
for(int i=0; i<index; i++)
{
cout << fixed << setprecision(2) << temperature_array[i] << " ";
sum = sum + temperature_array[i];
}
cout << endl;
cout << "Average of temperatures given by " << (sum/index) << endl;
//system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote