Thread: i/o file array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    i/o file array

    Hello i'm currently working on an i/o file dealing with arrays, basically I have to get the information from an input file get the sum and the average for each row and then get the sum, average, and standard deviation for each column. I got the rows down but i'm having a lot of trouble with the arrays since I barely started learning about them. Any help is appreciated

    Code:
    #include<stdio.h>           /* defines printf, scanf, fprint, fscanf, fopen, fclose*/
    
    
    int main()
    {
          double n1, n2, n3,sum, mean;
          double *sum1, *sum2, *sum3;
          double *ave1, *ave2, *ave3;
          double *sd1, *sd2, *sd3;
          int count;
          
          FILE *inp,         /*input data*/
                *outp;       /*output data*/
           
              /* Open the input and output files. */
                inp = fopen("numbers.dat", "r");
                outp = fopen("result.out", "w")
              
           /* Displays the table layout */
           printf("      Count   #1        #2        #3        Sum        Average\n");
           printf("      ----   ---------  --------  --------  --------  --------\n");
           /* while loop scanning input file rows and printing output results */
             while (fscanf(inp, "%lf %lf %lf", &n1, &n2, &n3) != EOF) {
             sum=n1+n2+n3;
             mean=sum/3;
             count=count+1;
             printf("%9.3d %9.3lf %9.3lf %9.3lf %9.3lf %9.3lf\n", count, n1, n2, n3, sum, mean);
             fprintf(outp, "%9.3d %9.3lf %9.3lf %9.3lf %9.3lf %9.3lf\n", count, n1, n2, n3, sum, mean);
             }
      
           printf("      ----   ---------  --------  --------  --------  --------\n");
             /* Use only the first two columns. */
             for ( count = 0; count < 17; ++count )
             {
                printf("sum1 = %9.3f, sum2 = %9.3f, sum3 = %9.3f\n", sum1[count], sum2[count], sum3[count]);
             }
           printf("      Sum\n");
    fclose(inp);
    fclose(outp);
    
    
           system("pause");
           
           return (0);
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    -0.043200 -0.003471 0.000000
    -0.040326 -0.004851 -0.000737
    -0.018204 -0.004246 -0.001530
    0.022249 0.008891 0.004870
    0.074892 0.044237 0.032171
    0.129600 0.100233 0.089016
    0.174747 0.160100 0.161792
    0.200242 0.199106 0.214417
    0.200242 0.199106 0.214417
    0.174747 0.160100 0.161792
    0.129600 0.100233 0.089016
    0.074892 0.044237 0.032171
    0.022249 0.008891 0.004870
    -0.018204 -0.004246 -0.001530
    -0.040326 -0.004851 -0.000737
    -0.043200 -0.003471 0.000000

    oh yea and here are the numbers from the input file in case any of you want to try out the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with array def in different file
    By dford425 in forum C Programming
    Replies: 6
    Last Post: 03-24-2011, 06:15 PM
  2. getting an array from a file
    By kgehrma in forum C Programming
    Replies: 13
    Last Post: 02-01-2011, 04:48 PM
  3. Replies: 1
    Last Post: 11-08-2010, 02:03 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. file into 2d array
    By lakai02 in forum C Programming
    Replies: 0
    Last Post: 12-22-2002, 04:02 PM

Tags for this Thread