Problems Related to Binomial and Factorial Computations

• 02-04-2012
abhishekcoder
Problems Related to Binomial and Factorial Computations
Dear all,

There are two problems that I have written a code for:

1. Program to determine whether a number is a factorial number or not.

A. I am getting the errors: (a) Linker error - undefined reference to 'WinMain@16' (b) Id return 1 exit status (c) Build Error. My code for this problem is:

Code:

```#include<stdio.h> int main() {      int i,n,prod=1;     printf("Enter n to be checked");     scanf("%d",&n);     if(n==1 || n==2)     {             printf("n is a factorial number");     }     else if(n>2)     {      for(i=1;i<=n/2;)     {     while (prod<=n)           {           prod*=i;           i++;           }     }     }     if(prod==n)                 {                 printf("factorial");                 }     else                 printf("not factorial");     system("pause");     return(0); }```

2. Program to calculate all the coefficients of x for a given value of n. Here nCr in the rth power of x in the expansion of (x+1)^n is given by

nCr = n! / r! (n-r)!

A. I am getting the errors: (a) Linker error - undefined reference to 'WinMain@16' (b) Id return 1 exit status (c) Build Error. My code for this problem is:

Code:

```#include<stdio.h> int main() {     int n,r,i,num,*coefficients,*den1,*den2;     printf("enter the value of n");     scanf("the values are %d",&n);     //for calculating r!     for(r=0;r<=n;r++)     {     if(r==0)             {             den1[r]=1;             }            else for(i=1;i<=r;i++)                  {             den1[r]*=i;             }     }     //for calculating n-r!            for(r=0;r<=n-r;r++)     {     if(r==0)             {             den2[r]=1;             }     else for(i=0;i<=n-r;i++)             {              den2[r]*=i;             }     }     //for calculating n!     for(i=1;i<=n;i++)                     {                     num*=i;                     }     //for calculating coefficients          for(i=0;i<=n;i++)     {     coefficients[i]=(num/(den1[i]*den2[i]));     printf("the %dth coefficient is %d\n",i,coefficients[i]);     } system("pause"); return 0; }```

3. Generating the numbers of the Lucas sequence which is a variation of fibonacci sequence. The lucas sequence is:

1 3 4 7 11 18 29 ...

A. This code for this is successfully compiled but on running the program it hangs after entering the value of integer 'n'. My code for this problem is:

Code:

```#include<stdio.h> int main() {     int i,n,*a;     printf("number of terms of the lucas sequence to be calculated");     scanf("%d",&n);     for(i=3;i<=n;i++)     {                a[i-1]=3;           a[i-2]=1;           a[i]=a[i-1]+a[i-2];           printf("the lucas sequence to n terms is 1 3 %d",a[i]);     }              system("pause");     return```
Thank you for you help.
• 02-04-2012
abhishekcoder
Guys the problem to the third question is solved. Thank you for your help :).
• 02-04-2012
Salem
> A. I am getting the errors: (a) Linker error - undefined reference to 'WinMain@16' (b) Id return 1 exit status (c) Build Error. My code for this problem is:
You're telling the linker that you're creating a GUI program.
But what you actually have is a console program.

• 02-08-2012
abhishekcoder
First problem also solved! Thank you for your assistance!
• 02-08-2012
abhishekcoder
I have modified the code for problem 2 (binomial and factorial computations) but my code hangs after I input a number, kindly check the code and let me know:

Code:

```#include<stdio.h> int fact(int a); int main() {     int n,r,i,num,*coefficients,*den1,*den2;     printf("enter the value of n");     scanf("the values are %d",&n);     //for calculating r!     for(r=0;r<=n;r++)     {     if(r==0)             {             den1[r]=1;             }            else             {           den1=fact(r);             }     }     //for calculating n-r!            for(r=0;r<=n-r;r++)     {     if(r==n)             {             den2[r]=1;             }     else             {              den2[r]=fact(n-r);             }     }     //for calculating n!     for(i=1;i<=n;i++)                     {                     num*=i;                     }     //for calculating coefficients          for(i=0;i<=n;i++)     {     coefficients[i]=(num/(den1[i]*den2[i]));     printf("the %dth coefficient is %d\n",i,coefficients[i]);     } system("pause"); return 0; } int fact(int a) {         int i,factorial=1;         for (i=1;i<=a;i++)         {                             factorial*=i;         } return factorial; }```
• 02-08-2012
Salem
First, learn how to indent code.
\$ indent -kr -nut -ts2 -i2 bar.c
Code:

```#include<stdio.h> int fact(int a); int main() {   int n, r, i, num, *coefficients, *den1, *den2;   printf("enter the value of n");   scanf("the values are %d", &n);   //for calculating r!   for (r = 0; r <= n; r++) {     if (r == 0) {       den1[r] = 1;     } else {       den1 = fact(r);     }   }   //for calculating n-r!   for (r = 0; r <= n - r; r++) {     if (r == n) {       den2[r] = 1;     } else {       den2[r] = fact(n - r);     }   }   //for calculating n!   for (i = 1; i <= n; i++) {     num *= i;   }   //for calculating coefficients   for (i = 0; i <= n; i++) {     coefficients[i] = (num / (den1[i] * den2[i]));     printf("the %dth coefficient is %d\n", i, coefficients[i]);   }   system("pause");   return 0; } int fact(int a) {   int i, factorial = 1;   for (i = 1; i <= a; i++) {     factorial *= i;   }   return factorial; }```
Second, get a decent compiler, and use maximum warnings.
\$ gcc -W -Wall -Wextra bar.c
bar.c: In function ‘main’:
bar.c:13: warning: assignment makes pointer from integer without a cast
bar.c:33: warning: implicit declaration of function ‘system’
bar.c:19: warning: ‘den2’ may be used uninitialized in this function
bar.c:30: warning: ‘coefficients’ may be used uninitialized in this function

> den1[r] = 1;
Here you dereference a pointer, but you don't initialise it

> den1 = fact(r);
Here you assign your pointer - but not with any useful value.