Thread: for statements

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    for statements

    I know I don't need this many for statement but I can't figure out how they should be combined.


    main()

    {

    cout<<"\n\n\n";
    cout<< "*";
    cout<<"\n";

    for (int s1=0; s1<2; ++s1)
    cout<<"*";
    cout<<"\n";

    for (int s2=0; s2<3; ++s2)
    cout<<"*";
    cout<<"\n";

    for (int s3=0; s3<4; ++s3)
    cout<<"*";
    cout<<"\n";

    for (int s4=0; s4<5; ++s4)
    cout<<"*";
    cout<<"\n";

    for (int s5=0; s5<6; ++s5)
    cout<<"*";
    cout<<"\n";

    return 0;

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You only need 2 loops. They should be nested 1 inside the other. 1 controls printing of '*' and the other controls printing of '\n'.
    See if you can work it out.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    Your code should be "s5++", not "++s5"

    Thought you should know.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Tommaso
    Your code should be "s5++", not "++s5"

    Thought you should know.
    why? it doesn't make a difference in this case. just a loop update, and you can increment either it way you want, whether it be post- or pre- increment.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    nested for loop...have the outer loop control the \n, and the inner control the *

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    Re: for statements

    Originally posted by Lyanette

    Code:
    main()
    
    {
    	
    	cout<<"\n\n\n";
    	cout<< "*";
    	cout<<"\n";
    	
    	for (int s1=0; s1<2; ++s1)
    		cout<<"*";
    	cout<<"\n";
    
    	for (int s2=0; s2<3; ++s2)
    		cout<<"*";
    	cout<<"\n";
    
    	for (int s3=0; s3<4; ++s3)
    		cout<<"*";
    	cout<<"\n";
    
    	for (int s4=0; s4<5; ++s4)
    		cout<<"*";
    	cout<<"\n";
    
    	for (int s5=0; s5<6; ++s5)
    		cout<<"*";
    	cout<<"\n";
    
    	return 0;
    here's an example of what you could do (comin' up with this off the top of my head)

    Code:
    int main()
    {
    	cout<<"\n\n\n";
    	for(int a=1;a<=6;a++)
    	{
    		for(int b=0;b<a;b++)
    			cout<< '*';
    		cout<< endl;
    	}
    	return 0;
    }
    that theoretically works (i'm at school right now, so i don't have a compiler handy)

    hope that helps, and i'm assuming this is for an assignment or something, because i had a problem somewhat like this when i was in programming...........

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    Angry but i can't figure out the mirror images

    1st what is a signature virus---there's one attached to one of the replies

    sometimes i think it's where i place my parenthese.

    should i use a space and a * loop. when i do that it prints continous stars

    {

    cout<<"\n\n\n";
    {
    for (int r=1; r<=11; ++r)

    {
    for (int c=1; c<=r; ++c)
    cout<<"*";
    cout<<"\n";
    }
    }

    cout <<"\n\n";
    { for (int r2=1; r2<=11; ++r2)

    {
    for (int c2=11;c2>=r2;--c2)
    cout<<"*";
    cout<<"\n";
    }
    }
    cout<<"\n\n";
    {for (int r3=1; r3<=11; ++r3)//loop for rows

    {for (int sp=1; sp<=1; ++sp)//loop for spaces
    cout <<" ";
    {for (int c3=10;c3>=r3;--c3)// loop for stars
    cout<<"*";
    cout<<"\n";
    }
    }
    }
    return 0;
    }
    Last edited by Lyanette; 03-29-2003 at 07:00 PM.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>1st what is a signature virus---there's one attached to one of the replies

    Its not a real virus. Not quite sure what the purpose of it is though.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    it's just kinda funny to see how many people will add it to theirs

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What would be a better invention, is a code tag virus. Lyanette, please make use of the tags, your code is too hard to read without them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed