Thread: for loop confusion

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    for loop confusion

    Ok, I'm just learning C++ by use of a book called You Can Do It, A Beginners Intro to Comp Programming. I'm on Chapter 2, and have just learned the for-loop. At the end, there are these exercises to do. The last one requires you to display all 256 colors on an object call paper that is specific to the compiler that came with the book.

    Now, the book provided the following code for the exercise:

    Code:
    	for(int i(0); i != 16; ++i){
    		for(int c(0); c != 16; ++c){
    			
    		paper.plot(c - 8, i - 8, (i  * 16 + c));
    				
    				 
    			}	 
    		}
    It works, displaying all of the colors on the paper object. The confusion is how it works. Looking at it, I don't understand how this would ever plot all 256 colors in a perfect sqaure on the screen.

    Can somebody help me out? And feel free to ask questions if you don't understand what the code is about or what I'm asking or anything else.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You Can Do It, A Beginners Intro to Comp Programming
    Not a bad choice, Francis is fairly well respected in the C++ community. I personally would recommend Accelerated C++ instead, but some people would rather have graphics.

    >I don't understand how this would ever plot all 256 colors in a perfect sqaure on the screen.
    Let's take a look. There's nothing immediately obvious that shows how the colors will print, so you have to assume that it's magic by the plot member function.

    >paper.plot(c - 8, i - 8, (i * 16 + c));
    Page 20 tells you how the plot function works. Presumably m is equivalent to x and n is equivalent to y in the usual coordinates convention. So it makes sense to guess that the first argument (c - 8) handles the column and the second argument (i - 8) handles the row, and both are completely dependent on the value of the loop counter. In the end it's a math trick.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Is 2 loops c inside i, so 16*16=256

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Yes, but going through the loop step by step, I would think it would produce a diagonal line. Because each time, both m and n are increasing at the same time, and then the color is plotted.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Because each time, both m and n are increasing at the same time
    Not exactly. For each iteration of the outer loop, the inner loop runs completely:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      for ( int i = 0; i < 10; i++ ) {
        for ( int j = 0; j < 10; j++ )
          cout<<'('<< i <<','<< j <<") ";
        cout<<'\n';
      }
    }
    The effect of your program is identical except instead of text index pairs, you print colors.
    My best code is written with the delete key.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No, electron is right, 16 is squared so you get a square. Yay.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Enges
    Yes, but going through the loop step by step, I would think it would produce a diagonal line. Because each time, both m and n are increasing at the same time, and then the color is plotted.
    No, the two numbers aren't increasing at the same time The number for the outer-loop only increases when the inner-loop has completely finished (in other words, the inner loop has run 16 times).
    Once the outer-loop increments, the inner-loop runs fully all over again.. another 16 times.. then the outer-loop increments again.. etc. etc.


    EDIT : duh, that's just what prelude said.. ignore that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. a Bit of confusion (haha - Pun)
    By Junior89 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 11:22 PM
  3. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM