Constructing pascal's triangle (acute angled) with C

132 Views Asked by At

I'm quite new to programming and just learned the basics of C.I was trying to build the pascal's triangle with C. But can't make it acute angled.

This is what I've done.

#include<stdio.h>
int main(void)
{
    int n,s=1;
    do
    {
        printf("give the value of n: \n");
        scanf("%d",&n);
    }
    while(n<0);
    for (int i=0; i<=n;i++)
    {
        printf("1  ");
        for (int j=1;j<=i;j++)
        {
            s=s*(i-j+1)/(j);
                printf("%d  ",s);
        }
        printf("\n");
    }   
}

this gives a right angled triangle, like

                                        1
                                        1 1
                                        1 2 1
                                        1 3 3 1 
                                        1 4 6 4 1

but how to make it an acute angled one? like

                                                     1
                                                    1 1
                                                   1 2 1 
                                                  1 3 3 1 

Thank you.

3

There are 3 best solutions below

0
Mark Adler On

printf() returns the number of characters printed. You could save all the lines as strings and then print them centered using the length of the last line. But what the heck, just compute the triangle twice. Then nothing advanced like malloc() would be needed.

Do it once just to get the length of the last line (you can use snprintf() into a zero-length buffer to get that length), and then compute it again to print out the lines centered. For each line you can first use snprintf() to get the length, and then printf() with a %*s in the format and n, "", in the arguments to precede it with n spaces. Ok, really three times, since you'll need to compute the length of each line again. Though you could save the lengths in a variable length array, which C permits.

0
Aconcagua On

The main problem that might arise to get a nice printout is that the size of the number strings varies. Note that e.g. first two-digit number already appears at 6th row, first three-digit number at 10th. You won't get a clean triangle if you don't take that into account:

          1
         1 1
       1 121 1
    1 1331 1331 1
1 14641 14641 14641 1

Just for illustration, of course the effect is not as drastic with real values and occurs later, still it exists.

So for a clean triangle you need to reserve boxes for the values of the triangle, blocks of output all of the same size where you centre the individual values within and pad the rest with spaces (visualised by periods below):

            ..1..
         ..1.. ..1..
      ..1.. .121. ..1..
   ..1.. .1331 .1331 ..1..
..1.. 14641 14641 14641 ..1..

Question now is how large these boxes need to be; luckily there's a closed formula for each position in the triangle, see wikipedia, and the largest value is always the one in the middle of the last row (or any one of the two middle values, if there's an even number of values), so for m lines to print you use m-1 for n and m/2 for k, thus:

unsigned int num = 1; den = 1;
for(unsigned int n = m - 1, k = 1, m2 = m / 2; k <= m2; ++k, --n)
{
    num *= n;
    den *= k;
}
unsigned int max = num/den;

Now you can count the number of digits by simply dividing by 10 until reaching zero, et voilà, this is your box size. Centering the number within is already answered here, so I won't cover it in this answer.

Then the rest is rather simple: You need indentation blocks all of the same number of spaces, the initial line is indented by m-1 of such blocks and each subsequent one uses one less until the last line finally is not indented any more at all (i.e. by zero blocks).

Size of the indentation blocks? I experimented a little and discovered that the triangle gets a cleaner look if you use an odd value box size (so if number of digits is even, increment block size by one), say this is b. Then indentation blocks should consist of (b+1)/2 spaces, assuming you separate the value boxes by one single space.

1
SpaciousCoder78 On

You can try my implementation of pascal's triangle or use it as a reference.

#include <stdio.h>

int main(){
     int rows,k=1,m,i,j;
     printf("Enter the number of rows: ");
     scanf("%d",&rows);
     for(i=0;i<rows;i++){
         for(m=1;m<=rows-i;m++){
             printf("  "); //added space here for formatting
         }
         for(j=0;j<=i;j++){
             if(j==0 || i==0){
                k=1;
             }
             else{
                k=k*(i-j+1)/j;
                printf("%4d",k);
         }
         printf("\n");
     }
}

You will get the output as this:

Enter the number of rows: 4
           1
         1   1
       1   2   1
    1    3   3   1

Hope this helps