Weather Calculator Program in C ++

 

A weather analysis program uses the following array to store the temperature for each hour of the day on each day of a week.

int temp[7][24];

Each row represents a day (0 = Sunday, 1 = Monday, etc.) and each column represent sa time (0 = midnight, 1 = 1 a.m., … , 12 = noon, 13 = 1 p.m., etc.).

  1. A) Write code to find Tuesday’s average temperature.
  2. B) Write code to find the average weekly noon temperature.

 

Solution

int main()

 

{

 

int temp[7][24];

 

/* tuesday = 2, noon = 12; */

 

cout<<“enter the two dimensional array with rows as days of the week and time as columns”;

 

for(int i=0;i<7;i++)

 

{for(int j=0;j<24;j++)

 

{cin>>temp[i][j];

 

}

 

} /* 00 will show position sunday and 12 noon

 

01 ->sunday 1 am

 

02->sunday 2 am

 

temp[1][0]->monday 1 am

 

temp[2][0]->tuesday 1am

 

temp[2][1]->tuesday 2 am

 

temp[2][2]->tuesday 3 am

 

similary we can find for all tuesdays in the array */

 

int tuesday_sum = 0, tuesday_averagetemp;

 

int noon_total=0, noon_average;

 

for (int i = 0; i<24; i++)

 

{

 

tuesday_sum += temp[2][i]; /* we have used 2 as it signifies tuesday */

 

}

 

tuesday_averagetemp = tuesday_total/24;

 

cout<<“Average temperature for tuesday is: “<<tuesday_averagetemp<<endl;

 

/* calculate average weekly noon temperature.Here we will fix the j variable as 12 as it detemines the time at 12 pm(noon) and we will increment i for days sunday to saturday from i=0 to i=6 */

 

for (int i=0; i<7; i++)

 

{

 

noon_total += temp[i][12];

 

}

 

noon_average = noon_total/7;

 

cout<<“” the average weekly noon temperature is “;

 

cout<<noon_average;

 

}