![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | |
| Registered User Join Date: Oct 2008
Posts: 25
| Printing "shapes" based on character lines and number of line inputs 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:
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 | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #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 << "+"; ++ so Code: for (i=0; i<5; i++) cout << "+" << endl; + + + + + therefore wouldnt Code: for (i=0; i<5; i++)
{
cout << + << endl;
for(i=0; i<5; i = i+2)
cout << +;
}
+++ ++ + + + |
| matt_570 is offline | |
| | #4 |
| C++ Witch 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 | |
| | #5 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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 | |
| | #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 << "+";
}
+ +++++ 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 | |
| | #7 | |
| C++ Witch 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:
__________________ 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 | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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 | |
| | #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;
}
|
| matt_570 is offline | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #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 | |
| | #12 |
| and the Hat of Guessing 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |