There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop, unless your teacher is trying to make you use a math formula to calculate the number of '*' on each line. If I were you I'd just ask my teacher about this.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Hello..... I want to ask you something..... My teacher gave us our final exam last October 16 & 17.... I want to ask you about this question: Create a program that could show this output
*
***
****
******
If you'd try reading your post, you'd see that the question will never give you the proper answer. Your triangle does not give us the actual shape. Had you pressed PREVIEW you would have seen this. Wrap any lines you want to keep the proper formatting between CODE tags.for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}
Before posting code,format and test it first. You should never post working code as an answer to anyone's question (help them find the answer instead), and NEVER EVER post code that flat out doesn't work -- as your code doesn't. Twice.
There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop...
And what would you use? A switch statement? Don't justsay something controversial (wrong), back it up with a reason. I for one always thought a loop is the absolute best statement to use to do a repetitive operation.
-------------------
Loop #1: Loop for each line. Within this loop (nested is the term)
Loop #2: Loop to print the spaces before the *s (inner loop 1)
Loop #3: Loop to print the *s (inner loop 2)
Base both the inner loops on the number of spaces and *'s needed for the specific line. This can be determined by a simple equation if you study the shape.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it. The code to write the loops alone is probably longer than the code to just print the '*'s' out. And the code definitely makes more sense at first glance hard coded than if it is put in for loops. Also, you might want to consider that the sentence 'nested is the term' only makes sense to Yoda. Learning you are.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it.
You've obviously never passed a programming course.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I saw people asking this, in Yahoo Answers last week.
I like this funnier leaning tree version(I don't have a life to be writing this stuff):
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16 - i; j++)
{
for(int l = 0; l < 1 + j / 2; l++)
{
printf(" ");
}
}
for(int k = 0; k < i; k++)
{
printf("*");
}
printf("\n");
}
It's also possible to do an upside down version, with only two 'for' loops, and one 'if-else'.
It should be easy to turn that into a tree, too. A good practice-exercise.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
i made it upside :P that's not giving away the code.. right?
Debatable... :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
You've obviously never passed a programming course.
You've obviously never had friends.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
You've obviously never had friends.
Ahh, when someone points out why your advice is lacking, you go to personal attack. I've been soundly trounced! :icon_rolleyes:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944