Displaying calculation help ?

This is a discussion on Displaying calculation help ? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include "Boolean.h" #include "combinatorics.h" #include <limits.h> /* For all the functions below, return TRUE if the calculation ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Displaying calculation help ?

    Code:
    #include <stdio.h>
    #include "Boolean.h"
    #include "combinatorics.h"
    #include <limits.h>
    
    /* For all the functions below, return TRUE if the
       calculation is successful, FALSE if overflow occurs
       Return the calculated value in the pass by reference
       parameter provided
    */
    
    Boolean calcFactorial (int n, int* nfact)
    {
        *nfact = 1;
    
    	while(n > 0)
    	{
    	       *nfact = *nfact * n;
    		   n--;		   
    	}
    
    	return TRUE;
    
    }
    
    Boolean calcCNR( int n, int r, int* cnr )
    {
        #define min(n,r)  (((n) < (r)) ? (n) : (r));
    
        int multiplier = n;
        int divisor = 1;
        int k;
    
        k = min(r, n - r);
           
    	*cnr = 1;
        while (divisor <= k) 
    	{
    		if(INT_MAX / multiplier < *cnr) 
    		{ 
               return FALSE;
    		}
    		else
    		{
            *cnr = ((*cnr * multiplier) / divisor);
       		
            multiplier--;
            divisor++;
    		}
        }
    	return TRUE;
    }
    
    Boolean calcPNR( int cnr, int rfact, int* pnr )
    {	 
            if((INT_MAX / rfact) < cnr  &&  (INT_MAX / cnr) < rfact)
    		{
    			return FALSE;			
    		}
    		else
    		{
    			*pnr = cnr * rfact;
    		}
         return TRUE;
    }
    Test Program
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include "Boolean.h"
    #include "Combinatorics.h"
    
    
    
    	void displayResults( int n, int r,
                         Boolean cnrOK, Boolean pnrOK,
                         int cnr, int pnr )
    	{
        if ( cnrOK )
    	{
    		printf("C(%d,%d) = %d   ", n, r, cnr);
    	}
        else 
    	{
    		printf("C(%d,%d) = OVFL\n");
    	}
        if ( pnrOK )
    	{
            printf("P(%d,%d) = %d\n", n, r, pnr);
    	}
        else 
    	{
    		printf("P(%d,%d) = OVFL\n");
    	}
    	
    
    }
    
    void getData( int* n, int* r )
    {  
        scanf ("%d %d",n,r);
        assert ( *r >= 0 );
        assert ( *n >= *r );
      }
    
    void main()
    {
    	int n, r;
        int cnr = 0, pnr = 0;
        int rfact;
        Boolean cnrOK, pnrOK, factOK;
    
        printf("Enter two values > 0\n");
    	
    	getData(&n, &r);
    	calcCNR(n, r, &cnr);
    	if(cnrOK != TRUE)
    	{
    		pnrOK = FALSE;
    	}
    	else
    	{
    		calcFactorial(r, &rfact);
    	}
    	if(factOK != TRUE)
        {
    		pnrOK = FALSE;		
    	}
    	else
    	{
    		calcPNR(cnr, rfact, &pnr);
    	}
    	
        displayResults( n,r, cnrOK, pnrOK, cnr, pnr ); 
    }
    Suppose to look:
    C(18,13) = 8568 P(18,13) = OVFL
    C(40,10) = OVFL P(40,10) = OVFL
    But mine looks:
    C(18,13) = 8568 P(1245056,1243068) = OVFL
    C(40,10) = 76904685 P(1245056,1243068) = OVFL
    Can someone help me fix this please ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, main does NOT return void. It returns an int. Always. Read the FAQ for more, but change it in your code.

    Next off, your indentation is absolutely horrible, and I just don't feel like formatting the whole thing for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Code:
    #include <stdio.h>
    #include "Boolean.h"
    #include "combinatorics.h"
    #include <limits.h>
    
    /* For all the functions below, return TRUE if the
       calculation is successful, FALSE if overflow occurs
       Return the calculated value in the pass by reference
       parameter provided
    */
    
    Boolean calcFactorial (int n, int* nfact)
    {
        *nfact = 1;
    
         while(n > 0)
         {
             *nfact = *nfact * n;
              n--;		   
         }
         return TRUE;
    }
    
    Boolean calcCNR( int n, int r, int* cnr )
    {
        #define min(n,r)  (((n) < (r)) ? (n) : (r));
    
        int multiplier = n;
        int divisor = 1;
        int k;
    
        k = min(r, n - r);
           
        *cnr = 1;
        while (divisor <= k) 
        {
            if(INT_MAX / multiplier < *cnr) 
            { 
                return FALSE;
            }
            else
            {
               *cnr = ((*cnr * multiplier) / divisor);
                multiplier--;
                divisor++;
            }
         }
         return TRUE;
    }
    
    Boolean calcPNR( int cnr, int rfact, int* pnr )
    {
        if((INT_MAX / rfact) < cnr  &&  (INT_MAX / cnr) < rfact)
        {
            return FALSE;			
        }
        else
        {
            *pnr = cnr * rfact;
        }
        return TRUE;
    }
    Test Program
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include "Boolean.h"
    #include "Combinatorics.h"
    
    void displayResults( int n, int r, Boolean cnrOK, Boolean pnrOK, int cnr, int pnr )
    {
        if ( cnrOK )
        {
            printf("C(%d,%d) = %d   ", n, r, cnr);
        }
        else 
        {
            printf("C(%d,%d) = Overflow\n");
        }
        if ( pnrOK )
        {
            printf("P(%d,%d) = %d\n", n, r, pnr);
        }
        else 
        {
            printf("P(%d,%d) = Overflow\n");
        }
    }
    
    void getData( int* n, int* r )
    {
        scanf ("%d %d",n,r);
        assert ( *r >= 0 );
        assert ( *n >= *r );
    }
    
    int main()
    {
        int n, r;
        int cnr = 0, pnr = 0;
        int rfact;
        Boolean cnrOK, pnrOK, factOK;
    
        printf("Enter two values > 0\n");
    	
       getData(&n, &r);
       calcCNR(n, r, &cnr);
       if(cnrOK != TRUE)
       {
           pnrOK = FALSE;
       }
       else
       {
           calcFactorial(r, &rfact);
       }
       if(factOK != TRUE)
       {
           pnrOK = FALSE;		
       }
       else
       {
           calcPNR(cnr, rfact, &pnr);
       }	
    displayResults( n,r, cnrOK, pnrOK, cnr, pnr ); 
    }
    Suppose to look:

    C(18,13) = 8568 P(18,13) = OVFL
    C(40,10) = OVFL P(40,10) = OVFL

    But mine looks:

    C(18,13) = 8568 P(1245056,1243068) = OVFL
    C(40,10) = 76904685 P(1245056,1243068) = OVFL
    Sorry for the identation problem. My problem still stays. Help please.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    Code:
    int main()
    {
        int n, r;
        int cnr = 0, pnr = 0;
        int rfact;
        Boolean cnrOK, pnrOK, factOK;
    
        printf("Enter two values > 0\n");
    	
       getData(&n, &r);
      ? = calcCNR(n, r, &cnr);   if(cnrOK != TRUE)
       {
           pnrOK = FALSE;
       }
       else
       {
          ? = calcFactorial(r, &rfact);   }
       if(factOK != TRUE)
       {
           pnrOK = FALSE;		
       }
       else
       {
         ? = calcPNR(cnr, rfact, &pnr);
       }	
    displayResults( n,r, cnrOK, pnrOK, cnr, pnr ); 
    }
    look at your properly, u are returning the boolean varibale from all the function factorial . calcCNR and calcPNR but you are not storing the return value in the main function when it is called.


    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  3. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 02:37 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  5. Displaying Arrays
    By mr_spanky202 in forum C Programming
    Replies: 3
    Last Post: 04-07-2003, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21