In: Computer Science
Write a c++ program that does the following,
read temperatures from a file name temp.txt into an array, and
after reading all the temperatures, output the following information: the average temperature, the minimum temperature, and the total number of temperatures read.
Thank you!
Dear Student,
Below i have write the complete C++ program as per the requirement.
Please make sure you put the temp.txt file in the same directory where the program is
===================================================================Program:
===================================================================
//Calculating averages
//Data in file: temp.txt
#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;
int main()
{
//variables
double T;
double sum = 0, Avg;
double temp_arr[100];
int count = 0;
//creating a ifstream object
ifstream infile;
//open file for reading
infile.open("temp.txt");
//if file does not exist
if(!infile)
{
cout<<"failed to open the file."<<endl;
}
//start reading the temp value in the file
while(infile>>T)
{
temp_arr[count] = T;
//calculate sum
sum = sum + T;
//incremnt count
count++;
}
double min_temp = temp_arr[0];
for(int i =0; i<count; i++)
{
if(temp_arr[i] < min_temp)
{
min_temp = temp_arr[i];
}
}
//calculate Avg
Avg = double(sum)/count;
//display Avg temp, min, total temperatures
cout<<"The Avg of temperature is :
"<<fixed<<setprecision(2)<<Avg<<endl;
cout<<"Min temperature is: "<<min_temp<<endl;
cout<<"Total number of temperature read is : "<<count<<endl;
infile.close();
return 0;
}
//end of the main function
==================================================================
Sample Output:
==================================================================temp.txt file
==================================================================
Kindly Check and Verify Thanks...!!!