By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

Program help

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Josh" <to*****@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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


This sounds like homework. In any case, you should try to describe the
problem a little better.

You ask the user for an odd int less than 22. OK, but what are you trying
to do with it? I don't see much correlation between the number of stars or
the number of spaces and the number "22". I'm guessing that you meant to
write 22 stars on the first line but instead wrote 17?

I'm assuming that your example output's width was intended to be 22, so your
program somehow displays an "asterisk" grid with width 22 and an empty
diamond in the middle. Is that right? In that case, I recommend that you
post your attempt at solving the problem so others do not feel like they are
giving you the answer to your homework. Currently, it sounds like a problem
that could be solved with two (relatively simple) for loops.

--
David Hilsee
Jul 22 '05 #2

P: n/a

"Josh" <to*****@yahoo.com> wrote in message
news:3b**************************@posting.google.c om...
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?
No
Any help would help thanks
Josh


You need several loops. At the highest level you have a loop, each time
round that loop you print an single line. Within that loop you have a
sequence of three loops. The first prints the first block of *, the second
prints the block of spaces, the third prints the second block of *.
Something like this

// print lines
for (...)
{
// print first *'s
for (...)
{
}
// print spaces
for (...)
{
}
// print second *'s
for (...)
{
}
}

john
Jul 22 '05 #3

P: n/a
Josh wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a
On 14 Sep 2004 20:50:01 -0700, to*****@yahoo.com (Josh) wrote:
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


As others have said you will generally get a better response to what
looks like a homework question if you post your own code. You were
very lucky to get Karl's reply - his advice is excellent.

You say that you can do the first line, but are having trouble with
the other lines. Think of the other lines in three parts: stars,
spaces, stars. You will need to work out how many of each you need on
each line, you have some pointers to this already. Hint the number of
spaces is given by the original number minus the total number of stars
on the line.

Since you can do the first line, you should be able to do the two
"stars" parts of the other lines, they are just shorter versions of
the first line. Indeed since you will be repeating basically the same
code you should probably put it into its own function (assuming you
have done functions that is). Something like: void stars(int num).
If you can do that, then you should be able to modify it to make a
second function that prints spaces: void spaces(int num). Test each
of these separately, and when they are both working correctly you can
use them in your final program.

rossum

--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.