Thread: for loop

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Unhappy for loop

    hello all,

    I'm university student and i have final exam in c++ on monday very soon
    so
    i need some help

    how to write a program in for loop the given output

    Code:
          
    
       *
      ***
     *****
    *******

    this is what i tried
    Quote:
    int n =5;

    for(int i=1; i<n+1 ;i++)
    {cout << endl;
    for(int j=1; j<i+1; j++)
    cout << "*";
    }
    and the output came out like this
    *
    **
    ***
    ****
    *****
    but now i dont know how to make the first stars be in the middle of the line
    what ways can i make it???
    do i need to use a third for loop hope not for loops make my head hurts


    thanks
    Last edited by digital artist; 10-10-2008 at 03:10 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're right, you need a for loop.
    One which counts from 1 to 5 seems like a good bet.

    Now, what have YOU tried to do, apart from just posting your assignment?
    See the forum rules if you're still unclear.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    this is not assignment this a Q came out in previous papers
    and im asking for my styding

    u might not believe i understand
    anyways

    this is what i tried
    int n =5;

    for(int i=1; i<n+1 ;i++)
    {cout << endl;
    for(int j=1; j<i+1; j++)
    cout << "*";
    }
    but i dont know how to make the first stars be in the middle of the line
    what ways can i make it???

    and thank you for the fast replys

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe investigate the relationship between the number of leading spaces compared to the number of stars on each line.

    > but i dont know how to make the first stars be in the middle of the line
    See, if this was your first post, you'd have gotten a lot more positive attention.
    It shows effort, and a specific question.

    As opposed to the zero effort "here's my homework, gimme an answer" which characterised your first missive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    thank u mr.salem
    i appreciate ur welling to help and ur advise

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I normally refrain from nit-picking.... well nit-picking grammar, anyway. Advice and advise are not the same word. At least you didn't spell lose wrong. That drives me crazy.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you're already nitpicking spelling or grammar, you may as well point out that netspeak is frowned upon on this forum.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was born with a rare brain disorder that doesn't allow the images of netspeak to even be fully processed by my brain. Thus I only see:

    thank mr.salem
    i appreciate welling to help and advise

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Quote Originally Posted by master5001 View Post
    I normally refrain from nit-picking.... well nit-picking grammar, anyway. Advice and advise are not the same word. At least you didn't spell lose wrong. That drives me crazy.
    thanks for the help
    and sorry for driving you crazy

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah but you didn't, friend. You have a loooooooooooooooong way to go before that You didn't spell lose as loose.

    Do you see the pattern between base and leading whitespace?

Popular pages Recent additions subscribe to a feed