Thread: Arrays help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Arrays help

    I am trying to make a code to read in from a file which looks like this

    2 2 (num contestants and judges)
    50 100 (scores contestant 1)
    90 98 (scores contestant 2)

    I want to read the scores into a [i][j] array and print it out and im having trouble.
    You can see where I tried to start my array. Any ideas/hints?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
    int i,j,max_c, max_j;
    int num_shows;
    
        
        FILE * fin;
        fin = fopen("ucfidol.txt","r");
        
        
        fscanf(fin,"%d", &num_shows);
        printf("%d\n",num_shows);
        fscanf(fin,"%d",&max_c);
        printf("Number of contestants = %d\n", max_c);
        fscanf(fin,"%d",&max_j);
        printf("Number of judges = %d\n", max_j);
        
        
    
    int scores[i][j];
        for(i=1;i<=max_c;i++)
            for(j=1;i<=max_j;j++)
            fscanf(fin,"%d",&scores[i][j]);
        
        for(i=1;i<=max_c;i++)
            for(j=1;i<=max_j;j++)   
            printf("%d", board[i][j]);
            printf("\n");
        
        
        
        
        fclose(fin);
    
    system("PAUSE");
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just read the whole line:
    Code:
    mj = -1, mc = -1;
    while( fgets( somebuffer, BUFSIZ, file ) != NULL )
    {
        if( sscanf( buf, "%d %d", &j, &c ) != 2 )
        {
            ...error, do whatever you want with this line
        }
        else
        {
            put values some place
        }
    }
    You didn't actually say what you were having trouble with.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Im just having trouble understanding how to read in the values to the array
    Is the array like a matrix as below? or am I completely wrong

    i=0 j=0, i=1 j=0
    i=0 j=1, i=1 j=1

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int foo[ ROWS ][ COLS ];
    Given an array of ROWS and COLS, you can run through each element like so:
    Code:
    for( r = 0; r < ROWS; r++ )
        for( c = 0; c <COLS; c++ )
            foo[ r ][ c ] ... do something with it
    In your above example:

    [0][0] = 50, [0][1] = 100
    [1][0] = 90, [1][1] = 98

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    ok for mine that i wrote...is the scanf correct to read in from the file how I want it to? Cause When I run it it compiles but the output stops working and Im not sure if its the print or the scanf

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also, arrays start at 0 and go through size-1. So if ROWS was 2, you could access 0 and 1, but not 2 or -1. So you need to adjust your looping to start at 0 and go to < instead of <=.

    The *scanf functions wants exact input matching. If it encounters anything it doesn't expect, it will have a fit. The best way to know if it's going to have a fit is to check the return value, and make sure it is what you expect.

    For example, if you are reading 1 thing at a time, then your return value should be 1. If it's not, something is there that it doesn't like. In which case, you probably just want to grab the line with fgets and discard it, or display it so you can see what was there that shouldn't have been.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    i guess what i dont get is how this reads...for the first run row is 0 and column is 0...then the next one is both plus one so you have 1 and 1...Does it run all the row commands until r<rows then go to the column portion?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int row, col;
    
        printf( "Pretend we have an array named a:\n" );
        for( row = 0; row < 4; row++ )
        {
            for( col = 0; col < 3; col++ )
                printf( "a[ row=%d ][ col=%d ]   ", row, col );
            printf( "\n" );
        }
        printf( "Does that help?\n" );
        return 0;
    }
    Compile and run that.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    yes alot thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 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