Thread: Printing diagonally in an array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Printing diagonally in an array

    I'm trying to print diagonals in an array but I have no idea why my code isn't working. The upper and lower functions work but I have a hard time tinkering with the column elements. For example, in the upper function I have no idea why my code works without resetting the y value to 0 after every row. In fact, it doesn't work if I did reset.

    Code:
    #include <stdio.h>
    
    int matrix[5][5];
    
    int upper ()
    {
        int a, b, x = 0, y = 0;
    
        for (a = 0; a < 5; a++, x++, b++)
        {
            for (b = 0; b < 4; b++, y++)
            {
                matrix[x][y] = 1;
            }
        }
    
        return matrix[5][5];
    }
    
    int diagonal ()
    {
        int x, y;
    
        for (x = 0, y = 5; y > 0; x++, y--)
        {
            matrix[x][y] = 3;
        }
        return matrix[5][5];
    }
    
    int lower ()
    {
        int a, b, c, x = 0, y = 5;
    
        for (a = 0; a < 5; a++, x++)
        {
            for (b = 0, c = 1; b < c && c < 5; c++, y++)
            {
                matrix[x][y] = -1;
            }
        }
    
        return matrix[5][5];
    }
    
    int main (void)
    {
        int a = 0, b = 0, i, j, k;
    
        upper ();
        lower ();
        diagonal();
    
        for (i = 0; i < 5; i++, a++)
        {
            for (j = 0; j < 5; j++, b++)
            {
                printf("%d\t", matrix[a][b]);
            }
            printf("\n");
        }
    
        return 0;
    }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    From looking at your code, I think the general problem is that you make your loops unnecessarily complicated.

    For example:
    Code:
        for (i = 0; i < 5; i++, a++)
        {
            for (j = 0; j < 5; j++, b++)
            {
                printf("%d\t", matrix[a][b]);
            }
            printf("\n");
        }
    What is the point of the variables 'a' and 'b'? you could alternatively get rid of them and simply print out matrix[i][j] instead.

    I am curious how you managed to run your lower function without a problem. In your post you said it worked fine but when I look at this code:
    Code:
        int a, b, c, x = 0, y = 5;
    
        for (a = 0; a < 5; a++, x++)
        {
            for (b = 0, c = 1; b < c && c < 5; c++, y++)
            {
                matrix[x][y] = -1;
            }
        }
    I wonder how a 5 x 5 matrix (labelled 0 to 4) can have element ( 0 < x < 4, y > 4 ). Since you define y to equal 5 and then increase it!

    You could get rid of the a, b and c in this loop and just try something like this:
    Code:
    for(x = 0; x < 5; x++)
    {
          for(y = 0; y < x; y++)
          {
                matrix[x][y] = -1;
          }
    }
    For your diagonal, you are right that you only need one loop but again, you use too many variables to describe your problem.

    You could try something like this:
    Code:
    for(x = 0; x < 5; x++)
    {
          matrix[x][x] = 3;
    }
    P.S. The reason your code isn't working is because of this:
    Code:
        for (x = 0, y = 5; y > 0; x++, y--)
        {
            matrix[x][y] = 3;
        }
    You start y off at '5' then lower it but remember that your matrix elements are labelled 0 to 4. That was the problem I was wondering about with your lower function - you are accessing matrix elements which the variable 'matrix' does not have.
    Last edited by Swarvy; 11-10-2010 at 01:03 AM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    It works! Thanks for the help. I'm not surprised there are redundancies in my inefficient codes since I'm still fairly new to programming. I'm learning though, from my mistakes.

    For the lower function, I think I tinkered with it a bit but it used to work before.

    Your diagonal code wouldn't work because the diagonal is supposed to be in the form of a forward slash, so I think two variables are necessary.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > return matrix[5][5];
    This isn't doing anything for you, since your matrix is a global variable.

    What it is however is an out of bound memory access. The last position in the array is at [4][4]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing 2D Array
    By BB89 in forum C Programming
    Replies: 11
    Last Post: 11-16-2009, 05:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM