Thread: Printing "shapes" based on character lines and number of line inputs

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

    Printing "shapes" based on character lines and number of line inputs

    Hey I enrolled in an online beginners C++ this semester. I think I made my first mistake by taking my first programming class online.
    Anyways we've had assignments in sequence and selection programming and I've found it pretty easy so far, but now were on Repetition and whew does it give me a headache.
    I was able to program the "guess a random number" program but now I have another one to do. I spent hours today working on the pseudo-code I thought I got part of the program right but it turned out to be wrong. Heres the assignments-

    **Note : This program should use a do-while loop and for loops.

    Write the program printShapes to repeatedly ask the user for a character and then print output according to the following table. Your program should continue until the user enters 'f'. Note that for each valid input (a through e), at least 2 MORE inputs are needed before the program can determine what to output. The "output" column in the following table gives an example of one set of input for each valid input choice.
    Input character Additional required input Output

    If: a

    1. character to print,
    2. number of lines to print,
    3. maximum number of characters per line

    Example: if character = '*', number of lines = 3, maximum number of characters per line = 4, output is

    ****
    ****
    ****

    If: b

    1. character to print,
    2. number of lines

    Example: if character = '+', number of lines = 3, output is

    +
    ++
    +++

    If: c

    1. character to print,
    2. number of lines

    Example: if character = '#, number of lines = 5, output is

    ##########
    ########
    ######
    ####
    ##

    If: d

    1. character to print,
    2. parallelogram height in lines

    Example: if character = '#, parallelogram height in lines = 5, output is

    ##
    ####
    ######
    ########
    ##########
    ########
    ######
    ####
    ##

    If: e

    1. character to print,
    2. maximum characters per lines

    Example: if character = '*', maximum characters per line = 5, output is

    *
    ***
    *****
    ***
    *


    Example for 'e' with even number:
    if character = 'E', maximum characters per line = 6, output is

    EE
    EEEE
    EEEEEE
    EEEE
    EE

    If: f none (no output, this signals that the user wished to exit)

    any character other than a..f none error message**
    I spent hours today working on the pseudo-code I thought I got part of the program right but it turned out to be wrong.
    I would post my code for "a" and "c" but it makes the post even longer and they turned out to be wrong. I cant for the life of me figure out how to increase the number of characters every line like in "b" "d" and "e".
    Any tips would be greatly appreciated
    And thanks in advance.
    Or Maybe I could get one of your guys aim or something so I can get direct feedback while I'm coding.
    I would ask my teacher but he takes like 48 hours to respond.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you ever noticed how, in a for loop like for (i=0; i < 5; i++), the value of i goes up by one each time through the loop? Do you know how that works? Do you think you could maybe figure out how to add 2 each time?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    wouldn't it be

    for (i=0; i < 5; i= i +2)

    I think the main part I'm having trouble with is the "endl;"

    Code:
    for(i=0; i<5; i = i+2)
    cout << "+";
    that code would yield
    ++

    so
    Code:
    for (i=0; i<5; i++)
    cout << "+" << endl;
    yield
    +
    +
    +
    +
    +

    therefore wouldnt
    Code:
    for (i=0; i<5; i++)
    {
      cout << + << endl;
        for(i=0; i<5; i = i+2)
         cout << +;
    }
    yield
    +++
    ++
    +
    +
    +

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Note that your inner loop is controlled by the same variable as the outer loop.
    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

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matt_570 View Post
    therefore wouldnt
    Code:
    for (i=0; i<5; i++)
    {
      cout << + << endl;
        for(i=0; i<5; i = i+2)
         cout << +;
    }
    yield
    +++
    ++
    +
    +
    +
    So read the code:
    i=0; print "+\n"
    Set i=0, print +, set i=2, print +, set i=4, print +, set i=6 stop
    Set i=7 (in main for loop), stop
    So you would get
    +
    +++and the next thing would get printed right here.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Ok I accidentally used "i" for both variables.

    Right now I tired to code a couple different solutions and non seemed to work. ( I tried to write algorithms yesterday and came across thinking they were wrong but I tried to code them right now anyways.


    Code:
       for (b = 6; b > 5; b--)
        {cout << "+\n";
      
          for(i=0; i<5; i = i+1)
         cout << "+";
         }
    yield:
    +
    +++++

    another variation was

    for (b = 0; b < 5; b++)
    {cout << "+\n";

    for(i=0; i<5; i = i+1)
    cout << "+";
    }
    yield:
    +
    +++++
    +++++
    +++++
    ++++

    I try to "read the code" but does the nested loop's cout end up on the first line of the main loop? Or a new line? My only resource is my text book and its really scarce is examples. I'm pretty sure I understand the loops, I just dont understand the placement of characters.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Indent the code properly, e.g.,
    Code:
    for (b = 6; b > 5; b--)
    {
        cout << "+\n";
    
        for (i = 0; i < 5; i = i + 1)
            cout << "+";
    }
    I try to "read the code" but does the nested loop's cout end up on the first line of the main loop? Or a new line?
    The inner loop does not print a new line, and a new line is not printed after the inner loop and before the end of the outer loop, therefore any characters printed could appear to "spill over" to the next line.
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matt_570 View Post
    Ok I accidentally used "i" for both variables.

    Right now I tired to code a couple different solutions and non seemed to work. ( I tried to write algorithms yesterday and came across thinking they were wrong but I tried to code them right now anyways.


    Code:
       for (b = 6; b > 5; b--)
        {cout << "+\n";
      
          for(i=0; i<5; i = i+1)
         cout << "+";
         }
    yield:
    +
    +++++

    another variation was

    for (b = 0; b < 5; b++)
    {cout << "+\n";

    for(i=0; i<5; i = i+1)
    cout << "+";
    }
    yield:
    +
    +++++
    +++++
    +++++
    ++++

    I try to "read the code" but does the nested loop's cout end up on the first line of the main loop? Or a new line? My only resource is my text book and its really scarce is examples. I'm pretty sure I understand the loops, I just dont understand the placement of characters.
    So to print a square you need to print five +, then an endl; five +, then an endl, and so on. You're print the endl in the middle of a line, but it needs to be at the end. So maybe
    Code:
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {
            cout << "+";  //five of these in a row is a line
        }
        cout << endl;  //now we can print a new line
    }

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks for the help, I was able to get shapes A-D, but now I'm having trouble on E.
    Heres the code so far-

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
     
      int numline;
      cin >> numline;
      int j;
      int i;
      int col;
      col = 1;
      int col2;
      numline = numline - 2;
      
      for (i = 0; i < numline; i++) {
        
          for (j = 0; j < col; j++) {
            cout << "+";  
          }    
        
        cout << endl;  
        col = col + 2;
    }
    
      col2 = numline;
    
      int k;
      int m;
     
    
      
      
      for (k = 0; k < numline; k++) {
        
          for (m = 0; m < col2; m++) {
            cout << "+";  
          }    
        
        cout << endl;  
        col2 = col2 - 2;
    }
    
    
    return 0;
    }
    I would really appreciate some tips like if I should use a for statement with an If-else, or should I use a do while statement

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matt_570 View Post
    Thanks for the help, I was able to get shapes A-D, but now I'm having trouble on E.
    Since D and E are exactly the same shape, that seems like a very ... interesting situation.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    That's what I thought at first, but D was basically B + C together meaning it always had (input * 2) - 1 for the number of lines. For example if the input was 5 the number of lines would be 9. If it was 6 number of lines would be 11. But for E number of lines needs to equal the input. And I cant just do numlines = (input/2) for a number of reasons.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you can, but as you noticed you'll have to make adjustments based on even and odd first.

    Edit: Oh, and notice that "number of lines equals the input" isn't true -- the example for input=6 has five lines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM