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

Write a C++ funtion that reads data from a file called temperature.dat and procc

ID: 653865 • Letter: W

Question

Write a C++ funtion that reads data from a file called temperature.dat and proccess it per the requirements.

The data from the file is as follows:

56 57 58 59 59 60 61 62 63 64 65 67 68 69 70 69 68 68 67 66 67 66 64 62
61 61 61 60 60 60 60 61 62 62 63 64 65 67 69 70 71 72 73 72 70 68 66 65
64 63 64 64 64 64 64 63 63 62 62 63 64 66 68 71 71 72 72 71 71 70 68 67
66 66 66 65 65 64 64 64 65 65 66 67 68 68 70 72 73 74 73 72 70 68 67 65
64 64 63 63 62 61 62 63 63 62 63 64 65 66 68 68 69 69 70 69 70 69 68 67
67 66 65 64 63 63 63 62 61 62 63 64 65 65 65 66 67 67 67 66 66 65 65 65
64 63 63 63 63 62 61 60 61 62 63 64 65 66 67 69 70 72 73 72 71 71 70 68

Requirements:

Write a function with the following prototype:
double find_average(double temperature[][24], int days);

This function takes a parameter temperature which is a two-dimensional array of some number of days, of 24 hours each. The second parameter days is the number of days for which to access the array temperature

This function needs to find and return the average of all the values in temperature. It must not print anything on the screen.

Explanation / Answer

#include<fstream>
#include <iomanip> // std::setprecision
//no of days
#define MAX 100
using namespace std;

double find_average(double temperature[][24], int days);

int main()
{
   int temperature[MAX][24];
   double tmp[MAX][24],Avg;
   ifstream input("temp.txt");
   int i=0,j=0;

   if(!input) //Always test the file open.
   {
       cout << "Error opening output file" ;
       return -1;
   }
   while( !input.eof())
   {
       input>>temperature[i][0]>>temperature[i][1]>>temperature[i][2]>>temperature[i][3]>>temperature[i][4]>>temperature[i][5]>>temperature[i][6]>>temperature[i][7]>>temperature[i][8]>>temperature[i][9]>>temperature[i][10]>>temperature[i][11]>>temperature[i][12]>>temperature[i][13]>>temperature[i][14]>>temperature[i][15]>>temperature[i][16]>>temperature[i][17]>>temperature[i][18]>>temperature[i][19]>>temperature[i][20]>>temperature[i][21]>>temperature[i][22]>>temperature[i][23];
       i++;
   }
   int days=i;
   for( int i =0 ; i < days ; i ++)
   {
       for( int j=0 ; j < 24 ; j++)
       {
           tmp[i][j]=(temperature[i][j]-32) * (double)5/9; // convert fahrenheit to celsius
       }
   }

   Avg= find_average(tmp,i);

   cout<<"The average temperature over all the days was: ";
   cout << fixed;
cout << setprecision(2) <<Avg<<"C"<<endl;
  
}

double find_average(double temperature[][24], int days)
{
   double Avg=0;

   for( int i =0 ; i < days ; i ++)
   {
       for( int j=0 ; j < 24 ; j ++)
           Avg+=temperature[i][j];
   }
   Avg=(Avg)/(days*24);
   return Avg;
}

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