In this assignment you will create two version of an overloa

In this assignment, you will create two version of an overloaded function with the name meanDailyTempChange. This function will be overloaded, meaning it can be called with different parameter lists. The main program called hw06-1.cpp is supplied to you with the function calls. Your assignment is the write the two version of the function and add it to the program. The program reads in the hourly average temperature in Fairbanks for the calendar year 2014. This is done using a filestream, which is similar to the the cin stream function you have already seen. Make sure the data file is in the same folder as your program.

The function you will write determines the average difference between the daily high and low temperature. There are two possible inputs: (1) a pair of Julian1 days as unsigned int, or (2) the month as a enumerated variable (enum). Both functions also take a reference to a vector that holds the data. This data are already loaded into a vector in the main program.

Your program must have or do the following:

Two version of the function meanDailyTempChange (i.e. overloaded).

Have the enum version call the unsigned int version (nested functions).

Use pass-by-reference functions.

Sample Output

Program that I have so far. I have the data under the file type in the program.

Solution

Here is the code with comments on how to solve this question. Since the .dat file is not available for testing its difficult. But please revert back if any issues. In that case request you to copy-paste the contents of .dat file into the question by editng the question. Please do rate the answer if it helped. Thank you very much.

#include <iostream>
#include <fstream>
#include <vector>

/* program to calculate the Mean of Daily change in temperature. What does this mean?
Every day, temperatures are recorded for 24 hours. Now the data given will have 365 x 24 readings if not leap year
and 366 *24 readings for a leap year. Now for a single day i.e 24 readings for that day, find the min and max.
Calculate the differenc max-min for the day. If more days are given repeat this step of
finding min,max for that day from the 24 readings and then the change in temperatur max-min for the day
When meanDailyTempChange is called with range of days , start and end day, calculate the max-min for each of those days
and then taken and average for those values. This is called Mean of Daily Temperature Change.

If a month name is passed , then calculate the change in temperature from day 1 of the month to last day in that
month. Using these differences , calculate the average i.e. mean.

Julian day means numbering of days in the start with 1 , 2, till 365 . So jan 1st is julian day 1, jan 31 is julian 31 , but
feb 1 is julian 32 ,feb 2nd is julian 33 and so ....till dec 31 is julian 365 (for non-leap)
*/
using namespace std;


enum Month {JAN=0, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

const int DAYS_IN_MONTH[]={ 31,28 ,31,30,31,30,31,31,30,31,30,31}; //please change 28 to 29 for month feb if the data being loaded is for leap year. 2014 is not leap

float meanDailyTempChange(const vector<float>&, const unsigned int&, const unsigned int&);
float meanDailyTempChange(const vector<float>&, const Month &);

int main() {
        ifstream inFile;
        vector<float> data;
        float value;

        Month thisMonth;

        inFile.open(\"temperature2014.dat\",ios::in);
        if (!inFile) cout << \"Failed to open file\ \";
        while(inFile>>value){data.push_back(value);}
        inFile.close();
      
       //The vector data consists of data for 1st day starting from index 0 to 23
       //day 2 data starting from index 24 to 47 and so on. Each day has 24 readings
  

       //edit the main function;

        cout << \"Mean for day 1 to 3 = \"<<meanDailyTempChange(data,1,3) << endl;
        cout << \"Mean for day 100 to 300 = \"<<meanDailyTempChange(data,100,300) << endl;
        cout << \"Mean for Feb = \"<<meanDailyTempChange(data,FEB) << endl;
        return 0;
}

//given the startDay (julian) and endDay, the function calculates the mean of daily temperature changes
//in that range of days
float meanDailyTempChange(const vector<float> &data,const unsigned int &startDay,const unsigned int &endDay)
{
   float hourlyTemp,dayMin,dayMax;
   vector<float> difference;
  
   for(int jday=startDay;jday<=endDay;jday++) //start from julian start day till end day
   {
       dayMin=0;
       dayMax=0;
       for(int hour=0;hour<24;hour++) //for 24 hours
       {
          
           hourlyTemp=data[(jday-1)*24+hour]; //get the temperature for the current hour of the day
           if(hourlyTemp<dayMin || dayMin==0) //if the current hourly temperature is lesser than min for the day or setting for 1st time
               dayMin=hourlyTemp; //save that temperature as min
           if(hourlyTemp>dayMax) //if the current hourly temperature is greater than max for that day
               dayMax=hourlyTemp; //save that temperature as min
       }
      
       //cout<<dayMin<<\" \"<<dayMax<<endl; //uncomment while testing
      
       //once all hours readings for the day are finished processing and max and min for day is found
       //now calculate the differnce and put in the difference vector
      
       difference.push_back(dayMax-dayMin); //since the question says temperature change, we calculate difference,
      
       //if it was given as mean temperature, then we do (min+max)/2
   }  
  
   //now calculate the mean
   float total=0,mean;
   for(int i=0;i<difference.size();i++)
   {
       total+=difference[i];
   }
   mean=total/difference.size();
  
   return mean;
}

//this function calculates the mean for temperature changes for the month specified. For this we can reuse the
//other version of the function which works on julian days. All we need to do is to calculate the start and end
//julian days based on the month

float meanDailyTempChange(const vector<float> &data,const Month &mon)
{
   int jstartDay=1,jendDay;
   for(int i=0;i<mon ;i++)
   {
       jstartDay=jstartDay+DAYS_IN_MONTH[i]; //add the number of days in the month
   }
  
   jendDay=jstartDay+DAYS_IN_MONTH[mon ]-1; //calculate the ending julian day number
  
   return meanDailyTempChange(data,jstartDay,jendDay);
   //example: for month feb, startday will be 1+31=32 so startday=32
   //endday = start +daysinmonth(feb)-1= 32+28-1=59 so endday =59
}

Tested with dummy data for 3 days. since data for other days not available , results for them is printed as 0

19
20
19
18
22
24
26
27
28
26
26
25
24
25
26
24
23
22
22
21
21
20
20
19
26
25
28
25
26
24
23
22
22
21
21
20
20
17
20
19
18
22
24
26
27
28
26
26
25
24
25
26
24
25
29
30
21
15
16
29
30
32
33
28
29
30
26
28
31
33
30
34

output

Mean for day 1 to 3 = 13.3333
Mean for day 100 to 300 = 0
Mean for Feb = 0

In this assignment, you will create two version of an overloaded function with the name meanDailyTempChange. This function will be overloaded, meaning it can be
In this assignment, you will create two version of an overloaded function with the name meanDailyTempChange. This function will be overloaded, meaning it can be
In this assignment, you will create two version of an overloaded function with the name meanDailyTempChange. This function will be overloaded, meaning it can be
In this assignment, you will create two version of an overloaded function with the name meanDailyTempChange. This function will be overloaded, meaning it can be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site