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

This is a discussion on Help - Reading a file and storing it as a 2d Array. within the C Programming forums, part of the General Programming Boards category; I'm having trouble reading a data file of size 190x190 and storing it as a 2d array. After i figure ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    UC Davis, California
    Posts
    4

    Talking 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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MetallicaX View Post
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    Can you provide a sample input file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 06:17 AM
  2. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 04:01 PM
  3. Replies: 5
    Last Post: 10-02-2005, 01:15 AM
  4. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 04:04 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21