Write a program that computes the average temperature for days with fog in June
ID: 3543791 • Letter: W
Question
Write a program that computes the average temperature for days with fog in June - December, 1991.Write output to both the
screen and an output file named av_temp_foggy_days.dat. Include the file names in the code so there is no need to prompt the user for file names. (File names : " MAY91.DAT" etc.)
The order of data in the file is
Column: 1 2 3 4 9
Data: day 3 max T min T avg. T fog
Columns 8 through 18 are weather types with column 9 being fog. 1 = fog that day, 0 = no fog
that day.
Output: The average temperature for the 5 foggy days in May, 1991: 56.2.
Or
There were no foggy days in May, 1991
Explanation / Answer
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
FILE *june,*july,*aug,*sept,*oct,*nov,*dec,*out,*may;
float mint,maxt,avgt,d1,d2,d3,d4,totavg=0;
int day,fog,totdays=0;
dec = fopen("Dec91.dat","r+");
june = fopen("Jun91.dat","r+");
july = fopen("Jul91.dat","r+");
aug = fopen("Aug91.dat","r+");
sept = fopen("Sep91.dat","r+");
oct = fopen("Oct91.dat","r+");
nov = fopen("Nov91.dat","r+");
may = fopen("May91.dat","r+");
out = fopen("av_temp_foggy_days.dat","w+");
while(fscanf(may,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in May, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in May, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in May, 1991" <<endl;
totavg=0;
totdays=0;
while(fscanf(june,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in June, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in June, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in June, 1991" <<endl;
totavg=0;
totdays=0;
while(fscanf(july,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in July, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in July, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in July, 1991"<<endl;
totavg=0;
totdays=0;
while(fscanf(aug,"%d %f %f %f %f%f%f%f%d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in August, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in August, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in August, 1991"<<endl;
totavg=0;
totdays=0;
while(fscanf(sept,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in September, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in September, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in September, 1991"<<endl;
totavg=0;
totdays=0;
while(fscanf(oct,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in October, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in October, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in October, 1991 "<<endl;
totavg=0;
totdays=0;
while(fscanf(nov,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in November, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in November, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in May, 1991 "<<endl;
totavg=0;
totdays=0;
while(fscanf(dec,"%d %f %f %f %f %f %f %f %d",&day,&maxt,&mint,&avgt,&d1,&d2,&d3,&d4,&fog)!=-1)
{
if(fog==1)
{
totavg+=avgt;
totdays++;
}
}
if(totdays!=0)
{
totavg/=totdays;
fprintf(out,"The average temperature for the %d foggy days in December, 1991: %.2f ",totdays,totavg);
cout<<"The average temperature for the " <<totdays << " foggy days in December, 1991: "<<totavg<<endl;
}
else
cout<<"There were no foggy days in December, 1991 "<<endl;
fclose(may);
fclose(june);
fclose(july);
fclose(aug);
fclose(sept);
fclose(oct);
fclose(nov);
fclose(dec);
fclose(out);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.