hi, im a beginner. i hav an assignment on Pascal's triangle. i saw some post over the topic but couldnt get it to work with my code. heres what i have done so far....

#include<stdio.h>
#include<conio.h>
void main()
{
	int a[10][10];
	int i,j,c,n;

	clrscr();

	printf("Enter how many lines do you want: ");
	scanf("%d",&n);
	
         a[1][1]=1;
	printf("                  %5d",a[1][1]);

	a[2][1]=1;a[2][2]=2;a[2][3]=1;

	printf("\n              %5d   %d   %d",a[2][1],a[2][2],a[2][3]);
	for(i=3;i<=n;i++)
	{

		a[i][1]=1;
		printf("\n             %5d",a[i][1]);

		j=2;c=3;
		while(j<=i)
		{
			a[i][j]=a[i-1][c-1]+a[i-1][c-2];
			printf("  %d ",a[i][j]);
			c=c+1;
			j=j+1;

		}
		a[i][j]=1;
		printf(" %d",a[i][j]);

	}
	getch();
}

and the output i get is :

[IMG]http://img407.imageshack.us/img407/562/untitledpl9.png[/IMG]
so you see, i cant align the rows properly after the third row. where do i need to put spaces? what wrong am i doing? can i get the desired output with this code or there is a problem with the logic itself. plz help:-|

printf("                  %5d",a[1][1]);

That's your first line, so that's the maximum number of spaces from the left of the screen. This won't work, because if you get more then 3 lines there won't be any room left on the left of the screen to print numbers.

You will have to think of a way to link the number of rows to the number of spaces. So if you have 2 rows, the number of spaces should be 2 for the first line. For 3 lines it should be 3. Etc.

And: Don't use void main() , clsrc() and getch() . use int main & getchar(); instead. Remove #include <conio.h> Regards Niek

I have done a similiar program. Hope it could help you...

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int i, j,n,*p,check=1, blank, memspace, mem=0, rowdiff=1;
printf("Enter the no. of rows to be printed :");
scanf("%d",&n);
blank=n-1;
memspace=((n*(n+1))/2);
p=(int*)malloc(sizeof(int)* memspace);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<blank;j++)
printf(" ");
for(j=0;j<check;j++)
{
if(j==0 || j==(check-1))
p[mem]=1;
else
p[mem]=p[mem-rowdiff] + p[mem-rowdiff+1];
printf("%d ",p[mem]);
mem++;
}
for(j=0;j<blank;j++)
printf(" ");
blank--;
check++;
rowdiff++;
}
getch();
}

Not quite there yet. Try 11 rows. The formatting will be all messed up because your program doesn't take into account that numbers might have more then 1 digit, so the more rows you enter, the more the spacing will be messed up.

Then:
Don't use void main() and getch(). use int main & getchar(); instead. Remove #include <conio.h>. This keeps the program portable.

And please learn how to use code-tags and indention, this makes your code a lot easier to read.

But apart from that, it's a nice piece of code.

Regards Niek

Bearing in mind that if this is a console application, there's a finite number of characters for any given line which you can output without the code spilling over to the next line, and completely ruining your formatting anyway.
Therefore, it may be simplest to put in an upper limit to the number of lines the program can produce, given the constraints of your environment.

The number of digits/characters that each number has will increase as you get further down the triangle, so you need to take that into account when working out the spacing between each character.

Bearing in mind that if this is a console application, there's a finite number of characters for any given line which you can output without the code spilling over to the next line, and completely ruining your formatting anyway.
Therefore, it may be simplest to put in an upper limit to the number of lines the program can produce, given the constraints of your environment.

...or better yet to redirect your output to a text file which can hold virtually infinite data, thereby removing the need for an upper limit.

hi, im a beginner.

Hi! :)

heres what i have done so far....

That's a good start, but you're missing one row that makes formatting an equilateral triangle easy.

and the output i get is :
[IMG]http://img407.imageshack.us/img407/562/untitledpl9.png[/IMG]

For a normal triangle you would probably do it one of these two ways:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define FILLER_LIMIT(height,row) ((height) - (row))
#define CONTENT_LIMIT(row) ((row) * 2 - 1)


/* Build a string with the specified number of characters */
const char *content_string( size_t n, const char c )
{
  char *str = calloc( n + 1, 1 );

  if ( !str )
    perror( NULL );

  return memset( str, c, n );
}


