# Help - Reading a file and storing it as a 2d Array.

• 03-08-2009
MetallicaX
Help - Reading a file and storing it as a 2d Array.
I'm having trouble reading a data file of size 190x190 and storing it as a 2d array. After i figure out what is wrong with reading the file and putting it into an array, I need to calculate the average, the highest number in the list, the location of that maximum, the maximum slope and the location of the maximum slope.

I'm sure when finding the locations i must use pointers. But before i even get to that point i can't get the array to work and calculate the average.

Here is my code:

Code:

```#include<stdio.h>  #include<stdlib.h> #include<math.h> #define array 36100 float avg(float a[190][], float *avg); int main () {  int i = 0;  float avgs = 0.0;  FILE *cfptr1; //open the sequential access file  cfptr1 = fopen("topography.txt","r"); //array for all of the #'s in the file  float a[190][190]; //scan and input values  for(i=0;i<36100;i++)   {   fscanf(cfptr1,"%d*[, ]\n",&a[190][190]);   } //call the #'s into the function  avg(a,&avgs);  printf ("The average elevation is %f\n",avgs); //close the file  fclose(cfptr1); //return value return 0; } //------------------------------------------------------- //Function to calculate the average elevation of the list //------------------------------------------------------- float avg(float a[190][], float *avgs)  {   float sum = 0.0;   int i = 0; //loop to get the sum  for(i=0; i<array; i++);   {     sum += a[190][i];   } *avgs = (sum)/36100.0; return; }```
I would appreciate any help i could get!!!

Here's the question if it makes it any clearer...

The data file “topography.dat” is available on the course smartsite. This file lists the
average elevation above sea level for a regular grid system with 4km cells that covers all
of California. The dimensions of the grid are 190 in the x direction and 190 in the y
direction.
The initial portion of the file up until the row with an “*” in the first column is
comments. The next two lines list information such as the grid dimensions. The rest of
the file is organized into 16 columns. The values are stored in the file such that each
complete row of grid cells is written in order, starting from column 1 and proceeding to
column 190. For example, the first 190 values in the file correspond to all the columns in
row 1.
(a) Read the file and store the results in a 2d array. Loop over the array and calculate the
following values:
average surface elevation.
maximum surface elevation and location of maximum surface elevation
maximum slope (defined as change in elevation/change in x or y) and location of
maximum slope

Thanks again!
-Robert
• 03-08-2009
Quote:

Originally Posted by MetallicaX
I'm having trouble reading a data file of size 190x190 and storing it as a 2d array. After i figure out what is wrong with reading the file and putting it into an array, I need to calculate the average, the highest number in the list, the location of that maximum, the maximum slope and the location of the maximum slope.

I'm sure when finding the locations i must use pointers. But before i even get to that point i can't get the array to work and calculate the average.

No, you can just use indexes, and I recommend you do so.

Here is my code:

with a few suggestions :)

Code:

```#include<stdio.h>  #include<stdlib.h> #include<math.h> #define array 36100 float avg(float a[][190]); int main () {  int i = 0;  float avgs = 0.0;  FILE *cfptr1; //open the sequential access file  cfptr1 = fopen("topography.txt","r");  //"rt",  may or may not be needed for you //array for all of the #'s in the file  float a[190][190]; //scan and input values  for(i=0;i<190;i++)  {     for(j = 0; j < 190; j++)      {         fscanf(cfptr1,"%d*[, ]\n", a[i][j]);     }  } //call the #'s into the function  avgs = avg(a,);  printf ("The average elevation is %f\n",avgs); //close the file  fclose(cfptr1); //return value return 0; } //------------------------------------------------------- //Function to calculate the average elevation of the list //------------------------------------------------------- float avg(float a[][190])  {   float sum = 0.0;   int i = 0; //loop to get the sum  for(i=0; i<array; i++);  {  for(j=0;j <array; j++)     {         sum += a[i][j];     }  } return (sum/36100.0); }```
It's always a good idea to print up some small sample of data after you've read it into
your array. Can save a lot of trouble troubleshooting a problem.

I have not run the above.
• 03-08-2009