Josh said:
Howdy
i am newb somewhat to programing and i was just for fun trying to
compile a program that asks the user for an odd int less than 22 and
then returns this
*****************
******* *********
****** ********
***** *******
etc. the first line reps. the number enterd by user
but i am having trouble i can get the first line but am having trouble
making the white space in the middle i am using a nested loop. Should
i use arrays? Any help would help thanks
Josh
Lets assume your user entered: 17 (because your example output
seems to be based on that number
*****************
******* *********
****** ********
***** *******
so lets analyze what your programs output looks like
The first line is simply a number of *, in this case 17
But what about the remaining lines?
Well. Looking at them you eventually will see a pattern
(programming is all about recognizing patterns)
There is a block, consisting of *. Then comes a block consisting of
spaces, followed by a block consisisting of *
Lets make a table (I start couting the lines with 0, since
C++ programmers start with 0 when couting
Line *_left spaces *_right
---------------------------------
1 7 1 7
2 6 3 6
3 5 5 5
Hmm. There should be some pattern in it. Eg. Lets look
at the number of spaces. Is there any relationship between
the line number and the number of spaces? You need to find
a formula, representing the table:
line spaces
1 1
2 3
3 5
(That is: Given a line number, compute the number of spaces)
Fiddeling around a little bit, you may come up with:
(line-1) * 2 + 1
Try it
line | line - 1 * 2 + 1
-----+-------------------------
1 | 0 0 1
2 | 1 2 3
3 | 2 4 1
The rightmost column, which represents the result of
(line-1)*2+1, is identical to the spaces column from abovem, so
it seems that this formula indeed calculates the required number
of spaces given a specific line.
So in a program ...
for( int line = 0; line < number_given_by_user; ++line ) {
// print a block of *
// code not implemented right now
// print a block of spaces
for( int j = 0; j < ( line - 1 ) * 2 + 1; ++j )
cout << ' ';
// printf a block of *
// code not implemented right now
cout << '\n';
}
.... the part that handles the spaces is already done. What about
the first block of *
Again: Lets look at the numbers
line *
1 7
2 6
3 5
Hmm. Again. The task is to find a formula that connects the line numbers
with the number of required *. Entering 1 into the formula should give
a result of 7, 2 -> 6, 3->5
Hmm. There doesn't seem to be an obvious relationship. The number of *
decreases when line goes up. But why did it start with 7?
Hey. That looks like the key. The number entered by the user was 17.
17 / 2 equals 8 (using integer arithmetic), thats 1 higher then the
required 7. / 2 because there are 2 blocks of * in each line, and the
one higher can easily be accounted by the line number (which was
1)
So the hypothesis is: The formula looks like this
( 17 / 2 ) - line
Lets try it
line 17 / 2 - line
---------------------------
1 8 7
2 8 6
3 8 5
Again. Comparing the rightmost column with the required numbers
it seems like they match. All we need to do right now, is to get
rid of the magical constant 17. But that's easy: That was the
number entered by the user.
So you finally get:
for( int line = 0; line < number_given_by_user; ++line ) {
// print a block of *
for( int j = 0; j < ( number_given_by_user / 2 ) - line; ++j )
cout << '*';
// print a block of spaces
for( int j = 0; j < ( line - 1 ) * 2 + 1; ++j )
cout << ' ';
// printf a block of *
for( int j = 0; j < ( number_given_by_user / 2 ) - line; ++j )
cout << '*';
cout << '\n';
}
And that should do it.
So you (or anybody else in this group) may ask: Why did he go
to that length in explaining how to come up with that program.
And hey, Karl solved a homework problem!
And my answer is: I wanted to show you, that programming problems
are *not* attacked by fireing up an editor and writing a program.
You always start with looking at the problem. You probably use
paper and pencil for that, look at the problem, search for relations,
try formulas, test hypothesis, until you are able to solve that
problem on paper!
Only then you start coding. And you definitly don't try around
until your program works somehow or use arrays just because you
don't know what else to do.