problem with 'for' loop

This is a discussion on problem with 'for' loop within the C++ Programming forums, part of the General Programming Boards category; Here's the deal. I have to create a program that outputs a pic like the one below using a 'for' ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    problem with 'for' loop

    Here's the deal. I have to create a program that outputs a pic like the one below using a 'for' loop.

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    No input required for this program and in order to get the *'s up there all u can use is a cout statement involving "*". Then i have to produce the same pic again 3 times, but have it turned around in the 3 different directions it can be turned. I need some help with this and any u can give is appericiated. Thanks to all!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Hint: a for loop which prints one line in each pass and keeps track of how many *'s to print in each line. Inside the for loop, another for loop that prints each line. That's the first pic.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    I thought there might be a loop inside of a loop and I know i need a counter to keep track, but I don't know the coding on how to get the stars to add one on every print until it reaches 10

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    this is all i can come up with:

    for( int counter=1; counter <= 10; counter++ )
    cout << "*" << endl;

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    All 4 pics involve 2 nested for loops. The outer loop corresponds to going down, the inner loop corresponds to going across.

    Edit: Also note that in the inner loop, you only use cout << "*". You don't use cout << endl until after the inner loop (but still inside the outer one).
    Last edited by robatino; 10-12-2006 at 09:17 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    OK, i did something like that using a 'while'statement but it was a box that looked like this...

    Code:
    ************
    *                    *
    *                    *
    *                    *
    ************
    Last edited by Dave_Sinkula; 10-12-2006 at 10:12 PM. Reason: Added [code][/code] tags to preserve whitespace.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    sorry the line in the middle is supposed to be on the very end

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Use [code][/code] tags when posting code or the pictures that you are trying to show so that they retain their formatting.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    thanks for the tip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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