Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

Windows Controls Examples: Algebra

   

Introduction

In the exercises in the following sections, we will calculate the factorial, the permutations, and the combinations.

In the study of 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. In case, the arrangement of objects is important. For example, red followed by green is different from green followed by redf. This type of arrangement is called a permutation. The formula to calculate a permutation is:

Permutation

Imagine you have a certain number of objects and you want to arrange the objects in different sequences, you want each sequence to start with certain objects, but you don't care how the objects are arranged because red-green is the same as green-red. This type of arrangement is called a combination. To calculate it, you can use the following formula:

Combination

ApplicationTopic Applied: Introducing Buttons

  1. Start Microsoft Visual Studio
  2. Create a Console Application named Algebra
  3. Change the document as follows:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : System.Windows.Forms.Form = new System.Windows.Forms.Form();
    
    let rec factorial (x : int) : int =
        if x <= 1 then
            1;
        else
            x * factorial(x - 1);
    
    let rec permutation (n : int) (r : int) : int =
        if r = 0 then
            0;
        elif n = 0 then
            0;
        elif (r >= 0) && (r <= n) then
            factorial(n) / factorial(n - r);
        else
            0;
    
    let combinatorial (a : int) (b : int) : int =
        if a <= 1 then
            1;
        else
            factorial(a) / (factorial(b) * factorial(a - b));
    
    // Tab Control: Algebra
    let tclAlgebra = new System.Windows.Forms.TabControl();
    tclAlgebra.HotTrack <- true;
    tclAlgebra.Location <- new System.Drawing.Point(12, 12);
    tclAlgebra.SelectedIndex <- 0;
    tclAlgebra.Size <- new System.Drawing.Size(272, 158);
    tclAlgebra.TabIndex <- 0;
    
    // Tab Page: Factorial
    let tabFactorial = new System.Windows.Forms.TabPage();
    tabFactorial.Location <- new System.Drawing.Point(4, 22);
    tabFactorial.Padding <- new System.Windows.Forms.Padding(3);
    tabFactorial.Size <- new System.Drawing.Size(264, 132);
    tabFactorial.TabIndex <- 0;
    tabFactorial.Text <- "Factorial";
    tabFactorial.UseVisualStyleBackColor <- true;
    
    let lblFactorialNumber :System.Windows.Forms.Label = new System.Windows.Forms.Label();
    lblFactorialNumber.AutoSize <- true;
    lblFactorialNumber.Location <- new System.Drawing.Point(22, 21);
    lblFactorialNumber.Size <- new System.Drawing.Size(47, 13);
    lblFactorialNumber.TabIndex <- 4;
    lblFactorialNumber.Text <- "Number:";
    tabFactorial.Controls.Add(lblFactorialNumber);
    
    // Text Box: Factorial Number
    let txtFactorialNumber = new System.Windows.Forms.TextBox();
    txtFactorialNumber.Location <- new System.Drawing.Point(88, 18);
    txtFactorialNumber.Size <- new System.Drawing.Size(50, 20);
    txtFactorialNumber.TabIndex <- 5;
    txtFactorialNumber.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabFactorial.Controls.Add(txtFactorialNumber);
    
    // Text Box: Factorial Result
    let txtFactorialResult = new System.Windows.Forms.TextBox();
    txtFactorialResult.Location <- new System.Drawing.Point(88, 54);
    txtFactorialResult.Size <- new System.Drawing.Size(140, 20);
    txtFactorialResult.TabIndex <- 7;
    txtFactorialResult.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabFactorial.Controls.Add(txtFactorialResult);
    
    // Button: Calculate Factorial
    let btnCalculateFactorial = new System.Windows.Forms.Button();
    btnCalculateFactorial.Location <- new System.Drawing.Point(153, 17);
    btnCalculateFactorial.Size <- new System.Drawing.Size(75, 23);
    btnCalculateFactorial.TabIndex <- 17;
    btnCalculateFactorial.Text <- "Calculate";
    let btnCalculateFactorialClick(e : EventArgs) =
        let mutable number : int = 0;
        let mutable result : int = 0;
    
        try
            number <- (int txtFactorialNumber.Text);
            result <- factorial(number);
            txtFactorialResult.Text <- result.ToString();
        with
        | :? FormatException as exc -> MessageBox.Show("Invalid Number", "Algebra",
                                              MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;
    btnCalculateFactorial.Click.Add(btnCalculateFactorialClick);
    tabFactorial.Controls.Add(btnCalculateFactorial);
    
    // Label: Factorial Result
    let lblFactorialResult = new System.Windows.Forms.Label();
    lblFactorialResult.AutoSize <- true;
    lblFactorialResult.Location <- new System.Drawing.Point(22, 56);
    lblFactorialResult.Size <- new System.Drawing.Size(40, 13);
    lblFactorialResult.TabIndex <- 6;
    lblFactorialResult.Text <- "Result:";
    tabFactorial.Controls.Add(lblFactorialResult);
    tclAlgebra.Controls.Add(tabFactorial);
    
    // Tab Page: Permutation
    let tabPermutation = new System.Windows.Forms.TabPage();
    tabPermutation.Location <- new System.Drawing.Point(4, 22);
    tabPermutation.Padding <- new System.Windows.Forms.Padding(3);
    tabPermutation.Size <- new System.Drawing.Size(264, 132);
    tabPermutation.TabIndex <- 1;
    tabPermutation.Text <- "Permutation";
    
    // Label: Permutation n
    let lblPermutationN = new System.Windows.Forms.Label();
    lblPermutationN.AutoSize <- true;
    lblPermutationN.Location <- new System.Drawing.Point(22, 21);
    lblPermutationN.Size <- new System.Drawing.Size(16, 13);
    lblPermutationN.TabIndex <- 8;
    lblPermutationN.Text <- "n:";
    tabPermutation.Controls.Add(lblPermutationN);
    
    // Text Box: Permutation n
    let txtPermutationN = new System.Windows.Forms.TextBox();
    txtPermutationN.Location <- new System.Drawing.Point(88, 18);
    txtPermutationN.Size <- new System.Drawing.Size(50, 20);
    txtPermutationN.TabIndex <- 9;
    txtPermutationN.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabPermutation.Controls.Add(txtPermutationN);
    
    // Label: Permutation r
    let lblPermutationR = new System.Windows.Forms.Label();
    lblPermutationR.AutoSize <- true;
    lblPermutationR.Location <- new System.Drawing.Point(22, 56);
    lblPermutationR.Size <- new System.Drawing.Size(13, 13);
    lblPermutationR.TabIndex <- 10;
    lblPermutationR.Text <- "r:";
    tabPermutation.Controls.Add(lblPermutationR);
    
    // Text Box: Permutation r
    let txtPermutationR = new System.Windows.Forms.TextBox();
    txtPermutationR.Location <- new System.Drawing.Point(88, 54);
    txtPermutationR.Size <- new System.Drawing.Size(50, 20);
    txtPermutationR.TabIndex <- 11;
    txtPermutationR.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabPermutation.Controls.Add(txtPermutationR);
    tclAlgebra.Controls.Add(tabPermutation);
    
    // Text Box: Permutation Result
    let txtPermutationResult = new System.Windows.Forms.TextBox();
    txtPermutationResult.Location <- new System.Drawing.Point(88, 90);
    txtPermutationResult.Size <- new System.Drawing.Size(140, 20);
    txtPermutationResult.TabIndex <- 13;
    txtPermutationResult.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabPermutation.Controls.Add(txtPermutationResult)
    
    // Button: Calculate Permutation
    let btnCalculatePermutation = new System.Windows.Forms.Button();
    btnCalculatePermutation.Location <- new System.Drawing.Point(152, 53);
    btnCalculatePermutation.Size <- new System.Drawing.Size(75, 23);
    btnCalculatePermutation.TabIndex <- 14;
    btnCalculatePermutation.Text <- "Calculate";
    btnCalculatePermutation.UseVisualStyleBackColor <- true;
    let btnCalculatePermutationClick(e : EventArgs) =
        let mutable n : int = 0;
        let mutable r : int = 0;
        let mutable result : int = 0;
    
        try
            n <- (int txtPermutationN.Text);
        with
        | :? FormatException as exc -> MessageBox.Show("Invalid Number", "Algebra",
                                                       MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;
    
        try
            r <- (int txtPermutationR.Text);
            result <- (permutation n r);
            txtPermutationResult.Text <- result.ToString();
        with
        | :? FormatException as exc -> MessageBox.Show("Invalid Number", "Algebra",
                                                       MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;
    
    btnCalculatePermutation.Click.Add(btnCalculatePermutationClick);
    tabPermutation.Controls.Add(btnCalculatePermutation);
    
    // Tab Page: Combination
    let tabCombination = new System.Windows.Forms.TabPage();
    tabCombination.Location <- new System.Drawing.Point(4, 22);
    tabCombination.Padding <- new System.Windows.Forms.Padding(3);
    tabCombination.Size <- new System.Drawing.Size(264, 132);
    tabCombination.TabIndex <- 2;
    tabCombination.Text <- "Combination";
    tclAlgebra.Controls.Add(tabCombination);
    
    // Label: Combination n
    let lblCombinationN = new System.Windows.Forms.Label();
    lblCombinationN.AutoSize <- true;
    lblCombinationN.Location <- new System.Drawing.Point(22, 21);
    lblCombinationN.Size <- new System.Drawing.Size(16, 13);
    lblCombinationN.TabIndex <- 17;
    lblCombinationN.Text <- "n:";
    tabCombination.Controls.Add(lblCombinationN);
    
    // Text Box: Combination Result
    let txtCombinationResult = new System.Windows.Forms.TextBox();
    txtCombinationResult.Location <- new System.Drawing.Point(88, 90);
    txtCombinationResult.Size <- new System.Drawing.Size(140, 20);
    txtCombinationResult.TabIndex <- 22;
    txtCombinationResult.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabCombination.Controls.Add(txtCombinationResult);
    
    // Text Box: Combination n
    let txtCombinationN = new System.Windows.Forms.TextBox();
    txtCombinationN.Location <- new System.Drawing.Point(88, 18);
    txtCombinationN.Size <- new System.Drawing.Size(50, 20);
    txtCombinationN.TabIndex <- 18;
    txtCombinationN.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabCombination.Controls.Add(txtCombinationN);
    
    // Label: Combination r
    let lblCombinationR = new System.Windows.Forms.Label();
    lblCombinationR.AutoSize <- true;
    lblCombinationR.Location <- new System.Drawing.Point(22, 56);
    lblCombinationR.Size <- new System.Drawing.Size(13, 13);
    lblCombinationR.TabIndex <- 19;
    lblCombinationR.Text <- "r:";
    tabCombination.Controls.Add(lblCombinationR);
    
    // Text Box: Combination r
    let txtCombinationR = new System.Windows.Forms.TextBox();
    txtCombinationR.Location <- new System.Drawing.Point(88, 54);
    txtCombinationR.Size <- new System.Drawing.Size(50, 20);
    txtCombinationR.TabIndex <- 20;
    txtCombinationR.TextAlign <- System.Windows.Forms.HorizontalAlignment.Right;
    tabCombination.Controls.Add(txtCombinationR);
    
    // Label: Combination Result
    let lblCombinationResult = new System.Windows.Forms.Label();
    lblCombinationResult.AutoSize <- true;
    lblCombinationResult.Location <- new System.Drawing.Point(22, 92);
    lblCombinationResult.Size <- new System.Drawing.Size(38, 13);
    lblCombinationResult.TabIndex <- 21;
    lblCombinationResult.Text <- "C(n, r):";
    tabCombination.Controls.Add(lblCombinationResult);
    
    // Label: Permutation Result
    let lblPermutationResult = new System.Windows.Forms.Label();
    lblPermutationResult.AutoSize <- true;
    lblPermutationResult.Location <- new System.Drawing.Point(22, 92);
    lblPermutationResult.Size <- new System.Drawing.Size(38, 13);
    lblPermutationResult.TabIndex <- 12;
    lblPermutationResult.Text <- "P(n, r):";
    tabPermutation.Controls.Add(lblPermutationResult);
    
    let btnCalculateCombinationClick(e : EventArgs) =
        let mutable n : int = 0;
        let mutable r : int = 0;
        let mutable result : int = 0;
    
        try
            n <- (int txtCombinationN.Text);
        with
        | :? FormatException as exc -> MessageBox.Show("Invalid Number", "Algebra",
                                                       MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;
    
        try
            r <- (int txtCombinationR.Text);
            result <- (combinatorial n r);
            txtCombinationResult.Text <- result.ToString();
        with
        | :? FormatException as exc -> MessageBox.Show("Invalid Number", "Algebra",
                                                       MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore;
    // Button: Calculate Combination
    let btnCalculateCombination = new System.Windows.Forms.Button();
    btnCalculateCombination.Location <- new System.Drawing.Point(152, 53);
    btnCalculateCombination.Size <- new System.Drawing.Size(75, 23);
    btnCalculateCombination.TabIndex <- 23;
    btnCalculateCombination.Text <- "Calculate";
    btnCalculateCombination.Click.Add(btnCalculateCombinationClick);
    tabCombination.Controls.Add(btnCalculateCombination);
    
    exercise.Controls.Add(tclAlgebra);
    
    // Button: Close
    let btnClose : Button = new System.Windows.Forms.Button();
    btnClose.Location <- new System.Drawing.Point(209, 180);
    btnClose.Size <- new System.Drawing.Size(75, 23);
    btnClose.TabIndex <- 4;
    btnClose.Text <- "Close";
    let btnCloseClick(e : EventArgs) = exercise.Close();
    btnClose.Click.Add(btnCloseClick);
    exercise.Controls.Add(btnClose);
    
    exercise.ClientSize <- new System.Drawing.Size(298, 210);
    exercise.FormBorderStyle <- System.Windows.Forms.FormBorderStyle.FixedDialog;
    exercise.MaximizeBox <- false;
    exercise.MinimizeBox <- false;
    exercise.StartPosition <- System.Windows.Forms.FormStartPosition.CenterScreen;
    exercise.Text <- "Factorial, Permutation, and Combination";
    
    [<EntryPoint>]
    let main argv = 
        Application.Run(exercise);
        0
  4. Execute the application, enter some values and click the Calculate buttons

Windows Controls Examples: Algebra

Windows Controls Examples: Algebra

Windows Controls Examples: Algebra


Home Copyright © 2015 FunctionX, Inc. Home