Thread: Problems Related to Binomial and Factorial Computations

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    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.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Guys the problem to the third question is solved. Thank you for your help .

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

    Adjust your project settings to create the right kind of project.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    First problem also solved! Thank you for your assistance!

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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;
    }
    Thank you for your reply and help.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where can i get c related problems
    By amrendra in forum C Programming
    Replies: 1
    Last Post: 04-05-2010, 04:50 AM
  2. Computations - which is faster?
    By ulillillia in forum C Programming
    Replies: 9
    Last Post: 12-09-2006, 10:23 PM
  3. anagrams problems with n factorial
    By treenef in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2005, 01:52 AM
  4. Template-Related Problems
    By Orborde in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2005, 02:44 PM
  5. A binomial queue (binomial tree) question.
    By NightWalker in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 08:25 AM

Tags for this Thread