need help!

This is a discussion on need help! within the C++ Programming forums, part of the General Programming Boards category; I have this program i need help with. i have to make a program that will display this. 000000* 00000*** ...

  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 09:48 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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,053
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Continued here Please more help!
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21