Two dimensional arrays with input files?

I have an assignment where im given an imput file with info that looks like this:

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

This is information about oil wells. All values in the file are integer type and the data file is space delimited. The first record line is the month, day, and year that the data was collected, and the second record line is the number of rows and columns in the data file. All record lines after the second line are as follows: the first number is the oil well identification number and the seven numbers to the right of the identification number are the production levels in barrels per day. With this info I have to write a program that will read the information from the input file into a two-dimensional array and generate a report that is illustrated below. The program will compute the weekly average oil production for each well, and compute the total of all weekly averages. Once the total of all the weekly averages is computed, the program will compute the overall average. The output file should look like the picture below:



The report format, when printed to the output file production_results, is illustrated below. Your program will also print to the computer screen using the same output format. Assume that all oil wells will have no more then a three digit identification number.

Here is what I have for code that doesn't work:

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;
}
/******************************************************************************/


Now, first of all the program wont run at all with this exact code. However if I take out the last four printf and fprint statements about overal average the program will run and print a bunch of zeroes for well ID number and well averages which is wrong. Also when I run it like that when i try to close it it says that "Run-Time Check Failure #2 - Stack around the variable 'production' was corrupted." so theres another probvlem ive found. Im thinking my zeroes problem is coming from variable type agreement but im not sure and I have no clue about the other stuff so if someone could help me out I would appreciate it a lot.
On line 70, you tell fscanf via the format specifier "%lf" that the pointer you feed to it (&production[i][j]) is pointing at space for an object of type double, however it is not. Every element of production is an int. Don't do that.
Thanks, dont know what I was thinking there.
Topic archived. No new replies allowed.