Nested for loops

This is a discussion on Nested for loops within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! Please don't bag me about this.... I've researched every textbook, visited at least 25 sites, looked at more ...

  1. #1
    Unregistered
    Guest

    Unhappy Nested for loops

    Hi everyone! Please don't bag me about this....

    I've researched every textbook, visited at least 25 sites, looked at more tutorials than I can remember and I still can't solve this problem.

    It's a easy problem as I'm only a newbie but please help me.... I've have been racking my brains for over a week now...

    Here it is.
    I'm trying to display this pattern with using just nested for loops but I don't know how...

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


    This is what I've got so far but that's it. I'm stuck!!!

    #include <iostream.h>

    main()
    {
    for (int a = 1; a <= 5; a++) //First loop. The condition is
    //true so it will continue to next loop
    {
    for (int b = 5; b >= a; b--) //Second loop. Also true until b is
    //zero
    {
    cout << "*";
    for (int c = b; c <= a; c++) //This is where I'm stuck!!!
    //HELP. HELP.
    {
    cout << " ";
    }
    }
    cout << endl;
    }
    }


    What should I do? Is the third loop required? Do I need another loop?

    Please if anyone could help me with this I will be eternally grateful

  2. #2
    Unregistered
    Guest
    The correct pattern I'm trying to get is

    *****
    _****
    __***
    ___**
    ____*

    The "_" are empty spaces...

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Code:
    #include <iostream.h>
    
    main ()
    {
    	int i, j;
    
    	for (i = 0; i < 5; i++)
    	{
    		// Spaces.
    		for (j = 0; j < i; j++)
    			cout << " ";
    
    		// Stars.
    		for (j = 0; j < (5 - i); j++)
    			cout << "*";
    
    		cout << endl;
    	}
    }
    MSVC++ 6.0

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > for (j = 0; j < (5 - i); j++)
    > cout << "*";

    Ewww....

    Do this instead...

    for (j = 5; j > i; j--)
    cout << "*";

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Whatever.
    MSVC++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 05:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM

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