Top of Form CSE1301E Lab Assignment #8 Objectives : Instructions: In this lab, y
ID: 3566982 • Letter: T
Question
Top of Form
CSE1301E Lab Assignment #8
Objectives:
Instructions:
In this lab, you will be examining a set of temperatures collected over a twenty four hour period. Be sure to make use of an array to store these temperatures. You will be required to read in the data points from a file. Write a function to read in the data. (See data details below.) After reading in the data, you will need to write a function that will calculate the average of the data set. Then you should write a function to display the temperatures in the table format shown below.
Table Format:
Hour
Temperature
Difference from Avg
1
35
-02.29
2
32
-05.29
Hour
Temperature
Difference from Avg
1
35
-02.29
2
32
-05.29
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
float AverageTemp(int l[],int n){
float total = 0;
for (int i = 0; i < n; i++){
total += l[i];
}
return (total/n);
}
int MaxTemp(int l[],int n){
int max = -1000000;
for (int i = 0; i < n; i++){
if (l[i] > max)
max = l[i];
}
return max;
}
int MinTemp(int l[],int n){
int min = 1000000;
for (int i = 0; i < n; i++){
if (l[i] < min)
min = l[i];
}
return min;
}
void PrintTable(int l[],int n,float avg){
cout << "Hour " << "Temperature " << "Difference from Avg" << endl;
cout.precision(4);
for (int i = 0; i < n; i++){
cout << (i+1) << " " << l[i] << " " << (l[i]-avg) << endl;
}
}
int main(){
ifstream infile;
infile.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try {
infile.open("input.txt");
string str;
getline(infile,str);
string res = "";
int l[24];
int j = 0;
for (int i =0; i < str.length(); i++){
if (str[i] == ' '){
int n = atoi(res.c_str());
l[j] = n;
j++;
res = "";
}
else{
res = res + str[i];
}
}
float aver = AverageTemp(l,24);
int max = MaxTemp(l,24);
int min = MinTemp(l,24);
PrintTable(l,24,aver);
ofstream outfile;
outfile.open("tempstats.txt");
outfile.precision(4);
outfile << "Average Temperature = " << aver << endl;
outfile << "Minimum Temperature = " << min << endl;
outfile << "Maximum Temperature = " << max << endl;
outfile.close();
}
catch (std::ifstream::failure e) {
std::cerr << "The File ---- input.txt ---- is not Here ";
}
}