c two dimensional array code

This is a discussion on c two dimensional array code within the C Programming forums, part of the General Programming Boards category; I tried to understand C array from various sources and then looked into the codes below but I cannot understand ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    20

    c two dimensional array code

    I tried to understand C array from various sources and then looked into the codes below but I cannot understand it. Can you please tell me what the code exactly does and what they meant to write?

    why they declared five variables matrix[3][3],i,j,r,c in the first place?

    http://s2.postimage.org/p9rznkq2g/Tw...onal_array.gif
    Last edited by learning_grc; 09-21-2011 at 04:09 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You made a screenshot of your code? How about actually just posting it here between code tags like a normal person?


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

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    Quote Originally Posted by quzah View Post
    You made a screenshot of your code? How about actually just posting it here between code tags like a normal person?


    Quzah.
    Thank you friend. But in the source it was image. Anyway, I typed now in notepad and made copy paste here
    Code:
    int main()
    {
    int matrix[3][3],i,j,r,c;
    clrscr ();
    printf ("Enter the order of matrix\n");
    scanf("%d%d", &r, &c);
    printf ("Enter elements of %d * %d matrix\n", r,c);
    for (i=0; i<r; i++)
    for (j=0; j<c; j++)
    scanf ("%d", &matrix[i][j]);
    printf ("Given matrix:\n");
    for (i=0; i<r; i++)
    {
    for (j=0; j<c; j++)
    printf ("%d\t", matrix[i][j]);
    printf ("\n");
    }
    printf ("%d\t", matrid [2][2]);
    getch ();
    return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They have declared five variables to:

    matrix[3][3] - declare a 3x3 array.
    i - loop through matrix[ HERE ][ ]
    j - loop through matrix[ ][ HERE ]
    r - stop counter i
    c - stop couner j

    The problem here is that you never make sure r and c are less than 3.


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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    You should really find a better source for learning C, than watching crappy animated GIF files generated by a TurboC programmer.
    why not 1,2,3,4,5 is output when I expected 1,2,3,4,5,6 for below codes


    How about http://www.eskimo.com/~scs/cclass/cclass.html
    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. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  2. Replies: 1
    Last Post: 04-25-2006, 01:14 AM
  3. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  4. 2-dimensional array
    By sjalesho in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 06:07 PM
  5. 2 Dimensional Array
    By Shazza in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 02:25 AM

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