You are on Mars. One of your tasks is to send your robot out and record temperat
ID: 3757785 • Letter: Y
Question
You are on Mars. One of your tasks is to send your robot out and record temperatures in Celsius over a 20 sols period. Your robot did as instructed, and you uploaded the data to a file. In the file, the first column is what sol this is for, from 1 to 20. The second column is the temperature. For each sol, sum the data, and find the average and send this to a file, with the sol number and average temperature. Each sol does not have the same number of temperature readings. Also find the highest temperature recorded and the lowest temperature recorded. The Data file is on Blackboard. (I will provide you with the data file if needed)
You should submit along with your programs, a copy of your pseudocode or flowchart.
Please upload to blackboard by the due date only the source code (*.cpp file).
Explanation / Answer
/**C++ program that writes the temperatures on mars for each sol
with average temperature .
Write to maximum and minimum temperatures to file "readings.txt"*/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<limits>
using namespace std;
//set max value of array
#define MAX 20
//function prototypes
void writeToFile(float temp[]);
int main()
{
//assume that the following are robot recored temperaturs on mars
float marsTemps[MAX]=
{100.0,90.0,120.0,130.0,140.0,
100.0,110.0,120.0,130.0,140.0,
100.0,110.0,120.0,130.0,140.0,
100.0,110.0,120.0,130.0,250.0
};
//call the method writeToFile that writes the temp with average
//maximum and minimum temperautures
writeToFile(marsTemps);
system("pause");
return 0;
}
/**Method that takes the temp and creates an output
object file and write the sol number, average temperature
upto the each recoreded temperatures and write maximum and
minimum temperatures to file "readings.txt" */
void writeToFile(float temp[])
{
//open output file
ofstream fout;
fout.open("readings.txt");
if(!fout)
{
cout<<"File creation error. "<<endl;
system("pause");
exit(0);
}
float total=0;
float avg=0;
//assume that first element is the maximum and minimum temperatures
float maxTemp=temp[0];
float minTemp=temp[0];
for(int index=1;index<MAX;index++)
{
if(temp[index]>maxTemp)
maxTemp=temp[index];
if(temp[index]<minTemp)
minTemp=temp[index];
}
fout<<setw(10)<<"Sols"<<setw(10)<<"Average"<<endl;
for(int sol=0;sol<MAX;sol++)
{
//add to total
total+=temp[sol];
//set total to tempTotal
float temTotal=total;
//calculate the average temperature
avg=temTotal/(sol+1);
//write to file
fout<<setw(10)<<(sol+1)<<setw(10)<<fixed<<setprecision(2)<<avg<<endl;
//reset the tempTotal to zero
temTotal=0;
}
//write max and min temperatures to file with a precision of 2 decimal places
fout<<"Maximum Temperature :"<<setprecision(2)<<fixed<<maxTemp<<endl;
fout<<"Minimum Temperature :"<<setprecision(2)<<fixed<<minTemp<<endl;
//close file
fout.close();
}
----------------------------------------------------------------------------------------------------
Sample Output file :
readings.txt
Sols Average
1 100.00
2 95.00
3 103.33
4 110.00
5 116.00
6 113.33
7 112.86
8 113.75
9 115.56
10 118.00
11 116.36
12 115.83
13 116.15
14 117.14
15 118.67
16 117.50
17 117.06
18 117.22
19 117.89
20 124.50
Maximum Temperature :250.00
Minimum Temperature :90.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.