Home

Example Application: Factorials, Permutations, Combinations

   

Introduction

In this exercise, we will calculate the factorial, the permutations, and the combinations.

In the studies of algebra and statistics, a factorial is a technique of finding different ways to arrange a series of objects (or values). For example, imagine you have five colors as red, green, blue, white, and black. In how many arrangements can you produce a list of those five objects? You can get the answer by calculating the factorial of the number of objects.

The formula to calculate the factorial is:

F = n!

Imagine you have a set of five objects in different colors as red, green, blue, white, and black. Imagine you want to arrange the objects in different sequences but you want each sequence to start with two specific objects, for example you may want to arrange the objects so that you always start with any combination of black and white followed by any combination of the other objects. This type of arrangement is called a permutation. The formula to calculate a permutation is:

Permutation

Imagine you have five objects and you want to arrange the objects in different sequences but you want each sequence to start with certain two objects. This type of arrangement is called a combination. To calculate it, you can use the following formula:

Combination

   

Practical LearningPractical Learning: Introducing Buttons

  1. Start Microsoft Visual Studio
  2. To start a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application and set the Name to Algebra1
  4. Click OK
  5. On the main menu, click Project -> Add Class...
  6. In the middle list of the Add Class dialog box, click C++ Class and click Add
  7. Set the Class Name to CAlgebra
     
    Algebra
  8. Click Finish
  9. Change the header file as follows:
    #pragma once
    public ref class CAlgebra
    {
    public:
        CAlgebra(void);
    
        long Factorial(long x);
        long Permutation(long n, long r);
        long Combinatorial(long a, long b);
    };
  10. Click the Algebra.cpp tab and change the source file as follows:
    #include "StdAfx.h"
    #include "Algebra.h"
    
    CAlgebra::CAlgebra(void)
    {
    }
    
    long CAlgebra::Factorial(long x)
    {
        if( x <= 1 )
    	return 1;
        else
            return x * Factorial(x - 1);
    }
    
    long CAlgebra::Permutation(long n, long r)
    {
        if( r == 0 )
            return 0;
        if( n == 0 )
            return 0;
        if( (r >= 0) && (r <= n) )
            return Factorial(n) / Factorial(n - r);
        else
            return 0;
    }
    
    long CAlgebra::Combinatorial(long a, long b)
    {
        if( a <= 1 )
            return 1;
    
        return Factorial(a) / (Factorial(b) * Factorial(a - b));
    }
  11. Display the form
  12. Click the body of the form to make sure it is selected.
    In the Containers section of the Toolbox, click TabControl and click the form
  13. On the form, right-click the right side of tabPage2 and click Add Page
  14. Design the form as follows:
     
    Factorial
    Control Text Name Additional Properties
    TabControl   tclAlgebra HotTrack: True
    TabPage Factorial tabFactorial  
    Label Number:    
    TextBox   txtFactorialNumber TextAlign: Right
    Button Calculate btnCalculateFactorial  
    Label Result:    
    TextBox   txtFactorial TextAlign: Right
    Permutation
    Control Text Name
    TabPage Permutation tabPermutation
    Label n:  
    TextBox   txtPermutationN
    Label r:  
    TextBox   txtPermutationR
    Button Calculate btnCalculatePermutation
    Label P(n, r):  
    TextBox   txtPermutation
    Combination
    Control Text Name
    TabPage Combination tabCombination
    Label n:  
    TextBox   txtCombinationN
    Label r:  
    TextBox   txtCombinationR
    Button Calculate btnCalculateCombination
    Label C(n, r):  
    TextBox   txtCombination
  15. Access the Factorial tab page and double-click its Calculate button
  16. Implement the event as follows:
    #pragma once
    #include "Algebra.h"
    
    namespace Algebra1 {
    
    . . . No Change
     
    System::Void btnCalculateFactorial_Click(System::Object^  sender,
    					 System::EventArgs^  e)
    {
        long result;
        long number = 0;
        CAlgebra ^ statistics = gcnew CAlgebra;
    
        try
        {
            number = long::Parse(txtFactorialNumber->Text);
            result = statistics->Factorial(number);
            txtFactorial->Text = result.ToString();
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Number");
        }
    }
    };
    }
  17. Return to the form
  18. Access the Permutation tab page and double-click its Calculate button
  19. Implement the event as follows:
    System::Void btnCalculatePermutation_Click(System::Object^  sender,
    					   System::EventArgs^  e)
    {
        long result;
        long n = 0, r = 0;
        CAlgebra ^ statistics = gcnew CAlgebra;
    
        try
        {
            n = long::Parse(txtPermutationN->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Number");
        }
    
        try
        {
            r = long::Parse(txtPermutationR->Text);
            result = statistics->Permutation(n, r);
            txtPermutation->Text = result.ToString();
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Number");
        }
    }
  20. Return to the form
  21. Access the Combination tab page and double-click its Calculate button
  22. Implement the event as follows:
    System::Void btnCalculateCombination_Click(System::Object^ sender,
    			                   System::EventArgs^  e)
    {
        long result;
        long n = 0, r = 0;
        CAlgebra ^ statistics = gcnew CAlgebra;
    
        try
        {
            n = long::Parse(txtCombinationN->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Number");
        }
    
        try
        {
            r = long::Parse(txtCombinationR->Text);
            result = Algebra.Combinatorial(n, r);
            txtCombination->Text = result.ToString();
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Number");
        }
    }
  23. Return to the form and double-click the Close button
  24. Implement the event as follows:
    System::Void btnClose_Click(System::Object^  sender,
    			    System::EventArgs^  e)
    {
        Close();
    }
  25. Execute the application to test the calculations
     
    Factorial
    Permutation
    Combinatorial
  26. Close the form and return to your programming environment
 
 
     
 

Home Copyright © 2011 FunctionX, Inc.