English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hey, newbie programmer here. I've been trying to program something and I'm having a little trouble.

I needed to ask for user input for the amount of rows in the triangle. The number of rows can only be odd numbers and less than 10.
The output should look like

*

**

***

**

*

For a 5 row triangle. I got the input thing down I think but I can't find out how to put the * in that pattern. I'm trying to use a nested for loop, I'm I going at it with the wrong loop? The spaces need to be there too.

Here is my code, it's not done, I couldn't get it to work for 3 so I stopped.

printf("How many rows do you want?\n");
scanf("%i", &c);

switch(c){
case 0:
printf("You can't have 0 rows\n");
break;
case 1:
printf("*")
break;
case 2:
printf("Your number must be an odd number\n");
break;
case 3:
for(counter=1; counter<=c; counter++){
for(a=1; a<=counter; a++){
printf("*");}
printf("\n\n");
break;

I'm sure I am just stupid but I can't figure how to make the triangle decrease in *.

2007-02-19 09:53:07 · 4 answers · asked by Gearz 2 in Computers & Internet Programming & Design

4 answers

Looking at your code, it will not handle anything over 3 rows.

My advice would be to capture the users input. Ensure that it is not 0., if it is, exit the routine.

Then use a modulus operator with the value of 2 to ensure an odd number. If it is an even number, exit the routine.

Find the midway point. Add 1 to c and then divide by 2. For instance if c is 5, add 1 to that to get 6 and divide by 2 to get a midway point of 3. Then using the for loop like you currently have set up (with the nested loop) loop until counter <= to the midway point. Once that loop structure is done executing have the program execute another loop sequence very similar to the first one (with the included nested loop) but have the loop set up as:
for (counter=midwaypoint-1;counter >0;counter--)

This way you will create all of the rows as required.

2007-02-19 10:10:17 · answer #1 · answered by Scottee25 4 · 0 0

Hi I wont get into the way you validate that the user has entered an odd number but I'll show you how to draw the decreasing side:

c=5; /* or any odd number 3, 5, 7, 9 etc */

/* start at 1 and draw the increasing side */
for (counter=1; counter<=c; counter++)
{
for (a=1; a<=counter; a++)
{
printf("*");
}

printf("\n\n");
}

/*
** counter should now be 1 over the rows eg counter = c+1
** take off 2 so that the largest row isnt drawn again
*/
counter = counter - 2;

/*
** now draw decreasing side but decrementing it in a loop
** until it hits 1
*/
for(; counter>=1; counter--)
{
/* This code is the same as above */
for(a=1; a<=counter; a++)
{
printf("*");
}

printf("\n\n");
}


Good luck.

2007-02-19 11:07:30 · answer #2 · answered by Clip-Man 3 · 0 0

Not stupid - just learning...

Have a try at this:
1) After the user enters a number, make sure it is valid. You say only odd numbers between 1 and 10. Note that odd numbers when divided by two will give a remainder. Thus, the MOD() function can help in determining if a number is odd or not. (and eliminate the need to a case statement)

2) Assuming the number is good, set up three FOR loops
a) the first is as you have it now using counter
b) the second goes from 1 to half of your number or (5/2)+1
Each time through, print an asterisk
c) the third goes from half the number to 1 (5/2)
Each time through, print an asterisk

(There's another way to do the above, but since you're learning, the above way is easier to understand for now.)

2007-02-19 10:06:00 · answer #3 · answered by BigRez 6 · 0 0

A couple of things ..

to check for validity of input

int c;
...
if( (c/2 * 2 ==c) || (c>9) ) .....error, ask for new input

to go backwards just run your loop backward

for(counter = c-1; counter>=1; counter--)

2007-02-19 10:11:06 · answer #4 · answered by Dr Ditto 2 · 0 0

fedest.com, questions and answers