Skip to main content
deleted 17 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Pascal Triangle programmprogram in C. Is there any way that i can make it better?

So iI've created a programmprogram to calculate the PascalPascal's triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
}

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

So myMy idea was to work with 2 arrays at the same time, but still there is still a problem in my algorithm. How am I supposed to free the memory i engagedI allocated before? The first 8 bytes of begin are easy to free, but what should I do so I can free the memory I engagedallocated through the pointer spointers?

Pascal Triangle programm in C. Is there any way that i can make it better?

So i created a programm to calculate the Pascal triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
}

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

Pascal Triangle program in C

I've created a program to calculate Pascal's triangle:

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
}

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

My idea was to work with 2 arrays at the same time, but there is still a problem in my algorithm. How am I supposed to free the memory I allocated before? The first 8 bytes of begin are easy to free, but what should I do so I can free the memory I allocated through the pointers?

some more formatting
Source Link
ratchet freak
  • 13k
  • 22
  • 46

So i created a programm to calculate the Pascal triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
} 

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

So i created a programm to calculate the Pascal triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
int *begin;
begin=(int*)malloc(2*sizeof(int));
*begin=1;
*(begin+1)=1;
int* p,q,s,t;
int row=2,n=3,x;
p=begin;
q=begin+1;
for(n=3;n<=limit+1;n++)
{
    if(n==4)
    {
        free(begin);
    }
    x=1;
    printf("Row (%d) : ",row);
    for(int* i=p;i&lt;=q;i=i+1)
    {
        printf("%d ",*i);
    }
    putchar('\n');
    s=(int*)malloc(n*sizeof(int));
    t=s+n-1;
    *s=*p;
    *t=*q;
    for(int* g=p;g&lt;q;g=g+1)
    {
        *(s+x)=*g+*(g+1);
        x++;
    }
    p=s;
    q=t;
    row++;
}
}
int main(void)
{
int i;
printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
printf("Type your answer:\t");
scanf("%d",&i);
putchar('\n');
if(i<n)
printf("Nope, you can't do that\n");
else
   {
    printf("Row (1) : 1\n");
    pascal(i);
   }
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

So i created a programm to calculate the Pascal triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
} 

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

added 61 characters in body
Source Link
RookieCookie
  • 363
  • 2
  • 3
  • 9

So i created a programm to calculate the Pascal triangle! Here is my code :

 #include < stdio<stdio.h>
 #include < stdlib<stdlib.h>
 
void pascal(int limit)
 
{
 
int *begin;
 
begin=(int*)malloc(2*sizeof(int));
 
*begin=1;
 
*(begin+1)=1;
 
int* p,q,s,t;
 
int row=2,n=3,x;
 
p=begin;
 
q=begin+1;  

for(n=3;n<=limit+1;n++) 

{
 
    if(n==4)
{    {
        free(begin);
}

x=1;

    }
    x=1;
    printf("Row (%d) : ",row);
 
              for(int* i=p;i<=q;i=i+1i=p;i&lt;=q;i=i+1)
              {
              printf("%d ",*i);
              }

 
            putchar('\n');
 
    s=(int*)malloc(n*sizeof(int));
 
    t=s+n-1;
 
*s=*p;

*t=*q;
    *s=*p;
    *t=*q;
    for(int* g=p;g<q;g=g+1g=p;g&lt;q;g=g+1)
            {
            *(s+x)=*g+*(g+1);
              x++;
    }
    p=s;
    }

p=s;

q=t; 
    

 row++;
 
}
 
}
 
int main(void)
{   

int i;
 
printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
 
printf("Type your answer:\t");
 
scanf("%d",&i); 

putchar('\n');
 
if(i<1i<n)
printf("Nope, you can't do that\n");    

else
{   {
  
   printf("Row (1) : 1\n");   
    
 pascal(i);
   }
 
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

So i created a programm to calculate the Pascal triangle! Here is my code :

 #include < stdio.h>
 #include < stdlib.h>
 
void pascal(int limit)
 
{
 
int *begin;
 
begin=(int*)malloc(2*sizeof(int));
 
*begin=1;
 
*(begin+1)=1;
 
int* p,q,s,t;
 
int row=2,n=3,x;
 
p=begin;
 
q=begin+1;  

for(n=3;n<=limit+1;n++) 

{
 
if(n==4)
{ 
free(begin);
}

x=1;

            printf("Row (%d) : ",row);
 
              for(int* i=p;i<=q;i=i+1)
              {
              printf("%d ",*i);
              }

 
            putchar('\n');
 
s=(int*)malloc(n*sizeof(int));
 
t=s+n-1;
 
*s=*p;

*t=*q;
            for(int* g=p;g<q;g=g+1)
            {
            *(s+x)=*g+*(g+1);
              x++;
            }

p=s;

q=t;    

 row++;
 
}
 
}
 
int main(void)
{   

int i;
 
printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
 
printf("Type your answer:\t");
 
scanf("%d",&i); 

putchar('\n');
 
if(i<1)
printf("Nope, you can't do that\n");    

else
{     
 printf("Row (1) : 1\n");      
 pascal(i);
}
 
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

So i created a programm to calculate the Pascal triangle! Here is my code :

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
int *begin;
begin=(int*)malloc(2*sizeof(int));
*begin=1;
*(begin+1)=1;
int* p,q,s,t;
int row=2,n=3,x;
p=begin;
q=begin+1;
for(n=3;n<=limit+1;n++)
{
    if(n==4)
    {
        free(begin);
    }
    x=1;
    printf("Row (%d) : ",row);
    for(int* i=p;i&lt;=q;i=i+1)
    {
        printf("%d ",*i);
    }
    putchar('\n');
    s=(int*)malloc(n*sizeof(int));
    t=s+n-1;
    *s=*p;
    *t=*q;
    for(int* g=p;g&lt;q;g=g+1)
    {
        *(s+x)=*g+*(g+1);
        x++;
    }
    p=s;
    q=t; 
    row++;
}
}
int main(void)
{
int i;
printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
printf("Type your answer:\t");
scanf("%d",&i);
putchar('\n');
if(i<n)
printf("Nope, you can't do that\n");
else
   {
    printf("Row (1) : 1\n"); 
    pascal(i);
   }
}

So my idea was to work with 2 arrays at the same time but still there is problem in my algorithm. How am I supposed to free the memory i engaged before? The first 8 bytes of begin are easy to free but what should I do so I can free the memory I engaged through the pointer s?

added 61 characters in body
Source Link
RookieCookie
  • 363
  • 2
  • 3
  • 9
Loading
deleted 141 characters in body
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312
Loading
Source Link
RookieCookie
  • 363
  • 2
  • 3
  • 9
Loading