Array prints strange numbers?

This is a discussion on Array prints strange numbers? within the C Programming forums, part of the General Programming Boards category; What I am trying to do is to have the program create 2D array that for each row, no two ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22

    Array prints strange numbers?

    What I am trying to do is to have the program create 2D array that for each row, no two numbers are the same and only consists numbers between 1-9. The array lists out every possible combinations. Supposely I do not know the number of possible combinations, I had it keep adding new rows until there are no more possiblities left.

    The problem with my code is when I print out the completed array, it gives out really, really large numbers. But when I print the array from inside the first loop, it gives correct values. I do not know exactly what happened. Here is the code:

    Code:
    #include <stdio.h>
    int main(void)
    {
        int double_digit[1][2];
    
        int a = 1;
        int b = 1;
    
        int i = -1;
        int c = 0;
        int d = 0;
        int f = 0;
    
        for ( a = 1; a < 10; a++)
        {
            for ( b = 1 ; b < 10; b++)
            {
                if (a != b)
                {
                    i++;
                    if (i == 0)
                    {
                        double_digit[i][0] = a;
                        double_digit[i][1] = b;
                    }
                    else
                    {
                        int new_array[i+1][2];
                        for (c = 0; c < i; c++)
                        {
                           new_array[c][0] = double_digit[c][0];
                           new_array[c][1] = double_digit[c][1];
                        }
                        int double_digit[i+1][2];
                        for (d = 0; d < i; d++)
                        {
                           double_digit[d][0] = new_array[d][0];
                           double_digit[d][1] = new_array[d][1];
                        }
                        double_digit[i][0] = a;
                        double_digit[i][1] = b;
    
    //                    printf("%d ",double_digit[i][0]);
    //                    printf("%d ",double_digit[i][1]);
    //                    printf("%d ",a);
    //                    printf("%d \n",b);
                    }
                }
            }
        }
    
    
        for (f = 0; f < i+1; f++)
        {
            printf("%d %d \n",double_digit[f][0],double_digit[f][1]);
        }
    
        return 0;
    }
    Last edited by vxs8122; 02-14-2013 at 01:45 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Because arrays double_digit (line 4) and double_digit (line 34) are different.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Because arrays double_digit (line 4) and double_digit (line 34) are different.
    You are correct, however, the old values are stored in new_array (see lines 28-33) then later stored in the new double_digit array.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    And yet, it doesn't work.

    Sure it's a new fresh array called double_digit, but it's life begins on line 34, but is tragically cut short on line 47 when the block it was declared in goes out of scope.

    By the time you try to print out things on line 53, all your shenanigans has been lost.

    You can't resize an array by redeclaring it.
    So just declare on line 4 how big you want it to be to begin with.
    int double_digit[10][10];
    seems like a good place to start.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Thanks, I understand now. So there is absolutely no way to resize the double_digit array while keeping all the data?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,069
    Quote Originally Posted by vxs8122 View Post
    So there is absolutely no way to resize the double_digit array while keeping all the data?
    If you want to resize an array, the normal way is to use dynamic memory. This adds complexity. Here is an example:

    Code:
    #define ROWS 2
    int **make_array2d(int cols) {
        int **arr = realloc(NULL, sizeof(int) * ROWS);
        for (int i=0; i < ROWS; i++) {
            arr[i] = realloc(NULL, sizeof(int) * cols);
        }
        return arr;
    }
    
    int **grow_array2d(int **arr, int cols) {
        arr = realloc(arr, sizeof(int) * ROWS);
        for (int i=0; i < ROWS; i++) {
            arr[i] = realloc(arr[i], sizeof(int) * cols);
        }
        return arr;
    }
    
    int main(void)
    {    
        // make array 2x2
        int **double_digit = make_array2d(2);
        
        double_digit[0][0] = 11;
        double_digit[0][1] = 12;
        
        // resize array to 2x4
        double_digit = grow_array2d(double_digit,4);
        double_digit[0][2] = 13;
        double_digit[0][3] = 14;
        double_digit[1][0] = 21;
        double_digit[1][1] = 22;
        double_digit[1][2] = 23;
        double_digit[1][3] = 24;
    
        // print result
        for (int i=0; i < 2; i++) {
            for (int j=0; j < 4; j++) {
                printf("%4d", double_digit[i][j]);
            }
            printf("\n");
        }
        
        return EXIT_SUCCESS;
    }
    Note there is no error checking, which normally should be used. And the number of rows is fixed. To make it unfixed you would need to have a way to track the old number of rows and the new number of rows, and grow_array2d should act accordingly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Replies: 8
    Last Post: 12-30-2010, 09:08 PM
  3. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  4. Replies: 12
    Last Post: 02-28-2008, 05:19 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM

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