C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-06-2008, 12:11 PM   #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-

Quote:
**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.
matt_570 is offline   Reply With Quote
Old 10-06-2008, 12:22 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 10-06-2008, 12:44 PM   #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
+++
++
+
+
+
matt_570 is offline   Reply With Quote
Old 10-06-2008, 12:49 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Note that your inner loop is controlled by the same variable as the outer loop.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-06-2008, 12:51 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-06-2008, 01:23 PM   #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.
matt_570 is offline   Reply With Quote
Old 10-06-2008, 01:28 PM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Indent the code properly, e.g.,
Code:
for (b = 6; b > 5; b--)
{
    cout << "+\n";

    for (i = 0; i < 5; i = i + 1)
        cout << "+";
}
Quote:
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-06-2008, 01:30 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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
}
tabstop is offline   Reply With Quote
Old 10-07-2008, 06:02 PM   #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
matt_570 is offline   Reply With Quote
Old 10-07-2008, 06:06 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-07-2008, 06:16 PM   #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.
matt_570 is offline   Reply With Quote
Old 10-07-2008, 06:19 PM   #12
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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