Thread: nested for loops/

  1. #1
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Thumbs down nested for loops/

    Code:
    #include<iostream>
    #include<conio.h>
    #include<iomanip>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;
    
    int main()
    {
        // declare & initialize variable/s
        
        int x,y,z,c;
        
        // print astericks
        
        for(x=1;x<=11;x++)
        {    
            cout<<'*';
            for(y=x-1;y<=9;y++)
            cout<<'*';cout<<endl;
        }
        
        cout<<"\n";
        
        for(x=0;x<=11;x++)
        {    
            cout<<'*';
            for(y-=x;y<=9;y++)
            cout<<'*';cout<<endl;
        }
        
        cout<<"\n";
        
        for(x=1;x<=11;x++)
        {    
            cout<<'*';
            for(y=1;y<=9;y++)
            cout<<'*';cout<<endl;cout<<setw(2);
        }
        
    getch();
    }
    The last for loop doesn't work if I have to print the following

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

    should it be

    ( print one whole row of * and print the next row after leaving first *)?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Something like this ?
    Code:
    for(int x = 9; x > 0; x--)
    {
        for(int y = 0; y < x; y++)
        {
    	cout << '*';
        }
        
        cout << endl;
    }
    Just a small remark:
    In your comments you say "declare & initialize variable/s", when no initialization is done.

  3. #3
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Lightbulb u r right/

    Thanku Calthun.I got it.And yes I didn't initialize the variables.Got to keep that in mind in future.

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, 04: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