int main( void )
{
  int height, row, col;

  printf( "How many rows do you want? " );
  if ( scanf( "%d", &height ) != 1 ) {
    perror( NULL );
    exit( EXIT_FAILURE );
  }

  /* Print an equilateral triangle */
  for ( row = 1; row <= height; row++ ) {
    /* Print a decreasing amount of filler space */
    for ( col = 0; col < FILLER_LIMIT( height, row ); col++ )
      putchar( ' ' );
    /* Print an increasing amount of content */
    for ( col = 0; col < CONTENT_LIMIT( row ); col++ )
      putchar( '*' );
    /* Move to the next row */
    putchar( '\n' );
  }

  printf( "\n\n" );

  /* Print an equilateral triangle */
  for ( row = 1; row <= height; row++ ) {
    int n = CONTENT_LIMIT( row );
    const char *s = content_string( n, '*' );
    /* Print a decreasing amount of filler space */
    printf( "%*.s", FILLER_LIMIT( height, row ), " " );
    /* Print an increasing amount of content */
    printf( "%.*s\n", n, s );
    free( s );
  }

  return EXIT_SUCCESS;
}

I won't do it for you, but you can translate one of the tricks into printing your triangle. To make things easier on yourself, you should do the Pascal calculations first and then print the triangle. That way you can focus only on formatting and don't have to worry about getting the right values. That separation of duties is a key concept in refactoring and it helps keep you from getting smothered by problems. :)

@ Ravalon
Thank you!, I have been trying to do that shape for some time.
I'm going thrugh your code to understand it, and I got a question in this line:

const char *content string( size_t n, const char c)

what is size_t n? I recognized that it's some kind of variable type, but I don't see it declared anywhere else in you code. Could you give me some information about it?.

size_t is a macro which is declared in stddef.h while defined in the header file string.h It normally amounts to the same as unsigned int or unsigned long depending on the compiler implementation. Most of the functions of string.h require size_t hence it has been used, though you could have used even signed int and got away with it, pushing the responsibility of type conversion on our dear old compiler. :D

thanx evry1 for ur help and suggestions. i hav used somepart of all the suggestions. and i've solved it.

thnx again.

hi, im a beginner. i hav an assignment on Pascal's triangle. i saw some post over the topic but couldnt get it to work with my code. heres what i have done so far....

#include<stdio.h>
#include<conio.h>
void main()
{
	int a[10][10];
	int i,j,c,n;

	clrscr();

	printf("Enter how many lines do you want: ");
	scanf("%d",&n);
	
         a[1][1]=1;
	printf("                  %5d",a[1][1]);

	a[2][1]=1;a[2][2]=2;a[2][3]=1;

	printf("\n              %5d   %d   %d",a[2][1],a[2][2],a[2][3]);
	for(i=3;i<=n;i++)
	{

		a[i][1]=1;
		printf("\n             %5d",a[i][1]);

		j=2;c=3;
		while(j<=i)
		{
			a[i][j]=a[i-1][c-1]+a[i-1][c-2];
			printf("  %d ",a[i][j]);
			c=c+1;
			j=j+1;

		}
		a[i][j]=1;
		printf(" %d",a[i][j]);

	}
	getch();
}

and the output i get is :

[IMG]http://img407.imageshack.us/img407/562/untitledpl9.png[/IMG]
so you see, i cant align the rows properly after the third row. where do i need to put spaces? what wrong am i doing? can i get the desired output with this code or there is a problem with the logic itself. plz help:-|

I just adjust your program to get the proper formated output, u may try. Thank u.

hai i am senthil, i am doing my mca in EBETi.
very useful for your website

i want pascal triangle code i am a beginner doing my m.c.a

So, if you can read, what's the problem?

The answer is right in front of you. ;)

i want pascal triangle code i am a beginner doing my m.c.a

If I am not wrong MCA is a post graduate degree right ? If it is indeed a post graduate degree then its high time you started writing your own code

hey please some one help me to solve the problem of pascal triangle.
plz give me code of the given program
Q. Create a data structure to calculate Pascal's triangle of 13 rows
Display Pascal's triangle
Verify nCr values
Verify sum of the numbers in any row is 2n
Verify Hockey Stick Pattern

commented: Did you read any of the threads? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.