|
Example Application: Factorials, Permutations, Combinations |
|
|
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:
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:
Practical
Learning: Introducing Buttons
|
|
- Start Microsoft Visual Studio
- To start a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application and set the
Name to Algebra1
- Click OK
- On the main menu, click Project -> Add Class...
- In the middle list of the Add Class dialog box, click C++ Class
and click Add
- Set the Class Name to CAlgebra
- Click Finish
- 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);
};
- 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));
}
- Display the form
- 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
- On the form, right-click the right side of tabPage2 and click Add
Page
- Design the form as follows:
|
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 |
|
|
Control |
Text |
Name |
TabPage |
Permutation |
tabPermutation |
Label |
n: |
|
TextBox |
|
txtPermutationN |
Label |
r: |
|
TextBox |
|
txtPermutationR |
Button |
Calculate |
btnCalculatePermutation |
Label |
P(n, r): |
|
TextBox |
|
txtPermutation |
|
|
Control |
Text |
Name |
TabPage |
Combination |
tabCombination |
Label |
n: |
|
TextBox |
|
txtCombinationN |
Label |
r: |
|
TextBox |
|
txtCombinationR |
Button |
Calculate |
btnCalculateCombination |
Label |
C(n, r): |
|
TextBox |
|
txtCombination |
|
- Access the Factorial tab page and double-click its Calculate
button
- 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");
}
}
};
}
- Return to the form
- Access the Permutation tab page and double-click its Calculate
button
- 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");
}
}
- Return to the form
- Access the Combination tab page and double-click its Calculate
button
- 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");
}
}
- Return to the form and double-click the Close button
- Implement the event as follows:
System::Void btnClose_Click(System::Object^ sender,
System::EventArgs^ e)
{
Close();
}
- Execute the application to test the calculations
- Close the form and return to your programming environment
|
|