Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

INTRODUCTION: Lake Powell is the second largest reservoir in the United States f

ID: 3699687 • Letter: I

Question

INTRODUCTION:

Lake Powell is the second largest reservoir in the United States following Lake Mead. It was formed after the completion of the Glen Canyon Dam on the Colorado River. Over an eight-year period (2000-2007) data was collected dealing with the height of the water in the reservoir measured in feet above sea level. A file called lake_powell contains the collected data.

EXAM:

Using only the techniques provided in demo programs and assignment programs, and using a defined structure, write a C program that reads from the input file the size of the array of structures, and then reads the reservoir name into a one-dimensional character array. The program will read from the input file into the array of structures the first column as a string of characters, the second column as an integer for the month number, and the remaining columns as the heights in feet above sea level.

The program will sort the data by month chronologically. The program will compute the average water height for each year and place the computed values in the last row of the array of structures. The program will compute the average water height for each month and place the computed values in the last column of the array of structures. The program will compute the overall eight-year average (do not include the monthly or yearly averages). The program will determine the minimum and maximum water heights for the eight-year period, and the month and year that the minimum and maximum occurred. The program will determine how many times for the eight-year period the water level exceeded the overall eight-year average (do not include the monthly or yearly averages).

The program will incorporate input and output files, if structures, an array of structures, and for loops. The defined structure will contain a character array, and integer variable, and double variables. The main function will contain the character array for the name of the reservoir. No other arrays are allowed in the program. Also, no C code copied from the internet will be allowed. Do not use while loops for any part of the program. Do not use any C++ code in the program.

Your program will print to the computer screen and print to an output file called lake_powell_report. The output format is given on page 2.

NOTE: When printing the array of structures, do not print a number in the last row/last column position. Review the output format provided on page 2.

OUTPUT FORMAT:

****************************************************************************************************

                                       LAKE POWELL RESERVOIR

                                       

                                                Year

             2000      2001      2002      2003      2004      2005      2006      2007     Monthly

Month                                                                                       Average

ssssssssss xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx  

    .           .         .         .         .         .         .         .         .         .

    .           .         .         .         .         .         .         .         .         .

    .           .         .         .         .         .         .         .         .         .

ssssssssss xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx  

ssssssssss xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx   xxxx.xx  

RESERVOIR STATISTICS:

Minimum water height above sea level was xxxx.xx feet in ssssssssss, xxxx.

Maximum water height above sea level was xxxx.xx feet in ssssssssss, xxxx.

Overall average water height above sea level was xxxx.xx feet.

Overall average height above sea level was exceeded xx times.

****************************************************************************************************

NAMING PROGRAM:

Your program must be named as follows:

exam_3_firstname_lastname

Submit Exam 3 to Blackboard like you have done for regular programming assignments.

REMEMBER:

Before submitting your completed program to Blackboard, the input and output file paths must be set to:

#define u:\engr 200\lake_powell.txt

#define u:\engr 200\lake_powell_report.txt

lake_powell.txt

********************************************************************************

PROGRAM DESCRIPTION

This program will reads from the input file the size of the array of structures,

and then reads the reservoir name into a one-dimensional character array. The

program will read from the input file into the array of structures the first

column as a string of characters, the second column as an integer for the month

number, and the remaining columns as the heights in feet above sea level. The

program will sort the data by month chronologically. The program will compute

the average water height for each year and place the computed values in the last

row of the array of structures. The program will compute the average water

height for each month and place the computed values in the last column of the

array of structures. The program will compute the overall eight-year average.

The program will determine the minimum and maximum water heights for the

eight-year period, and the month and year that the minimum and maximum occurred.

The program will determine how many times for the eight-year period the water

level exceeded the overall eight-year average. The program will incorporate

input and output files, if structures, an array of structures, and for loops.

The defined structure will contain a character array, and integer variable, and

double variables. The main function will contain the character array for the

name of the reservoir.

DESCRIPTION OF VARIABLES:

