Multiarray reading each column and determine average

Hey Guys,

How can I determine the average from each individual column?

/* Declare and initialize variables */
char month[12][11]= {"\nJanuary","\nFebruary", "\nMarch", "\nApril",
"\nMay", "\nJune", "\nJuly", "\nAugust", "\nSeptember", "\nOctober",
"\nNovember", "\nDecember"};
double height[12][8], aveheight, totheight=0.0, minheight, maxheight,
yrheight, annheight=0.0, aveheight2000;
int i, j, nrows, ncols, nummonth, year;
FILE *lake_powell,*report;

/* Open input and output files */
lake_powell = fopen(inputfile, "r");
report = fopen(outputfile, "w");

/* Verify input file and read input data */
if(lake_powell == NULL)
{
printf("\n\n\n\n ERROR OPENING INPUT FILE.");
printf("\n\n PROGRAM TERMINATED.\n\n\n");
return 0;
}
else
{
/* Read control number */
fscanf(lake_powell, "%i %i",&nrows,&ncols);

/* Read loop and water height table */
for(i=0; i<=nrows-1; i++)
{
for(j=0; j<=ncols-1; j++)
{
fscanf(lake_powell,"%lf", &height[i][j]);
}
}
}

/* Print headings */
printf("*******************************************************************"
"********************");
printf("\n LAKE POWELL RESERVOIR STATISTICS"
"\n (feet above sea level)\n\n"
" Year "
"\n 2000 2001 2002 2003 2004 2005 "
" 2006 2007\nMonth");
fprintf(report,"***********************************************************"
"******************************");
fprintf(report,"\n LAKE POWELL RESERVOIR"
"STATISTICS\n (feet above sea level)"
"\n\n Year "
"\n 2000 2001 2002 2003 2004 2005 "
" 2006 2007\nMonth");

/* Sum and overall average of water heights*/
minheight=height[0][0];
maxheight=height[0][0];
for(i=0; i<=nrows-1;i++)
{
for(j=0; j<=ncols-1; j++)
{
totheight=totheight + height[i][j];

if(height[i][j]< minheight)
{
minheight=height[i][j];
}
if(height[i][j] > maxheight)
{
maxheight=height[i][j];
}


}
}

/* Determine Average Height and number that exceed the average*/
for(i=0; i<=nrows-1;i++)
{
for(j=0; j<=ncols-1; j++)
{
aveheight = totheight/(nrows*ncols);
if(height[i][j] > aveheight)
{
nummonth ++;
}
}
}

/* Print array */
for(i=0; i<=nrows-1; i++)
{
for(j=0; j<=10; j++)
{
printf("%c", month[i][j]);
fprintf(report,"%c",month[i][j]);
}
for(j=0; j<=ncols-1; j++)
{
printf("%7.2f ", height[i][j]);
fprintf(report,"%7.2f ", height[i][j]);
}
}

/* Determine Yearly Averages*/
for(i=0; i<=nrows-1;i++)
{
for(j=0; j<=ncols-1; j++)
{
columntotal[j]=columntotal[j]+height[i][j];
printf("%7.2f ", columntotal[j]);
aveheight2000 = totheight/(nrows*ncols);
printf("\n%7.2f",aveheight2000);
}}

/* Print Yearly Averages*/
printf("\n\n YEARLY AVERAGES ");
fprintf(report, "\n\n YEARLY AVERAGES ");

/* Print height information, average heights, and months exceeded*/
printf("\n\nReservoir Statistics:");
printf("\nAverage water height above sea level is %7.2f feet.", aveheight);
printf("\nMinimum water height above sea level is %7.2f feet.", minheight);
printf("\nMaximum water height above sea level is %7.2f feet.", maxheight);
printf("\nNumber of months that exceeded the average height above sea level"
" is %2i", nummonth);
fprintf(report,"\n\nReservoir Statistics:");
fprintf(report,"\nAverage water height above sea level is %7.2f feet.",
aveheight);
fprintf(report,"\nMinimum water height above sea level is %7.2f feet.",
minheight);
fprintf(report,"\nMaximum water height above sea level is %7.2f feet.",
maxheight);
fprintf(report,"\nNumber of months that exceeded the average height above"
"sea level is %2i", nummonth);

/* Print headings */
printf("\n*****************************************************************"
"**********************");
fprintf(report,"\n*********************************************************"
"********************************");

/* Close input and output files */
fclose(lake_powell);
fclose(report);

/* Exit the program */
return 0;
}
Topic archived. No new replies allowed.