Help with array tutorials

This is a discussion on Help with array tutorials within the C++ Programming forums, part of the General Programming Boards category; Hello there ! I have been reading the tutorials for only the past 2 days whenever I had free time ...

  1. #1
    xD!
    xD! is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Question Help with array tutorials

    Hello there ! I have been reading the tutorials for only the past 2 days whenever I had free time and I really like them, I'm even ordering the first two books recommended.
    My programming skills are 0 but I really want to learn, I'm currently studying business in university and I believe knowing how to program will be very useful.

    Even though the tuts are pretty simply to follow I have some doubts about when some code is used and whats the difference (like structures and unions), but I really got annoyed with the array 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();
    }
    • First I don't get why the for loop is being declared twice.

    • Second doubt I have is why does the second bracket (y) loop differently to x? I know it has something to do with x for loop being defined with {} but I don't get the actual difference.


    Sorry to disturb with my noobness, its just if I don't get this simple concept I'm never going to move on !

    And if this isn't a help forum for newbies like me I would appreciate a link to a forum where I can get help!

    Thanks in advance!
    Last edited by xD!; 10-31-2010 at 12:25 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    > First I don't get why the for loop is being declared twice.
    No reason at all, except to illustrate the nature of loops and arrays.

    > Second doubt I have is why does the second bracket (y) loop differently to x?
    The y loops "get away" without braces because they only have a single statement.

    You could actually delete all the loop braces and it would still compile.
    It would do something different of course, but the exercise is worth doing non the less.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Welcome to the forums and to C++ programming!

    Quote Originally Posted by xD! View Post
    • First I don't get why the for loop is being declared twice.

    • Second doubt I have is why does the second bracket (y) loop differently to x? I know it has something to do with x for loop being defined with {} but I don't get the actual difference.
    I assume by "declared twice" (constructs such as for loops aren't "declared" by the way) you're referring to this nesting of loops?

    Quote Originally Posted by xD! View Post
    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 done because you're working with a two-dimensional array. This loop runs for pairs of (x, y) as follows: (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (1, 0), (1, 1), (1, 2), ...
    , (7, 7). The outer loop runs over every 'x', and the inner loop runs over every 'y'.

    Using curly braces in a loop encapsulates all of the code between them inside the loop. Consider the following loops:
    Code:
    for (i = 0; i < 4; ++i)
    	cout << "Hello!\n";
    	cout << "Goodbye.\n";
    
    for (i = 0; i < 4; ++i) {
    	cout << "Hello!\n";
    	cout << "Goodbye.\n";
    }
    In the first example, "Hello!" is printed four times, and "Goodbye." only once. This is because conditional constructs (if, else if, and else) and control flow constructs (for, while, and do...while loops) take either a single statement or a code block (a set of statements enclosed in curly braces) as their body (the code that is executed when the condition is true). Anything after that code is not part of the construct's body.

    In the example you give, curly braces aren't technically required in the first pair of loops; you can do away with them entirely if you want to. They are there to improve code readability. The code will work without braces around the outside loop's body because there is only one statement in the entire loop construct: 'array[x][y] = x * y; // Set each element to a value'. But because of the extra 'cout<<"\n";' statement, the braces are required in the second pair of loops.

    I hope I've explained this clearly enough; things like this become second nature eventually and it can be difficult to explain something you know "too well".
    Last edited by PehJota; 10-31-2010 at 01:07 PM.

  4. #4
    xD!
    xD! is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    @Salem: Ok I tried it with out the braces and it does work, that was pretty cool since I thought it would only follow statements within braces but now I know that it does follow for the next statement.
    @PehJota: That was very good example and also helped me understand better the loops!

    This "concept" actually helped me understand a bit whats going on "pseudocode-wise", from what I understood of the loop:

    -X starts at 0 and will loop until x < 8 increasing by 1 each loop.

    -Each loop of x contains a loop of Y which starts at 0 and will loop until y < 8 increasing by 1 each loop while maintaining x in current loop.

    -Then when the statements within for loop x are complete (the whole loop of y 1-8) it will continue to the next digit, which will cause y to loop again and so on.


    which is why it ends like (0 1) (0 2) etc.

    Ok that is one thing I didn't get (correct me if I'm wrong).

    That answered my second question.


    The first one was why is the two seperate for loops, i guess as PehJota says nesting. For example I tried writing:

    1.
    Code:
     
        #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x;
      int y;
      int array[8][8];
    
      cout<<"Array Indices:\n";
      for ( x = 0; x < 8;x++ ){
        for ( y = 0; y < 8; y++ )
        return array[x][y] = x * y;//I have even tried with out return and simply like
                                   //array[x][y] = x * y;
          cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
        cout<<"\n";
         
    }
    and 2.
    Code:
     #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x;
      int y;
      int array[8][8];
          array[x][y] = x * y;
      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();
    }
    This last one makes the program crash which made me laugh :P, but the first question mainly was why do we have to put twice the for x loop, for y loop ? (first right after setting the variables and the array and the after the cout)?

    Thanks alot for the previous answers and to the future ones !

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Quote Originally Posted by xD! View Post
    First I don't get why the for loop is being declared twice.
    A multidimensional array is really just a conceptual way of viewing an array. Your array[8][8] is really the same as array[64]. But since your using it as [8][8] the (2) for() loops are used to fill each seperate dimension. Add in a cout to view any random element in the array to see what I am saying, such as array[50].


    And if this isn't a help forum for newbies like me I would appreciate a link to a forum where I can get help!
    Sometimes the only help you may get is from other newbies!

    Beware the critics... They pounce.

  6. #6
    xD!
    xD! is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Incantrix View Post
    A multidimensional array is really just a conceptual way of viewing an array. Your array[8][8] is really the same as array[64]. But since your using it as [8][8] the (2) for() loops are used to fill each seperate dimension. Add in a cout to view any random element in the array to see what I am saying, such as array[50].




    Sometimes the only help you may get is from other newbies!

    Beware the critics... They pounce.
    Thx for the answer ! I thought that being multidimensional using for loop for x and then for y would be enough but the it declares it again so if I wrote array[8][8][8] would i have to do something like :

    for x
    for y
    for z
    then the array x*z*y

    and three times more ?

    for x
    for y
    for z

    for x
    for y
    for z

    then cout<< etc ?

    Hmm, i just feel theres something I'm not understanding exactly.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Tutorials are not exhaustive sources of information. They will not explain every possible use of arrays. As long as you understand the data structure and how to process an array, access individual elements, then you can literally master arrays.

    The tutorial does two things: fills a two-dimensional array, and then print the elements and their subscripts. You will not need to do that for every array. This use for arrays is very contrived.

    Code:
    int i, j;
    for( i = 0; i < 8; i++)
       for( j = 0; j < 8; j++)
           cout << "array[" << i << "][" << j << "] = " << i * j << endl;
    same output, no array.

    So the tutorial just wants you to understand arrays... and there are other sources, such as this one, that would explain in excruciating detail. If you want to know dirty details: read that.

  8. #8
    xD!
    xD! is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Wow ! Thanks alot for your answers they have helped me alot ! I'll be checkign that website you mentioned white flags, and I can't wait till the books I have ordered arrive.

    As for that link it seems to be C and not C++ maybe I would get mixed up due to different syntax or something like that.


    Either way thanks alot to those who have helped me !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 01:52 PM
  2. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

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