NAME | TYPE | DESCRIPTION

--------------------------------------------------------------------------------

i | int | index control variable for outer loop

nrows | int | number of rows in the array

ncols | int | number of columns in the array

month | char | The month of year

mnum | int | The monthenumber

hfasl | double | The heights in feet above sea level

mihght | double | Minimum water height above sea level

mxhgt | double | Maximum water height above sea level

mavg | double | Overall average water height above sea level

mavgexc | double | Overall average height above sea level was exceeded

year0 | double | year 2000

year1 | double | year 2001

year2 | double | year 2002

year3 | double | year 2003

tear4 | double | year 2004

year5 | double | year 2005

year6 | double | year 2006

year7 | double | year 2007

*******************************************************************************/

/* Preprocessor directives */

#include <stdio.h>

#include <math.h>

#define inputfile "u:\engr 200\lake_powell.txt"

#define outputfile "u:\engr 200\lake_powell_report.txt"

/* Define the structure to represent reservoir data */

struct height

{

char month[11];

int mnum;

double year0, year1, year2, year3, year4, year5, year6, year7, mavg;

};

/* Main function */

int main(void)

{

/* Declare variables */

struct height lake[13], temporary;

int nrows, ncols, i, j;

double data, ytol=0, mtol=0, mavg,yavg;

char pname[21];

FILE *powell,*report;

/* Open input file */

powell = fopen(inputfile,"r");

report = fopen(outputfile,"w");

/* Verify input file */

if(data == NULL)

{

printf(" ERROR OPENING INPUT FILE.");

printf(" PROGRAM TERMINATED. ");

return 0;

}

/* Read control numbers */

fscanf(powell,"%i",&nrows);

fscanf(powell,"%s",&pname);

  

/* Read input file into data array */

for(i=0; i<=nrows-1; i++)

{

fscanf(powell,"%s %i %lf %lf %lf %lf %lf %lf %lf %lf %lf",&lake[i].month,

&lake[i].mnum,&lake[i].year0,&lake[i].year1,&lake[i].year2,&lake[i].year3,

&lake[i].year4,&lake[i].year5,&lake[i].year6,&lake[i].year7,&lake[i].mavg);

}

/* Sort months into ascending order */

for(i=0; i<=nrows-1; i++)

{

for(j=0; j<=nrows-2; j++)

{

if(lake[j].mnum > lake[j+1].mnum)

{

temporary = lake[j];

lake[j] = lake[j+1];

lake[j+1] = temporary;

}

}

}

/* compute average water height for each year, month */

/* compute overall eight-year average */

/* Determine the min and max water heights for the eight years and month */

/* Determine how many times waterlevel exceded eight year average */

/* Print headings */

printf("*************************************************************"

"***************************************"

" %21s "

" Year"

" 2000 2001 2002 2003 2004 2005"

" 2006 2007 Monthly"

" Month "

" Average ",pname );

fprintf(report,"*************************************************************"

"***************************************"

" %21s "

" Year"

" 2000 2001 2002 2003 2004 2005"

" 2006 2007 Monthly"

" Month "

" Average ",pname );

/* Print array */

for(i=0; i<=nrows-2; i++)

{

printf(" %-11s %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f",

lake[i].month,lake[i].year0,lake[i].year1,

lake[i].year2,lake[i].year3,lake[i].year4,lake[i].year5,

lake[i].year6,lake[i].year7,lake[i].mavg);

fprintf(report," %-11s %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f",

lake[i].month,lake[i].year0,lake[i].year1,

lake[i].year2,lake[i].year3,lake[i].year4,lake[i].year5,

lake[i].year6,lake[i].year7,lake[i].mavg);

}

printf(" RESERVOIR STATISTICS:"

" Minimum water height above sea level was %7.2f feet in s, %4.0f."

" Maximum water height above sea level was %7.2f feet in ssssssssss, %4.0f."

" Overall average water height above sea level was %7.2f feet."

" Overall average height above sea level was exceeded %2.0f times.");

fprintf(report," RESERVOIR STATISTICS:"

" Minimum water height above sea level was %7.2f feet in ssssssssss, %4.0f."

" Maximum water height above sea level was %7.2f feet in ssssssssss, %4.0f."

" Overall average water height above sea level was %7.2f feet."

" Overall average height above sea level was exceeded %2.0f times.");

/* Print headings */

printf(" ******************************************************************"

"********************************** ");

fprintf(report," **********************************************************"

"****************************************** ");

/* Close input file */

fclose(powell);

fclose(report);

  

/* Exit program */

return 0;

}

/******************************************************************************/

Explanation / Answer

here is your modified code : ----------->>>>>>>>>>>

#include <stdio.h>
#include <math.h>
#define inputfile "lake_powell.txt"
#define outputfile "lake_powell_report.txt"

/* Define the structure to represent reservoir data */
struct height
{
char month[11];
int mnum;
double year0, year1, year2, year3, year4, year5, year6, year7, mavg;
};

/* Main function */
int main(void)
{
/* Declare variables */
struct height lake[13], temporary;
int nrows, ncols,i=0,j;
double data, ytol=0, mtol=0,mavg,yavg;
double min,max;
int numTimes = 0;
char pname[40];
FILE *powell,*report;

/* Open input file */
powell = fopen(inputfile,"r");
report = fopen(outputfile,"w");

/* Verify input file */
if(powell == NULL)
{
  printf(" ERROR OPENING INPUT FILE.");
  printf(" PROGRAM TERMINATED. ");
  return 0;
}

/* Read control numbers */
fscanf(powell,"%d",&nrows);
fscanf(powell,"%s",&pname);
for(i=0;i<=nrows-1;i++)
{
  fscanf(powell,"%s %i %lf %lf %lf %lf %lf %lf %lf %lf %lf",&lake[i].month,&lake[i].mnum,&lake[i].year0,&lake[i].year1,&lake[i].year2,&lake[i].year3,
&lake[i].year4,&lake[i].year5,&lake[i].year6,&lake[i].year7,&lake[i].mavg);
//printf("%s",lake[i].month);
}

/* Read input file into data array */
/* Sort months into ascending order */
for(i=0; i<=nrows-1; i++)
{
  for(j=0; j<=13-2; j++)
  {
   if(lake[j].mnum > lake[j+1].mnum)
   {
    temporary = lake[j];
    lake[j] = lake[j+1];
    lake[j+1] = temporary;
   }
  }
}/*

/* compute average water height for each year, month*/
double avg[8] = {0.0};
for(i = 0;i<nrows;i++){
  avg[0] = avg[0] + lake[i].year0;
  avg[1] = avg[1] + lake[i].year1;
  avg[2] = avg[2] + lake[i].year2;
  avg[3] = avg[3] + lake[i].year3;
  avg[4] = avg[4] + lake[i].year4;
  avg[5] = avg[5] + lake[i].year5;
  avg[6] = avg[6] + lake[i].year6;
  avg[7] = avg[7] + lake[i].year7;
  mavg = lake[i].year0 + lake[i].year1 + lake[i].year2 + lake[i].year3 + lake[i].year4 + lake[i].year5 + lake[i].year6 + lake[i].year7;
  
  lake[i].mavg = mavg/8;
}
lake[0].year0 = avg[0];
lake[0].year2 = avg[2];
lake[0].year3 = avg[3];
lake[0].year4 = avg[4];
lake[0].year5 = avg[5];
lake[0].year6 = avg[6];
lake[0].year7 = avg[7];
lake[0].year1 = avg[1];
/* compute overall eight-year average */
ytol = lake[0].year0 + lake[0].year1 + lake[0].year2 + lake[0].year3 + lake[0].year4 + lake[0].year5 + lake[0].year6 + lake[0].year7;
yavg = ytol/8;
lake[0].mavg = yavg;
/* Determine the min and max water heights for the eight years and month */
min = lake[1].year0;
max = lake[1].year0;
for(i = 1;i<nrows;i++){
  if(lake[i].year0 < min){
   min = lake[i].year0;
  }
  if(lake[i].year1 < min){
   min = lake[i].year1;
  }
  if(lake[i].year2 < min){
   min = lake[i].year2;
  }
  if(lake[i].year3 < min){
   min = lake[i].year3;
  }
  if(lake[i].year4 < min){
   min = lake[i].year4;
  }
  if(lake[i].year5 < min){
   min = lake[i].year5;
  }
  if(lake[i].year6 < min){
   min = lake[i].year6;
  }
  if(lake[i].year7 < min){
   min = lake[i].year7;
  }
  if(lake[i].year0 > max){
   max = lake[i].year0;
  }
  if(lake[i].year1 > max){
   max = lake[i].year1;
  }
  if(lake[i].year2 > max){
   max = lake[i].year2;
  }
  if(lake[i].year3 > max){
   max = lake[i].year3;
  }
  if(lake[i].year4 > max){
   max = lake[i].year4;
  }
  if(lake[i].year5 > max){
   max = lake[i].year5;
  }
  if(lake[i].year6 > max){
   max = lake[i].year6;
  }
  if(lake[i].year7 > max){
   max = lake[i].year7;
  }
}

/* Determine how many times waterlevel exceded eight year average */

for(i = 1;i<nrows;i++){
  if(lake[i].year0 > yavg){
   numTimes++;
  }
  if(lake[i].year1 > yavg){
   numTimes++;
  }
  if(lake[i].year2 > yavg){
   numTimes++;
  }
  if(lake[i].year3 > yavg){
   numTimes++;
  }
  if(lake[i].year4 > yavg){
   numTimes++;
  }
  if(lake[i].year5 > yavg){
   numTimes++;
  }
  if(lake[i].year6 > yavg){
   numTimes++;
  }
  if(lake[i].year7 > yavg){
   numTimes++;
  }
}

/* Print headings */
printf("*************************************************************""***************************************"
"                                                 %21s "
"                                                 Year"
"             2000       2001       2002      2003      2004      2005"
"      2006      2007      Monthly"
" Month                                                                                        Average ",pname );

fprintf(report,"*************************************************************"
"***************************************"
" %21s "
" Year"
" 2000 2001 2002 2003 2004 2005"
" 2006 2007 Monthly"
" Month "
" Average ",pname );



/* Print array */
for(i=0; i<=nrows-2; i++)
{
  printf(" %-11s   %7.2f %7.2f %7.2f   %7.2f   %7.2f   %7.2f   %7.2f   %7.2f   %7.2f",
  lake[i].month,lake[i].year0,lake[i].year1,
  lake[i].year2,lake[i].year3,lake[i].year4,lake[i].year5,
  lake[i].year6,lake[i].year7,lake[i].mavg);
fprintf(report," %-11s %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f",
lake[i].month,lake[i].year0,lake[i].year1,
lake[i].year2,lake[i].year3,lake[i].year4,lake[i].year5,
lake[i].year6,lake[i].year7,lake[i].mavg);
}
printf(" RESERVOIR STATISTICS:"
" Minimum water height above sea level was %7.2f feet"
" Maximum water height above sea level was %7.2f feet "
" Overall average water height above sea level was %7.2f feet."
" Overall average height above sea level was exceeded %d times.",min,max,yavg,numTimes);
fprintf(report," RESERVOIR STATISTICS:"
" Minimum water height above sea level was %7.2f feet in ssssssssss, %4.0f."
" Maximum water height above sea level was %7.2f feet in ssssssssss, %4.0f."
" Overall average water height above sea level was %7.2f feet."
" Overall average height above sea level was exceeded %2.0f times.");


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


/* Close input file */
fclose(powell);
fclose(report);
/* Exit program */
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote