2-d arrays

This is a discussion on 2-d arrays within the C Programming forums, part of the General Programming Boards category; I'm starting to feel bad for coming to the board so often I'm trying to make a program that asks ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    26

    2-d arrays

    I'm starting to feel bad for coming to the board so often
    I'm trying to make a program that asks the user for 6 numbers, how many times they would like to put in 6 numbers, and then print out all the sets of numbers. I'm pretty sure a 2-dimensional array is the way to go, but I'm not having any luck with getting this to work. It compiles, but won't do anything after entering in my numbers. Suggestions are welcomed and appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<time.h>
    
    int main(void)
    {
    int lotto[6][5];
    int r, c;
    int games = c;
    
       printf("How many games would you like to play?(5 games max) ");
       scanf("%d", &games);
    
       printf("enter in your 6 numbers \n");
    
      for(r=0; r<6; r++)
      {
         scanf("%d", &lotto[r]);
             for(c=0; c<5; c++)
            {
               scanf("%d", &lotto[c]);
            }
      }  //end for r
    
        for(r=0; r<6; r++)
       {
             printf("%d ", lotto[r]);
                for(c=0; c<5; c++)
                {
                     printf("%d ", lotto[c]);
               }
       }  //end for c
    
          printf("\n");
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<time.h>
    
    int main(void)
    {
    int lotto[6][5];
    int r, c;
    int games;
    /* originally you had this set to int games = c
    this means the value of games is whatever the value of c 
    is at that instant. Since c hadn't been initialized (hadn't had
    a value set for it) it could be anything*/
    
       printf("How many games would you like to play?(5 games max) ");
       scanf("%d", &games);
       /* games now has the number
       of times you want to loop, i.e the outer loop */
       
       printf("enter in your 6 numbers \n");
     
      /* You know how many games you want
      to play now so only ask for the numbers
      'games' times */
      for(r=0; r<games; r++)
      {
             for(c=0; c < 6; c++)
            {
               /* Get a value from the user
               for row 'c' and column 'r' */
               scanf("%d", &lotto[c][r]);
            }
      }  //end for r
    
        for(r=0; r<games; r++)
       {
                for(c=0; c < 6; c++)
                {
                     printf("%d ", lotto[c][r]);
               }
           /*At the end of each row printed
           print out a new line */
           printf("\n");
       }  //end for c
     
          system("PAUSE");
          return 0;
    }
    int games = c; means variable games is now the value of variable c. So if c was 6 then games would now be 6. If you changed the value of c later on the value of games would not change, and vice-versa. You read the number into games correctly but you never use games after that.

    There are many ways to think of and use 2-d arrays, but the easiest is to think of it just like a 2-d matrix. So you access a single element at a time on a particular row for a particular column, so the value in row 1, column 2 would be lotto[0][1].

    2-d arrays are actually arrays of arrays, so lotto[0] is the first array, lotto[1] is the second array etc. lotto[0][0] is the first element in the first array, etc. Because lotto[c] is an array, it's value is an address, so when you print it out you get the address in memory, not a value.
    Last edited by major_blagger; 03-13-2004 at 09:46 PM.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    I just can't thank you enough. And I'm most grateful for your easy-to-understand explanations.

  4. #4
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    If you don't initialize arrays, you can't be sure what value the elements contain, never good.

    To tie in with the array thing, you've looped twice, with 'magic numbers'. In such a case, I think a define statement would be better (makes it easier to modify later)

    As for the number of games, I suspect that this is how many times you want them to enter 6 numbers, though no loop exists.

    As far as I'm aware, that's an extravagant set of includes you have there. Going by the code you've placed below, all of them aren't needed.

    I'm not sure about the system("PAUSE") statement so I've left the library and the statement out. Sorry for that one.
    Code:
    #include <stdio.h>
     
    #define ARRAYSIZE 6
    #define GAMESIZE 5
    
    int main(void) {
            int lotto[GAMESIZE][ARRAYSIZE]; /* multi-dimensional array (2-D) */
            int games = 0, r = 0, i = 0;
    
            /* initialise array elements */
            for (i = 0; i < GAMESIZE; i++) {
                    for (r = 0; r < ARRAYSIZE; r++)
                            lotto[i][r] = 0;
                    }
    
            printf("How many games would you like to play?(%d games max) ", GAMESIZE);
            scanf("%d", &games);
            printf("Enter your %d numbers:\n", ARRAYSIZE); /* keeps in sync with any changes */
    
            /* get numbers for games */
            for (i = 0; i < games; i++) {
                    for (r = 0; r < ARRAYSIZE; r++)
                            scanf("%d", &lotto[i][r]);
                    }
            /* print numbers */
            for (i = 0; i < games; i++) {
                    for (r = 0; r < ARRAYSIZE; r++) 
                            printf("%d ", lotto[i][r]);
                    printf("\n");
                    }
    
            printf("\n");
            return 0;
            }
    Last edited by olbas; 03-13-2004 at 10:19 PM.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Won't
    Code:
    int lotto[GAMESIZE][ARRAYSIZE]={0};
    initialize all elements to 0
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Learnt something new, ty.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > int lotto[GAMESIZE][ARRAYSIZE]={0};
    It's usually OK for blanking arrays, but when you get into more complicated nestings (arrays within structures within arrays say), or more complicated initialisers, then you need to use additional braces to make the heirarchy more explicit.
    Code:
    #include <stdio.h>
    int main ( void ) {
        int a[5][5] = { 0, 1 };
        int b[5][5] = { { 0 }, { 1 } };
        printf( "%d %d\n", a[0][0], a[0][1] );
        printf( "%d %d\n", b[0][0], b[1][0] );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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