Generating Triangles

Page 1 of 1

3 Replies - 1774 Views - Last Post: 28 April 2008 - 01:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50321&amp;s=e7ee6197833809a616580f8b3f3284bc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 iamosi

Reputation: 0
• Posts: 6
• Joined: 11-April 08

Generating Triangles

Posted 27 April 2008 - 07:40 PM

I need to make 4 triangles. HELP!!!! How do i space the last two triangles?

*
**
***
****
*****
******
*******
********
*********
**********

**********
*********
********
*******
******
*****
****
***
**
*

*
**
***
****
*****
******
*******
********
*********
********** (this is supposed to be a right triangle _|)

And the last one is the same way except the right angle is this way _
| )

```#include <stdio.h>
#define SIZE 10

int main( void )
{

int n[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[SIZE] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;
int j;

printf( "\n(A)\n" );

for ( i=0; i < SIZE; i++ ){

for ( j = 1; j <=n[ i ]; j++ ){
printf( "%c", '*' );
}

printf( "\n" );
}

printf( "\n(B)\n" );

for ( i=0; i < SIZE; i++ ){

for ( j = 1; j <=b[ i ]; j++ ){
printf("%c", '*' );
}

printf( "\n" );
}
return 0;
}

```

*edit: Please use code tags in the future, thanks!

This post has been edited by Martyr2: 27 April 2008 - 07:49 PM

Is This A Good Question/Topic? 0

Replies To: Generating Triangles

#2 Martyr2

• Programming Theoretician

Reputation: 5061
• Posts: 13,655
• Joined: 18-April 07

Re: Generating Triangles

Posted 27 April 2008 - 07:54 PM

You might want to check out my blog which covers shape building like this. It is using Java, but the syntax is very similar to C++ and you should be able to easily adapt it. The first example shows the standard facing left, the second shows the right triangle facing right.

Notice that I introduce a loop for handling the spaces. That will be the trick to help you out.

Enjoy!

Martyr2's Programming Underground - Building Shapes with Loops

#3 iamosi

Reputation: 0
• Posts: 6
• Joined: 11-April 08

Re: Generating Triangles

Posted 27 April 2008 - 08:19 PM

Martyr2, on 27 Apr, 2008 - 07:54 PM, said:

You might want to check out my blog which covers shape building like this. It is using Java, but the syntax is very similar to C++ and you should be able to easily adapt it. The first example shows the standard facing left, the second shows the right triangle facing right.

Notice that I introduce a loop for handling the spaces. That will be the trick to help you out.

Enjoy!

Martyr2's Programming Underground - Building Shapes with Loops

Thanks. I just looked at the tutorial. I'm now having trouble trying to translate it into C.

#4 gabehabe

• GabehabeSwamp

Reputation: 1430
• Posts: 11,006
• Joined: 06-February 08

Re: Generating Triangles

Posted 28 April 2008 - 01:24 PM

iamosi, on 27 Apr, 2008 - 08:19 PM, said:

Thanks. I just looked at the tutorial. I'm now having trouble trying to translate it into C.

Here it is, converted to C:
```#include <stdio.h>

int main()
{
// lets make rows = 5 for sake of argument
int rows = 5;

// Find out our starting space count
int spaces = rows - 1;

// Loop through the rows
for (int i = 0; i < rows; i++) {
// Here we have to first print the spaces before our asterisks
// So we loop through the spaces and print those.
for (int j = 0; j < spaces; j++) {
printf(" ");
}

// Then we loop through the remaining columns of the row and print asterisks.
// Again we find asterisks by taking our row and subtracting the number of spaces.
for (int j = 0; j < (rows - spaces); j++) {
printf("*");
}

// New row
printf("\n");

// Decrease the space count, resulting in another asterisk printing next time around.
spaces--;
}
}
```

Hope this helps