I am supposed to write a program in c++ that read info from an input file. the i
ID: 3554183 • Letter: I
Question
I am supposed to write a program in c++ that read info from an input file. the input file is a two dimensional array with data for 21 oil wells. The first column of the data is the ID number for each oil well. The next seven columns are the number of barrels of oil the wells produced for the seven day period (7 columns after ID column). With this data im supposed to calculate the average oil production for the week and then the overall oil production along with the number of wells. here is my input file:
3 31 2014
21 8
52 87 136 0 54 60 82 51
63 54 73 88 105 20 21 105
24 67 98 177 35 65 98 0
8 23 34 52 67 180 80 3
64 33 55 79 108 118 130 20
66 40 44 63 89 36 54 36
67 20 35 76 87 154 98 80
55 10 13 34 23 43 12 0
3 34 46 187 34 202 23 34
2 98 98 87 34 54 100 20
25 29 43 54 65 12 15 17
18 45 65 202 205 100 99 98
14 36 34 98 34 43 23 9
13 0 9 8 4 3 2 10
36 23 88 99 65 77 45 35
38 23 100 134 122 111 211 0
81 23 34 54 98 5 93 82
89 29 58 39 20 50 30 47
99 100 12 43 98 34 23 9
45 23 93 75 93 2 34 8
88 23 301 23 83 23 9 20
The first row is the month, day, year (which I also have to print) the second row is the rows and columns numbers, and the third row starts the well data. Here is my code so far:
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "C:\Users\Matt\Desktop\assignment6\oil_wells.txt"
#define outputfile "C:\Users\Matt\Desktop\assignment6\production_results"
/* Main function */
int main(void)
{
/* Declare and initialize variables */
double well_average=0, ovr_average=0;
int i, j, nrows, ncols, date, wells, well_number, month, day, year;
int production[8][21];
char quit;
FILE *prod, *results;
/* Open input and output files */
prod = fopen(inputfile,"r");
results = fopen(outputfile,"w");
/* Verify input file and read elevation data */
if(prod == NULL)
{
printf(" Error opening input file. ");
printf(" Type q and press Enter to quit. ");
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=0;j<=ncols-1;j++)
Now Im getting confused because im not sure how I am supposed to seperate the first column from the others since they respresent different information. If someone could help me out with how to tackle this I would appreciate it.
Explanation / Answer
please rate - thanks
you had your dimensions reversed.
hope this is what you needed - your question wasn't that clear
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "C:\Users\Matt\Desktop\assignment6\oil_wells.txt"
#define outputfile "C:\Users\Matt\Desktop\assignment6\production_results"
/* Main function */
int main(void)
{
/* Declare and initialize variables */
double well_average=0, ovr_average=0;
int i, j, nrows, ncols, date, wells, well_number, month, day, year;
int total,total_all_wells=0;
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 elevation data */
if(prod == NULL)
{
printf(" Error opening input file. ");
printf(" Type q and press Enter to quit. ");
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,"%d",&production[i][j]);
}
}
/* Compute average per well */
fprintf(results,"well # total average ");
for(i=0;i<=nrows-1;i++)
{total=0; //clear total for the well
for(j=1;j<=ncols-1;j++) //add production columns
total=total+production[i][j];
well_average=total/7.; //get and print average
fprintf(results,"%d %d %.2f ",production[i][0],total,well_average);
total_all_wells+=total; //add to total of all wells
}
ovr_average=total_all_wells/(int)nrows; //get average all wells
fprintf(results," Number of wells: %d ",nrows);
fprintf(results,"total production all wells: %d ",total_all_wells);
fprintf(results,"average production all wells: %.2f ",ovr_average);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.