ISOCELES TRIANGLE

C

coinjo

I need to write a program, which takes two inputs:
•a value n that represents the number of elements in the
longest row of a triangle.
•a character c to be printed in place of each element.
My algorithm should produce an isosceles triangle of characters c. For
example,
for n=7 and c=’$’, the resulting triangle should be as follows:


$
$$$
$$$$$
$$$$$$$

It should work for only odd values of n.
Any hints please?
 
J

John Harrison

coinjo said:
I need to write a program, which takes two inputs:
•a value n that represents the number of elements in the
longest row of a triangle.
•a character c to be printed in place of each element.
My algorithm should produce an isosceles triangle of characters c. For
example,
for n=7 and c=’$’, the resulting triangle should be as follows:


$
$$$
$$$$$
$$$$$$$

It should work for only odd values of n.
Any hints please?

Think about it like this. You are drawing a rectangle of characters,
only some of the characters are dollars and some are spaces.

A rectangle is easy, that's just one for loop inside another for loop.
Now all you have to so is work out the formula for whether you should
draw a space or a dollar. Put that formula in an if statement and you're
done.

If you still get stuck post the code you're stuck with.

john
 
K

Karl Heinz Buchegger

coinjo said:
I need to write a program, which takes two inputs:
â€¢ï€ ï€ a value n that represents the number of elements in the
longest row of a triangle.
â€¢ï€ ï€ a character c to be printed in place of each element.
My algorithm should produce an isosceles triangle of characters c. For
example,
for n=7 and c=’$’, the resulting triangle should be as follows:

$
$$$
$$$$$
$$$$$$$

It should work for only odd values of n.
Any hints please?

Study the output.
Are there any obvious relationsships?
Hmm. Each line starts with some blank characters followed by some '$' characters.
How many are there in each line? Lets make a table:

Line Nr | Nr of ' ' Nr of '$'
----------|---------------------------
$ 0 | 3 1
$$$ 1 | 2 3
$$$$$ 2 | 1 5
$$$$$$$ 3 | 0 7


Now, can you come up with a formula, that describes the realtionship
between the line number and the nr of spaces? Same for nr of '$'?
And btw. Why are there 4 lines in the triangle when the input was 7?
(Hint: in allmost all formulas the 'side length' or the 'number of lines'
will apear somehow. Now formula use more then elementary arithmetic).

When you have your formulas, try them on paper with some more triangles
and see if they are correct.

Once you have correct formulas, the rest should be easy:

get side length from user
is the side length odd
if no, tell the user and exit (or let him retry the input)

calculate how many lines are needed

for( i = 0; i < NrOfLines; ++i ) {
Calculate how many ' ' are needed
Output that many ' ' using a loop

Calculate how many '$' are needed
Output that many '$' using a loop

Output '\n'
}


That is what programming is all about: looking closely at the world and
figuring all relationships no one else then a programmer would see. Then
take what you noticed and write code for it. But first of all you need
to have those relationships and have converted them to a cook book recipe.
If you don't have them, then you can't write a program.
 
M

Marcus Kwok

Karl Heinz Buchegger said:
And btw. Why are there 4 lines in the triangle when the input was 7?

The input tells you how many elements are in the longest row. The 4th
row,

$
$$$
$$$$$
$$$$$$$ <-- 4th row

has 7 '$' characters.
 
K

Karl Heinz Buchegger

coinjo said:
How to increase spaces each time when the loop runs?

Where and why do you need that in this particular assignment?
My posting clearly showed:

Calculate how many ' ' are needed
Output that many ' ' using a loop

What's unclear about: Calculate how many ' ' are needed (in this particular line of output)?
What's unclear about: Use a loop to output that many ' ' ?

Other then that:
a variable is increased by adding something to it. One can use
i = i + 2; // increase by 2
or
i++; // increase by 1
or
i = i + n; // increase by anthing you want as long as the result is not bigger
// then what i can hold
for that.
 
M

mlimber

coinjo said:
How to increase spaces each time when the loop runs?

Put the printing of spaces in a loop or use the width method of cout
(or the corresponding IO manipulator; see
http://www.cppreference.com/cppio/width.html for an example).

BTW, this newsgroup is really for discussions of the C++ language
itself. Thus your posts about help for your homework problems are
really off-topic here and would probably be better served in
comp.programming or alt.comp.lang.learn.c-c++. May I also recommend an
excellent starter book for you to take up and read: _Accelerated C++_
by Koenig and Moo.

Cheers! --M
 
K

Karl Heinz Buchegger

Marcus said:
The input tells you how many elements are in the longest row. The 4th
row,

$
$$$
$$$$$
$$$$$$$ <-- 4th row

has 7 '$' characters.

The question was intended for the OP. He should think about
a relationsship of the numbers 7 and 4 in this particular case.

Or did you forget a smily?
 
M

Marcus Kwok

Karl Heinz Buchegger said:
The question was intended for the OP. He should think about
a relationsship of the numbers 7 and 4 in this particular case.

Or did you forget a smily?

:)

Sorry, I honestly thought you misunderstood the input, instead of trying
to ask the OP something to get him to think about the problem.
 
S

shri314

How about a recursive solution.

void Isoc(int n, char c, int p) {
if(n > 1) {
Isoc(n - 2, c, p + 1);
}

cout << (string(p, ' ') + string(n ,c)) << endl;
}

int main() {
Isoc(11, '$', 0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top