Thread: need help!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    12

    need help!

    I have this program i need help with. i have to make a program that will display this.

    000000*
    00000***
    0000*****
    000*******
    00*********
    0***********
    *************

    //dont display the zeros.

    using only one cout<<"*"; one cout" "; and use no tabs. any help would be great!

    Im studying loops and i have

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int r,s,spl,space;
        spl = 7;
        
        for(r = 1;r <= 7;r++)
        {
              for(space = 2;space <= spl;space++)
              {
                    cout<<" ";
              }
              for(s = spl;s <= 7;s++)
              {
                    cout<<"*";
              }
              spl--;
              cout<<endl;
        }
        cin.ignore(2);
        return 0;
    }
    and the output is this:

    000000*
    00000**
    0000***
    000****
    00*****
    0******
    *******

    //agian for get the 0's
    Last edited by Chaosreborn; 09-26-2007 at 08:48 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What are your thoughts on how to do this? What are you studying right now? Chances are it will be a test of that.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    Im studying loops and i have

    #include <iostream>
    using namespace std;

    Code:
    int main()
    {
        int r,s,spl,space;
        spl = 7;
        
        for(r = 1;r <= 7;r++)
        {
              for(space = 2;space <= spl;space++)
              {
                    cout<<" ";
              }
              for(s = spl;s <= 7;s++)
              {
                    cout<<"*";
              }
              spl--;
              cout<<endl;
        }
        cin.ignore(2);
        return 0;
    }
    and the output is this:

    000000*
    00000**
    0000***
    000****
    00*****
    0******
    *******

    //agian for get the 0's

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i havent really looked at the code, but its usually best to draw out your algorithm on paper first.

    for the first line, you have 6 spaces then 1 *. second line you have 5 spaces then 3 *s. next line you have 4 spaces then 5 *s.

    you may notice that if there are n spaces on the first line, there are n-1 spaces on the next line. also, if there are x *'s on the first line, there are (x + 2) *'s on the next line.

    on your paper, write the algorithm thatll accomplish the first single line, which seems trivial. then modify it so that it prints the 2nd line properly, using variables rather than specific numbers (ie dont use 'print 5 spaces', etc). if it works for the second line, it should work for all subsequent lines.

    hope it helps

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    i keep trying to add 2 but then it just continues on.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Chaosreborn View Post
    i keep trying to add 2 but then it just continues on.
    What are you trying to add two to? you don't have a variable "x" as nadroj describes that keeps track of the previous number of stars.

    You can either add such a variable, or find an algorithm that does not depend on that. For example, you can figure out the number of stars as a function of the row.


    Also, use descriptive variable names. Especially if you are going to ask for help on an online forum. r,s, and spl are not good names for variables.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    ok im sorry but the variables are: r = rows, s = stars, and spl = stars per line.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed