take a look at this code

This is a discussion on take a look at this code within the C++ Programming forums, part of the General Programming Boards category; the code is supposed to display the following 5 10 15 20 15 20 25 30 25 30 35 40 ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    take a look at this code

    the code is supposed to display the following
    5 10 15 20
    15 20 25 30
    25 30 35 40
    35 40 45 50

    Here is my code
    Code:
    #include <iostream.h>
    void main ()
    {
    int i,j;
    for (i=5; i<=35; i=i+10)
    {
    for (j=0;j<=4;j=j+5)
    cout <<(i+j);
    cout<<endl;
    }
    }
    it is supposed to add 5 and make 4 rows of columns. it looks good to me, but when i tried compiling, it didn't look right. Any help is appreciated.
    Last edited by brutal; 09-24-2002 at 11:11 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    it would be usefull to display what you actually got as the output, though from what I can gather it was doing this.

    5
    10
    15
    20
    25
    and so on

    Your problem lies in the J loop. THe cout << endl; should be after the J loop, not a part of it.

    Also check your definition of the J loop, that apears to be wrong.

    This should now work.
    Remember, computers alway do exactly what you tell them to do.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The main problem was that you were incrementing j by 5 but the loop was stopping at 4, therefore it only went round once. Your answer is:

    Code:
    	int i, j;
    	for( i = 5; i <= 35; i += 10 )
    	{
    		for( j = 0; j < 4; j++ )
    		{
    			cout << i + 5*j << " ";
    		}
    		cout << endl;
    	}
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by WebmasterMattD
    it would be usefull to display what you actually got as the output, though from what I can gather it was doing this.

    5
    10
    15
    20
    25
    and so on

    Your problem lies in the J loop. THe cout << endl; should be after the J loop, not a part of it.

    Also check your definition of the J loop, that apears to be wrong.

    This should now work.
    Remember, computers alway do exactly what you tell them to do.

    Later,
    The j << endl; is not part of the loop. It only appears to be becase of indention. Only the line immediatly following the for is part of the loop unless {} are used.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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