1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/**********************************************************************
PROGRAM DESCRIPTION:
This program will read information from an input file, oil_wells.txt,
into a two-dimensional array and generate a report. The report will
contain the weekly average oil production from each well, the overall
average oil production, the number of wells, and the date. This report
will be printed to the screen and to an output file named
production_results.txt.
DESCRITPION OF VARIABLES:
NAME | TYPE | DESCRIPTION
--------------------------------------------------------------------------
ovr_average | double | weekly overall average for the wells
average | double | one-dimensional array for average per well
sum | double | sum of barrells per day to find well average
wells | int | one-dimensional array for well ID number
well_number | int | ID number for each well
production | int | a two-dimensional array of oil production
i | int | outer for loop control variable
j | int | inner for loop control variable
nrows | int | number of rows in the array
ncols | int | number of columns in the array
month | int | the month the oil information was recorded
day | int | the day the data was recorded
year | int | the year the data was recorded
quit | char | used to exit the program
****************************************************************************/
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "C:\\Users\\Matt\\Desktop\\oil_wells.txt"
#define outputfile "C:\\Users\\Matt\\Desktop\\assignment6\\production_results"
/* Main function */
int main(void)
{
/* Declare and initialize variables */
double average[21], sum=0, ovr_average=0;
int i, j, nrows, ncols, wells[21], well_number, month, day, year;
int production[21][8];
char quit;
FILE *prod, *results;
/* Open input and output files */
prod = fopen(inputfile,"r");
results = fopen(outputfile,"w");
/* Verify input file and read production data */
if(prod == NULL)
{
printf("\n\nError opening input file.\n\n");
printf("\n\n\nType q and press Enter to quit.\n");
do{
quit = getchar();
}while(quit != 'q');
return 0;
}
else
{
/* Read date */
fscanf(prod,"%i %i %i", &month, &day, &year);
/* Read number of rows and number of columns */
fscanf(prod,"%i %i", &nrows, &ncols);
/* Read data into production array */
for(i=0;i<=nrows-1;i++)
for(j=0;j<=ncols-1;j++)
fscanf(prod,"%lf",&production[i][j]);
}
/* Compute average per well */
for(i=0;i<=nrows-1;i++)
{
for(j=1;j<=ncols-1;j++)
{
sum = sum + production[i][j];
}
average[i] = sum/(ncols-1);
sum = 0;
}
/* Compute overall average */
sum = 0;
for(i=0;i<=nrows-1;i++)
{
sum = sum + average[i];
}
ovr_average = sum/(nrows-1);
/* Print headings and date*/
printf("**********************************************\n"
" OIL WELL PRODUCTION\n"
" WEEK OF %2i/%2i/%4i\n\n",month,day,year);
printf("WELL ID AVERAGE PRODUCTION \n"
" (IN BARRELS)\n");
fprintf(results,"**********************************************\n"
" OIL WELL PRODUCTION\n"
" WEEK OF %2i/%2i/%4i\n\n",month,day,year);
fprintf(results,"WELL ID AVERAGE PRODUCTION \n"
" (IN BARRELS)\n");
/*Print Calculated Data */
for(i=0;i<=nrows-1;i++)
{
wells[i] = production[i][1];
printf(" %3i %6.2f\n",wells[i],average[i]);
fprintf(results," %3i %6.2f\n",wells[i],average[i]);
}
/*printf("Overall average for %2i wells is %5.2fbarrels.\n",wells[i],ovr_average);
printf("**********************************************");
fprintf(results,"Overall average for %2i wells is %5.2fbarrels.\n",wells[i],ovr_average);
printf(results,"**********************************************");*/
/* Close the input data file */
fclose(prod);
fclose(results);
/* Exit program */
printf("\n\n\nType q and press Enter to quit.\n");
do{
quit = getchar();
}while(quit != 'q');
return 0;
}
/******************************************************************************/
|