Thread: 2d array question

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    2d array question

    Code:
    main(void)
    {
    int i, j, k, m, n, p[2][3], q[2][3], r[2][3];
    for(i = 0; i < 2; ++i)
    for(j = 0; j < 3; ++j)
    {
    m = i + 3;
    n = j + 2;
    k = m%n;
    
    p[i][j] = i + j;
    q[i][j] = k;
    }
    for(i = 0; i < 2; ++i)
    for(j = 0; j < 3; ++j)
    printf("%i, %i\n", p[i][j], q[i][j]);}
    so i see when the loop starts i = 0 j = 0 making the output p=0 q = 1
    then when the loop is i=1 j = 0 i see the output being 1 and 0
    but the next line of output is p=2 q=3 when i = 1 and j = 1. p = i+j which is 2... okay. k is 4%3 which is 1? not 3?

    output:
    0,1
    1,0
    2,3
    1,0
    2,1
    3,0
    maybe im not getting this right..

  2. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Ashlee,

    The result of the statement: a % b is the 'remainder' term in the expression:

    Code:
    a mod b = r if and only if a = n*b + r for some integer numbers n and 0 <= r < b
    You may verify that 4 % 3 <==> 4 = 1*3 + 1, so you got that part right. According to the Wikipedia article, Modulo operation - Wikipedia, the free encyclopedia, "...some programming languages, such as C89, don't even define a result if either of n or a is negative." So be weary of negative operands....

    What do those array's look like after the first set of loops, Ashlee? I got:

    p = { {0, 1, 2} {1, 2, 3} }

    and then for q, I did some substitution:

    (i + 3) % (j + 2) is on the interval...
    <==> [ 3 % (0 + 2), 3 % (2 + 2) ] when i is zero, and
    <==> [ 4 % (0 + 2), 4 % (2 + 2) ] when i is one,
    thus:

    q = { {1, 0, 3}, {0, 1, 0} }

    So the second set of loops produce:

    Code:
    0,1 -
    1,0 | Section where 0 == i, and j is on [0,2]
    2,3 -
    1,0 -
    2,1 | Section where 1 == i, and j is on [0, 2]
    3,0 -
    Did I loose you? Using interval notation for correctness proofs can be tedious, but it can clarify a suspected bug rather quickly. I hope this helps.

    Best Regards,

    New Ink -- Henry
    Last edited by new_ink2001; 11-02-2010 at 05:24 PM. Reason: spacing
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate space for 2d array
    By jtay in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 10:34 PM
  2. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  3. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  4. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM