473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Program help

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
4 2085
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
2
by: stanlo | last post by:
Hallo to everyone, i am just begining to learn c++ even though i did pascal when i studied mathematics in the univesity.i just took on my self a project which writing a c++ program which does...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
1
by: Willing 2 Learn | last post by:
Below is a program I did to recognize a Finite State Automata for ASCII (J+H)*. I got that one working but im having trouble getting the NFA program to work. I really desperately need help! My...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
0
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.