Thread: C Programming Help - Using Arrays and Files

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    C Programming Help - Using Arrays and Files

    Hi I would like some help on a programming problem, I'm new to all this and I'm new to the forum so I'll try my best to make this question easy to answer as possible.

    I've done everything to get through this problem but I just don't know how to do it, basically the program question is:

    "write a function to compute the average of a specified column of a 2-dimensional array that has NROWS rows and NCOLS columns. The parameters should be the integer array and the desired column. Assume that the corresponding function prototype is:

    double col_ave(int x[NROWS, NCOLS], int col); "

    well I've figured out how to find the average power output per week from a given data:

    (power1.dat)

    207 301 222 302 22 167 125
    367 60 120 111 301 499 434
    211 62 441 192 21 293 316
    401 340 161 297 441 117 206
    448 111 370 220 264 444 207
    21 313 204 222 446 401 337
    213 208 444 321 320 335 313
    162 137 265 44 370 315 322
    150 218 234 384 283 199 204
    204 245 287 298 302 288 297

    and this is the code I wrote below for that. However, the code ask for input so it was much easier to write. The problem I'm having is, how do I ask user to specify the column he/she wants to find the average of that involves arrays? how would I modify the code below to do this? I know this may not be easy to answer (I just only started to leanr programming for 3 weeks afterall), can someone at least give me a starting point? I mean I can ask user to input the column number he/she wants to find the average of but how do I specify the array of that column?

    and what does it mean to "Assume that the corresponding function prototype is:

    double col_ave(int x[NROWS, NCOLS], int col); " Thanks

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #define NCOLS 7
    #define NROWS 10
    #define FILENAME "power1.dat"
    
    int main(void)
    {
       /*  Define variables.  */
       int i,j; 
       int power[NROWS][NCOLS], col_sum;
       FILE *file_in;
    
       /*  Open input file.  */
       file_in = fopen(FILENAME,"r");
       
       /* Display Error msg if file cannot open */
       if (file_in == NULL)
          printf ("Error, cannot locate file.\n");
       else
       
           
       /*  Read data.  */
       {
           for (i=0; i<=NROWS-1; i++)
               for (j=0; j<=NCOLS-1; j++)
                   fscanf(file_in,"%d",&power[i][j]);
          
       for (j=0; j<=NCOLS-1; j++)
       {
           col_sum = 0;
           for(i=0; i<=NROWS-1; i++)
                  col_sum += power[i][j];
           printf("Average power output in week %d is %.2f. \n", j+1,(double)col_sum/NROWS);
       }
    
       /* Close File */
       fclose(file_in);
       }
       
    system("pause");
    return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this is x[NROWS, NCOLS], not a C (at least it does not what you could think it should do)
    So you should verify your assignment
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  2. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  3. reading files into arrays
    By SMB3Master in forum C Programming
    Replies: 12
    Last Post: 10-18-2003, 08:23 PM
  4. reading arrays from files
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 01:23 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM