-3
void Pascal(int n){
    int i,j;
    int a[100], b[100];
    a[0]= 1;

    for(i = 0; i <= n; i++){
        printf(" ");
        b[i]=1;
        for(j = 0; j <= i; j++){
            if (j <= 1) a[j-1]=0;
            b[j] = a[j-1] + a[j];
            printf("%d", b[j]);
        }
        for (j = 0; j <= i; j++){
            a[j] = b[j];
        }
        printf("%d \n");
    }
}    

This is the function I've been trying to build; please tell me what's wrong with it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Only way to track down this type of logic error is get out a `pencil` and pad of `paper` (you know, the way the did it in the **old** days). Start with `i=0` and go though the calculations. There are many ways to do pascals triangle, but I believe your `i` loop probably should start at `1` and the `j` loop should be indexed off `i` (e.g.: `for(j=i-1;j>0;--j)`). Just follow the calculations through. Your other option is to use a debugger. Compile with the `-g` option and fire up `gdb`. – David C. Rankin Nov 08 '14 at 09:15

4 Answers4

1

In line-11 you are using -1 as index. Check that first. Check the code—

void Pascal(int n){
    int i,j;
    int a[100]={0}, b[100]={0};
    a[1]= 1;
    for (i = 1; i <= n; i++){
        printf(" ");
        b[i]=1;
        for (j = 1; j <= i; j++){
            if (j <= 1) a[j-1]=0;
            b[j] = a[j-1] + a[j];
            printf("%d ", b[j]);
        }
        for (j = 1; j <= i; j++){
            a[j] = b[j];
        }
        printf("\n");
    }
}

I have started indexing from 1.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

I made a similar program:

#include <stdio.h>


int nk (int n, int k)
{
    int erg = 0;

    erg = fak(n)/(fak(k)*fak(n-k));
    return (erg);
}

int fak (int n)
{
    if (n==0)
        return 1;
    if (n==1)
        return 1;
    else
        return n = n*fak(n-1);

}

int main ()
{
    int n = 0;
    int k = 0;
    int s = 0; 
    int i = 0; 
    int a = 0; 
    printf ("How many rows: ");
    scanf ("%d", &a);
    s = (a-1);
    printf ("\nPascal'sches Dreieck\n");


    for (n = 0; n <= (a-1); n++)
    {

        for (i=0; i<=s; i++)
            printf("  ");

        for (k=0; k<=n; k++ )
        {
            if (nk(n,k)<=10)
                printf (" %d  ", nk(n,k));
            else
                printf ("  %d", nk(n,k));
        }
        s--;
        printf ("\n");

    }
        getch();
    return 0;
}
jollepe
  • 71
  • 13
0

please tell me what's wrong with it?

From my perspective, in the big picture, what's wrong with it is that you copy the contents of array b into array a on every iteration:

for (j = 0; j <= i; j++) {
    a[j] = b[j];
}

This is exactly the sort of needless memory shuffling that pointers allow us to avoid! So let's address that problem, and while we're at it, fix all the other little bugs in the code:

void pascal(int n) {
    int a[100], b[100]; // size appropriately or allocate dynamically

    int *a_ptr = a, *b_ptr = b;

    for (int i = 0; i < n; i++) {

        b_ptr[0] = b_ptr[i] = 1; // initialize both ends

        printf("%d ", b_ptr[0]); // print the left end

        for (int j = 1; j < i; j++) { // compute & print the middle
            b_ptr[j] = a_ptr[j] + a_ptr[j - 1];

            printf("%d ", b_ptr[j]);
        }

        if (i > 0) { // print the right end if it's not also the left end
            printf("%d", b_ptr[i]);
        }

        printf("\n");

        int *temporary = a_ptr; // swap a_ptr and b_ptr
        a_ptr = b_ptr;
        b_ptr = temporary;
    }
}
cdlane
  • 40,441
  • 5
  • 32
  • 81
0

Try this code !

I am also attach the screenshot of the output .

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
void Pascal(int n);
int main()
{
    int rows, coef = 1, space;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    Pascal(rows);

    return 0;

}
void Pascal(int rows)
{
    int coef = 1, space;
     for(int i=0; i<rows; i++)
    {
        for(space=1; space <= rows-i; space++)
            printf("  ");

        for(int j=0; j <= i; j++)
        {
            if (j==0 || i==0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;

            printf("%4d", coef);
        }
        printf("\n");
    }

}

enter image description here

Usman
  • 1,983
  • 15
  • 28