473,320 Members | 1,957 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,320 software developers and data experts.

ISOCELES TRIANGLE

I need to write a program, which takes two inputs:
•ï€*ï€*a value n that represents the number of elementsin 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?

Oct 20 '05 #1
9 3899
coinjo wrote:
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
Oct 20 '05 #2
coinjo wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 20 '05 #3
How to increase spaces each time when the loop runs?

Oct 20 '05 #4
> coinjo wrote:
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?

Karl Heinz Buchegger <kb******@gascad.at> wrote: 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.

--
Marcus Kwok
Oct 20 '05 #5
coinjo wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 20 '05 #6
coinjo wrote:
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

Oct 20 '05 #7
Marcus Kwok wrote:
coinjo wrote:
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?


Karl Heinz Buchegger <kb******@gascad.at> wrote:
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.


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?

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 20 '05 #8
>> > coinjo wrote:
>> 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?
Karl Heinz Buchegger <kb******@gascad.at> wrote:
> And btw. Why are there 4 lines in the triangle when the input was 7?
Marcus Kwok wrote:
The input tells you how many elements are in the longest row. The 4th
row,

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

has 7 '$' characters.

Karl Heinz Buchegger <kb******@gascad.at> wrote: 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.

--
Marcus Kwok
Oct 20 '05 #9
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);
}

Oct 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hi, Could anyone over here, write a program in C using only for loop to print the following output * *** ***** ******* ********* ***********
2
by: javadkhan | last post by:
Hi All, I am trying to supress the small black triangle that shows up in the menus meaning the menuitem is parent. The reason for that is I drew my own 3D looking triangle in DrawItem using...
1
by: Leo | last post by:
Can someone tell me how to make the sorting triangle on datagrid column header always show up? And which property can change the color of the triangle ? The default color is white. Thanks
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
0
geo039
by: geo039 | last post by:
I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 constructors...
19
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
2
by: nuimstudent | last post by:
Hey, I have to write a code which calculates pascals triangle using C. So Far I have the triangle with a right side allignment. As part of the assignment, we actually have to get the triangle to...
14
by: ginevralupin | last post by:
Puhleeeease check this ....... Isnt giving the correct output ... int a,b,c; printf("Enter the sides..."); scanf("%d %d %d",&a,&b,&c); if (a<(b+c)) printf("The triangle is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.