i need help programming pascal's triangle in C. i looked at the old threads, and none of them really helped. its a simple program, so no getch() or anything special. i started to create an array and give each element values. when i tried to run it, my output it funky looking. heres my code:

#include "stdio.h"

void main()
{
int x, i, j;
int C[10][10];

printf("Enter the number of levels: \n");
scanf("%d", &x);
for (i=0; i<x; i++){							
	for (j=0; j<=i; j++){
		if (j==0 || j ==1)								
			C[i][j] = 1;
		else
			C[i][j] = C[i-1][j-1] + C[i-1][j];			
	}
}
for (i=0; i<x; i++){
	printf("\n");
	for (j=0; j<x; j++){
		printf("%d ", C[i][j]);
	}
}
return;
}

also, is their a way to set the array as C[][]; or do i have to define its dimensions?

Yeah, and yet another post where the noob didn't read ANY of the threads / messages which describe how to post code tags.

#include "stdio.h"

void main()
{
int x, i, j;
int C[10][10];

printf("Enter the number of levels: \n");
scanf("%d", &x);
for (i=0; i<x; i++){	
for (j=0; j<=i; j++){
if (j==0 || j ==1)	
C[i][j] = 1;
else
C[i][j] = C[i-1][j-1] + C[i-1][j];	
}
}
for (i=0; i<x; i++){
printf("\n");
for (j=0; j<x; j++){
printf("%d ", C[i][j]);
}
}
return;
}

better?

better?

Not quite. Make use of the C in the (code=c). Example
Of course, that would not be worth a pinch of dirt if you did not care about proper indention before hand.
After that take a look at void main()
Also, unless you have copied the standard header file stdio.h to the current directory, substitute "stdio.h" for <stdio.h>

That's how much your effort of using tags buys from me, until you show some more.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Ahh, dude.. I wrote it in java for you:

http://schulte.homelinux.com/ftp/pascal.java

Nice avatar btw, aia..

Josh that doesn't help much in this forum.
And Pascal triangles are represented as an Equilateral or Isosceles where the angles are equal at the bottom, not as a right angle as your program displays.

By the way, if you want to get rid of the warning of your program.

javac -Xlint pascal.java
pascal.java:33: warning: [unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
                        rows.add(newRow);
                                ^ 
1 warning

After main declare ArrayList <int[]> rows;

nevermind i figured it out

Great, glad to hear it. At bottom of the page mark post as solved.

Josh that doesn't help much in this forum.
And Pascal triangles are represented as an Equilateral or Isosceles where the angles are equal at the bottom, not as a right angle as your program displays.

By the way, if you want to get rid of the warning of your program.

javac -Xlint pascal.java
pascal.java:33: warning: [unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
                        rows.add(newRow);
                                ^ 
1 warning

After main declare ArrayList <int[]> rows;

Yeah, well I just wanted to throw out an algo to give him a quick reference of a good way to go about programming it in C.. and I didn't want to deal with spacing the triangle out :P

nevermind i figured it out

What did you end up doing then? Convert that to C or did you just steal someone else's code? :P

This question has already been answered. Start a new discussion instead.