Thread: Help with arrays

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Help with arrays

    Reads from an external file called data2.dat note that the
    name is in lower case. The file has two columns. The first column is
    integers and the second is floating point. You are to create two
    columns of numbers. The numbers in the columns are composed of the
    numbers in the second column of data2.dat. If a zero is in column 1 of
    data2.dat the corresponding number from column 2 is to go into the new
    column 1. If a one is in column of data2.dat the corresponding number
    from column 2 is to go into column 2 of the out put. Note that the
    number corresponding with the first zero is to be the first output and
    the number corresponding with the first 1 is to be the second output
    on the same line as the first number. The total number of lines is the
    largest of the total numbers of either ones or zeros in column one of
    data2.dat. Note that if there are an unequal number zeros and ones
    then you should fill with 0.0 for the column with the smaller number
    of entries. Print both columns with a %5.lf format to the screen and
    to an external file called out.dat.



    Example from class

    data.dat
    0 7.3
    0 -2.2
    1 5.5
    0 9.2
    1 -2.2

    Output from program
    7.3 5.5
    -2.2 -2.2
    9.2 0.0


    So I know we have to use arrays but this is what I have as my code. It will print out the list but it is all 0s.

    Code:
    /* homework 7 */
    
    #include <stdio.h>
    #include <math.h>
    #define FILEIN "data2.dat"
    #define FILEOUT "out.dat"
    
    main()
    {
    
    /* Variables */
    int col_1;
    double col_2,x,y, num_data_pts=0;
    FILE *list_1;
    FILE *list_2;
    
    /* Open external file data */
    list_1=fopen(FILEIN,"r");
    list_2=fopen(FILEOUT,"w");
    
    /* Reading in the data */
    while((fscanf(list_1,"%i %lf",&col_1,&col_2))==2)
    {
    
    if(col_1==0)x==col_2;
    
    if(col_1==1)y==col_2;
    
    printf("%5.lf %5.lf \n",x,y);
    fprintf(list_2,"%5.lf %5.lf \n",x,y);
    }
    }
    Last edited by kiwimonster; 03-24-2009 at 02:13 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    I know i will probably have to rework this entire code I just dont know how to work arrays into this program. K so scratch the code from above. Here is my new code im stuck right now in main portion of the program.

    Code:
    /* homework 7 */
    /* array sorting of coloums */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    #define FILEIN "data2.dat"
    #define FILEOUT "out.dat"
    
    int main(void)
    {
       /* Declare Variables */
       int flag,i, nocol1=0, nocol2=0
       double nocol1[N], nocol2[N},x,y,col1,col2;
       FILE *indata;
       FILE *outdata;
    
       /* Open Files */
       indata=fopen(FILEIN,"r");
       outdata=fopen(FILEOUT,"w");
    
       /* Reading in data sorting */
       for(i=0; i<100; ++i)
         while((fscanf(indata,"%i %lf",&flag,&x))==2)
           if(flag==0)
            col1[nocol1]=x
            col1++;
    }
    Last edited by kiwimonster; 03-24-2009 at 02:16 PM. Reason: New Source Code

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    up

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1.your col1 var is not initialized
    2. use autoindent your code and see your second problem
    3. col1[nocol1] - more traditional way to write it will be nocol1[col1]
    4. check that you do not go outof bounds of your array
    5. why do you need for loop - while loop should be enough
    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

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    wouldn't I need the for loop for the data in my file? and i was using the while to open the file and read in the array. It is still not complete I am missing the second part i need for col 2 in the if statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM