Exercise 2 : Mining Temperature Data
The highs and lows of the 3 first weeks of 2020 temperature data are available on a piece paper and we have decided to store
them in a three-dimensional array in which the first index represents the 3 first weeks of the year, and take the value from 0 to
2; the second index is numbered 0 through 6 and represents the days of the week, and the last index which is numbered 0 and
1, represents the day’s high and low temperatures, respectively.
(2.1) Write a C program that enables you to capture the 3 first weeks of 2020 temperature data, from the keyboard, and store
them in the array described above. You must check the validity of any data entered.
(2.2) Write functions to request the following:
(a) Any day’s high and low temperature
(b) Average high and low temperature for given week
(c) Week and day with the highest temperature
(d) Week and day with the lowest temperature
(2.3) Write a driver program to test your functions defined in (2.1) and (2.2).
#include <stdio.h>
void highAndLow(int temperature[3][7][2], int week, int day)
{
printf("For the given day high was %d and low was %d.\n\n", temperature[week][day][0], temperature[week][day][1]);
}
void avgHighAndLow(int temperature[3][7][2], int week)
{
float avgHigh = 0, avgLow = 0;
for(int i = 0; i < 7; i++)
{
avgHigh += temperature[week][i][0];
avgLow += temperature[week][i][1];
}
avgHigh/=7;
avgLow/=7;
printf("For the given week average High is %f and average Low is %f.\n\n", avgHigh, avgLow);
}
void highestTemp(int temperature[3][7][2])
{
int temp = -1;
int week, day;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 7; j++)
{
if(temp < temperature[i][j][1])
{
temp = temperature[i][j][1];
week = i + 1;
day = j + 1;
}
}
}
printf("Highest temperature occurs at Week-%d and Day-%d\n\n", week, day);
}
void lowestTemp(int temperature[3][7][2])
{
int temp = 105;
int week, day;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 7; j++)
{
if(temp > temperature[i][j][1])
{
temp = temperature[i][j][1];
week = i + 1;
day = j + 1;
}
}
}
printf("Lowest temperature occurs at Week-%d and Day-%d\n\n", week, day);
}
int main()
{
int temperature[3][7][2];
for(int i = 0; i < 3; i++)
{
printf("Week %d \n",i+1);
for(int j = 0; j < 7; j++)
{
printf("\tDay %d\n", j+1);
printf("\t\tEnter day's high temperature: ");
scanf("%d", &temperature[i][j][0]);
while(temperature[i][j][0] < 0 || temperature[i][j][0] > 100)
{
printf("\t\tInvalid temperature!!\n");
printf("\t\tEnter again: ");
scanf("%d", &temperature[i][j][0]);
}
printf("\t\tEnter day's low temperature: ");
scanf("%d", &temperature[i][j][1]);
while(temperature[i][j][1] < 0 || temperature[i][j][1] > 100)
{
printf("\t\tInvalid temperature!!\n");
printf("\t\tEnter again: ");
scanf("%d", &temperature[i][j][1]);
}
}
}
printf("\nData stored successfully !!\n\n");
int choice, day, week;
menu:
printf("Choose one of the options given below\n");
printf("1. Any Day's high and low temperature\n");
printf("2. Average high and low temperature for a given week\n");
printf("3. Week and day with the highest temperature\n");
printf("4. Week and day with the lowest temperature\n");
printf("5. Exit\n\n");
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Week: ");
scanf("%d", &week);
while(week>3 || week <1)
{
printf("Invalid week!!\n");
printf("Enter week again: ");
scanf("%d", &week);
}
printf("Enter Day: ");
scanf("%d", &day);
while(day>7 || day<1)
{
printf("Invalid day!!\n");
printf("Enter day again: ");
scanf("%d", &day);
}
printf("\n");
highAndLow(temperature, week - 1, day - 1);
goto menu;
case 2:
printf("Enter Week: ");
scanf("%d", &week);
while(week>3 || week <1)
{
printf("Invalid week!!\n");
printf("Enter week again: ");
scanf("%d", &week);
}
printf("\n");
avgHighAndLow(temperature, week-1);
goto menu;
case 3:
printf("\n");
highestTemp(temperature);
goto menu;
case 4:
printf("\n");
lowestTemp(temperature);
goto menu;
case 5:
break;
default:
printf("Choose a valid option!!\n\n");
goto menu;
}
}
Comments
Leave a comment