Your program is to use temperature data stored in a file cal

Your program is to use temperature data stored in a file called temp.dat. The information is stored as follows: The first line of the file contains the number of days in a month. The remaining lines contain one value per line. Each of these numbers the high temperature for that day. All values are integers. You will write a C program which first reads in the number of days in the month. The data is the high temperature and was collected once a day. Your program must read in the temperatures, calculate the average temperature, and print this monthly average accurate to two decimal places. Next for each day of the month the day\'s number and the high temperature for the day are printed. Finally, for every day in a sequence of 3 or more consecutive days above the monthly average the program is to display a \'+\' beside the temperature. Numbers should line up justified to the right. Your program must use at least one array. See the sample output below. Average temperature 91.97 1 85 2 87 3 92 + 4 95 + 5 97 + 6 98 + 7 90 8 84 9 87 10 93 + 11 95 + 12 96 + 13 96 + 14 94 + 15 90 16 91 17 93 + 18 96 + 19 94 + 20 95 + 21 96 + 22 94 + 23 95 + 24 89 25 89 26 89 27 87 28 97 29 88 30 87

Solution

#include<stdio.h>
int main()
{
//File pointer created
FILE *q;
//Opens temp.dat file in read mode
q = fopen(\"temp.dat\", \"r\");
int month[60], temp[60], ch;
int c = 0, d, totalT = 0;
float avgT;
//Checks whether the file can open or not
if(!q)
{
perror(\"Error opening file\");
return -1;
}
//Loops till end of file or counter is less than 60
while (!feof(q) && c < 60)
{
//Loops till 30 for month
for(d = 0; d < 30; d++)
{
//If data is not space store it in month
if((fscanf(q, \"%c\", &ch)) != \' \')
{
fscanf(q, \"%d\", &month[d]);
c++;
}
else
c++;
}
//Loops till 30 for temperature
for(d = 0; d < 30; d++)
{
//If data is not space store it in temperature
if((fscanf(q, \"%c\", &ch)) != \' \')
{
fscanf(q, \"%d\", &temp[d]);
c++;
}
}
}
//Calculates the total temperature
for(d = 0; d < 30; d++)
totalT = totalT + temp[d];
//Calculates the average temperature
avgT = (float)totalT / 30;
//Displays the information as per the requirement
printf(\"\ Average temperature %.2f\", avgT);
for(d = 0; d < 30; d++)
{
//Adds + if temperature is greater than average temperature
if(temp[d]>avgT)
printf(\" %d %d +\",month[d], temp[d]);
else
printf(\" %d %d \",month[d], temp[d]);
}

fclose(q);
}

Output:

Average temperature 91.97 1 85 2 87 3 92 + 4 95 + 5 97 + 6 98 + 7 90 8 84 9 87 10 93 + 11 95 + 12 96 + 13 96 + 14 94 + 15 90 16 91 17 93 + 18 96 + 19 94 + 20 95 + 21 96 + 22 94 + 23 95 + 24 89 25 89 26 89 27 87 28 97 + 29 88 30 87

Your program is to use temperature data stored in a file called temp.dat. The information is stored as follows: The first line of the file contains the number o
Your program is to use temperature data stored in a file called temp.dat. The information is stored as follows: The first line of the file contains the number o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site