Generating Triangles

Generating Triangles with for loops

Page 1 of 1

3 Replies - 1421 Views - Last Post: 28 April 2008 - 01:24 PM Rate Topic: -----

#1 iamosi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :code:

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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • 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

:)
Was This Post Helpful? 0
  • +
  • -

#3 iamosi  Icon User is offline

  • New D.I.C Head

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

Re: Generating Triangles

Posted 27 April 2008 - 08:19 PM

View PostMartyr2, 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.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Generating Triangles

Posted 28 April 2008 - 01:24 PM

View Postiamosi, 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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1