Need help to understand Arrays

This is a discussion on Need help to understand Arrays within the C++ Programming forums, part of the General Programming Boards category; Alright, so I read through the Arrays tutorial on this website - Arrays in C and C++ - Cprogramming.com And ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Need help to understand Arrays

    Alright, so I read through the Arrays tutorial on this website -
    Arrays in C and C++ - Cprogramming.com

    And so I tried using the tutorial codes -

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
      int x;
      int y;
      int array[8][8]; // Declares an array like a chessboard
    
    
      for ( x = 0; x < 8; x++ ) {
        for ( y = 0; y < 8; y++ )
          array[x][y] = x * y; // Set each element to a value
      }
      cout<<"Array Indices:\n";
      for ( x = 0; x < 8;x++ ) {
        for ( y = 0; y < 8; y++ )
          cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
        cout<<"\n";
      }
      cin.get();
    }
    And when the program is launched, it prints all this-

    Name:  Arrays.JPG
Views: 122
Size:  49.5 KB

    So.., here is what I don't understand. In the left brackets of each column ( I assume it's where the x's are) shows 0 - 7. But in the right brackets of each column ( I assume it's where the y's are) show 0 in the first column and 1 on the second column, 2 on the third column and so on... That is what I don't understand.

    huh ?_? huh <- May someone please give me an easier explanation. Thanks.
    Attached Images Attached Images  

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    The left brackets hold the row, while the right brackets hold the column. Can't you see a pattern on how these values are printed?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Quote Originally Posted by GReaper View Post
    The left brackets hold the row, while the right brackets hold the column. Can't you see a pattern on how these values are printed?
    I still don't understand. And it's the numbers that has my fingers scratching my head.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by Conti View Post
    I still don't understand. And it's the numbers that has my fingers scratching my head.
    A 2D matrix has rows and columns. Think of it like a chess board for example:
    Code:
      A B C D E F G H
    1                 1
    2                 2
    3                 3
    4                 4
    5                 5
    6                 6
    7                 7
    8                 8
      A B C D E F G H 
    How do you determine, with words, where a pone is or what your next movement will be?
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Quote Originally Posted by GReaper View Post
    A 2D matrix has rows and columns. Think of it like a chess board for example:
    Code:
      A B C D E F G H
    1                 1
    2                 2
    3                 3
    4                 4
    5                 5
    6                 6
    7                 7
    8                 8
      A B C D E F G H 
    How do you determine, with words, where a pone is or what your next movement will be?
    Ah. I understand now. +1 to you chap ^.^

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Another way of visualizing it (which scales beyond 2 dimensions) is that you have some small boxes with some value in them.
    Then you take those boxes and put them into one big box.
    And finally, you create several of those big boxes (all of which contains all those small boxes inside them).
    For every dimension you add, you repeat this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2011, 03:18 PM
  2. Help me to understand
    By rits in forum C Programming
    Replies: 5
    Last Post: 10-15-2009, 11:58 AM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Trying to understand Arrays
    By LaGood in forum C Programming
    Replies: 5
    Last Post: 08-16-2007, 10:07 PM
  5. Replies: 2
    Last Post: 02-23-2004, 05:34 AM

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