Need help on understandind arrays

This is a discussion on Need help on understandind arrays within the C++ Programming forums, part of the General Programming Boards category; Hey, I went over the tut on arrays, and I haft to say I don't get one bit of if. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    Need help on understandind arrays

    Hey, I went over the tut on arrays, and I haft to say I don't get one bit of if. How do I use it and why, what for? I'm really confused about this, I read the tut, but I really don't understand it.

    When I ran the program in the tut, well, lets just say I don't know where all this info is coming from or how to use it, can anyone help me please? I'm really wanting to learn C++ right about now.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well, post the code you don't understand and tell us what you understand from the code. We'll take you on from there. Don't forget to post the code between [.code][./code] (without dots)

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Alright, this is the code,

    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();
    }
    Thing I don't understand is where all the numbers come from when I run this program.

    And if you don't mind, could you give me a better understanding of arrays, cause I still don't quite get it.

    EDT: Oh wow, nevermind, I figured out what was wrong. I didnt' put the code in properly, heh.

    But hey, could you still give me a better understanding of this?
    Last edited by C++mastawannabe; 06-15-2007 at 10:00 PM. Reason: Found my problem :D

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    int array[8][8];
    Any array or 'board' that is 9 by 9. It's not 8 by 8 because the array starts at [0][0].

    Code:
      for ( x = 0; x < 8; x++ ) {
        for ( y = 0; y < 8; y++ )
          array[x][y] = x * y; // Set each element to a value
      }
    This is the method of giving each part of the array it's own value so if you built the array the order the values assign would look like this because for each time that x loops y will loop 8 times. Each [] representing an element in the array
    Code:
    [1] [10] [19] [] [] [] [] []
    [2] [11] [20] [] [] [] [] []
    [3] [12] [21] [] [] [] [] []
    [4] [13] []   [] [] [] [] []
    [5] [14] []   [] [] [] [] []
    [6] [15] []   [] [] [] [] []
    [7] [16] []   [] [] [] [] []
    [8] [17] []   [] [] [] [] []
    [9] [18] []   [] [] [] [] []
    It would go on to fill the rest of the elements but keep in mind those numbers aren't actually stored in the array but they are there to demonstrate the order in which the for loop gives each [] a value. Each array element is set to x*y and the x and y values depend on how far the for loops have gone through their cycle.

    Code:
      cout<<"Array Indices:\n";
      for ( x = 0; x < 8;x++ ) {
        for ( y = 0; y < 8; y++ )
          cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
        cout<<"\n";
      }
    This just goes back through and outputs the numbers previously stored in the array.
    Last edited by cerin; 06-16-2007 at 08:13 PM.
    My computer is awesome.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Any array or 'board' that is 9 by 9. It's not 8 by 8 because the array starts at [0][0].

    This is not correct. The array is 8x8 because when it is declared the values in the brackets indicate the size. Indexes 0-7 are valid for each dimension.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Arrays are just large clumps of variables and data.
    In large programs with many variables it can be extremely tiresome to declare 300 integers in this manner.
    int Num1, Num2, Num3.. etc..

    You could declare an array of 300 integers with much more ease.
    int Array[300]

    The program you demonstrated was a two-dimensional array.
    int Array[300][300]

    Arrays work by indexing the desired integer based on the number you put inside the brackets.
    Array[253] = 12
    That would assign the 254th element in the array to 12.

    For loops are used to iterate through each element assigning or reading the data from within the array.

    for(int i = 0; i <= 299; ++i)
    std::cout << Array[i];

    would display every element in my previous array.

    Arrays first element is also 0
    So Array[0] would be a correct operation.

    Code:
      
      for ( x = 0; x < 8; x++ ) {
        for ( y = 0; y < 8; y++ )
          array[x][y] = x * y; // Set each element to a value
      }
    Just assigns the board to values similiar to a times table.
    Last edited by JJFMJR; 06-16-2007 at 10:51 PM.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Sorry accidentally posted twice.
    Last edited by JJFMJR; 06-16-2007 at 10:12 PM. Reason: Double Post by Accident

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by JJFMJR View Post
    Arrays work by indexing the desired integer based on the number you put inside the brackets.
    Array[253] = 12
    That would assign the 253rd element in the array to 12.
    well... 254th.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Thanks for your insights guys, it's really helping me out. My only question left now that leaves me in confusion is.

    Whats the difference between:
    cout<< array[i];

    and

    std::cout<< array[i];

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Quote Originally Posted by C++mastawannabe View Post
    Thanks for your insights guys, it's really helping me out. My only question left now that leaves me in confusion is.

    Whats the difference between:
    cout<< array[i];

    and

    std::cout<< array[i];
    In your code this line.

    using namespace std;

    removes compiler errors with just typing cout << array[i];
    However without that line, you would have to access the cout object throught the std namespace using the :: operator.
    Thus std::cout << array[i]. Some people prefer to use the using keyword, others just prefer to type it out. I personally like to type it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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