The file power1.dat contains power plant output in megawattsover a period of 8 w
ID: 3552238 • Letter: T
Question
The file power1.dat contains power plant output in megawattsover a period of 8 weeks. Each row of data contains 7 floatingpoint numbers that represent one weeks data. In developing theprogram, use symbolic constraints NROWS and NCOLS to represent thenumber of rows and columns in the two dimensional array used tostore the data.
Write a function to compute the average of a specified column of a two-dimensional array that has NROWS rows and NCOLS columns. Assume that the corresponding function prototype is; (double col_ave(int x [NORWS,NCOLS], int col);) then use the function just written to print a report that lists the average power output for the first day of the week, then for the second day of the week, and so on. Print the information in this format; (Day x: Average Power Output in Megawatts: xxx.xx )
This are the values power1.dat filecontains:
207 301 222 302 22 167 125
367 60 120 111 301 499 434
211 62 441 192 21 293 316
401 340 161 297 441 117 206
448 111 370 220 264 444 207
21 313 204 222 446 401 337
213 208 444 321 320 335 313
162 137 265 44 370 315 322
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
#define NROWS 8
#define NCOLS 7
int main()
{int a[NROWS][NCOLS],i,j,sum=0,greater=0;
double average;
FILE *fp;
if(!(fp=fopen("power1.dat","r")))
{
printf(" Error opening%s ","power1.dat");
system("PAUSE");
return 1;
}
for(i=0;i<NROWS;i++)
for(j=0;j<NCOLS;j++)
{fscanf(fp,"%d",&a[i][j]);
sum+=a[i][j];
}
average=(double)sum/(NROWS*NCOLS);
for(i=0;i<NROWS;i++)
for(j=0;j<NCOLS;j++)
if(a[i][j]>average)
greater++;
printf("The average power consumption was %.2fmegawatts ",average);
printf("There were %d day(s) with greater-than-average poweroutput ",greater);
fclose(fp);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.