Need a bit explanation!!

This is a discussion on Need a bit explanation!! within the C Programming forums, part of the General Programming Boards category; How does 2d matrix print to the output? i means if i got a[7][7] Will it on the first line ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Need a bit explanation!!

    How does 2d matrix print to the output? i means if i got a[7][7]
    Will it on the first line of the output from left to right or from top to bottom??
    For example
    Code:
    int sum;
    for (i=1; i <= 7; ++i) {
      for (j=1; j <= 7; ++j) 
      ......
     }
    i think output should be :
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7

    It prints left to right first or bottom all the 1 first then 2...thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Well it depends on if you are doing
    Code:
    matrix[i][j]
    or
    Code:
    matrix[j][i]
    Basically, it is your choice on how you represend the 2D array. There is no rule that i's represent rows, and j's represent columns

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > i means if i got a[7][7]
    Then the loop is
    for (i=0; i < 7; ++i)

    Not
    for (i=1; i <= 7; ++i)
    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.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    arrays start with 0.
    an array of size 7 would have 7 elements.
    thus there would be 7 valid indices: 0, 1, 2, 3, 4, 5, 6.
    the highest valid index is array_size - 1

    thus: yes, for (i=0; i < 7; ++i) is correct -
    also correct would be for (i=0; i <= 6; ++i)
    signature under construction

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    oo..ok..thanks for helping..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM

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