Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Code reading problem

  1. Aug 13, 2006 #1
    Hi!

    Code (Text):

    for (int i=0; i<=9; i++)
    {
       for (int j=0; j<i; j++)
           cout << " ";
       for (int j=i; j<=9; j++)
            cout << "*"
       cout << endl;
    }
     
    Can anyone explain me what these lines are doing?

    Thank you!
     
  2. jcsd
  3. Aug 13, 2006 #2
    what do you think the are doing?
    Do you know how to trace through code like that?
    ...write it all out on bpaper.

    If you don't know what a for loop does then i can't help ya.
    the "cout<<endl" is just a new line.

    EDIT: oh yeah you also hav ean error in your j-for loops
     
    Last edited: Aug 13, 2006
  4. Aug 13, 2006 #3

    -Job-

    User Avatar
    Science Advisor

    I don't know. It seems like it's printing 9 lines, each line having an increasing number of spaces and decreasing number of stars. But i can't figure out what something like that can possibly look like.
     
  5. Aug 13, 2006 #4
    It does this:
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    Press any key to continue
    When you don't try to redfine j in:
    for (int j=i; j<=9; j++).. (I used x)
    and correct the syntax error by adding a semi-colon to:
    cout << "*"
    Sorry for butting in...:blushing:
     
  6. Aug 13, 2006 #5

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    What it will look like depends on the font you use.
    But, most things like this are ment for a fixed spacing font.
    So take some graph paper and plot it out.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?