011 – C++ Builder. Developing the application of solution of the system of linear algebraic equations by Gauss

Developing the application of solution of the system of linear algebraic equations by Gauss

The task

The system of linear algebraic equations is given:

05_01_00_011_formula01

We need to develop the application, that realized the solving of system of linear algebraic equations by Gauss. The application must be realized as a wizard, where information on the next steps is defined from information on the previous step.

 

Progress

  1. Run Borland C++ Builder.

Save the project.

 The name of module of main form of application we leave by default “Unit1.cpp“.

05_01_00_011_01_

Fig. 1. The application form with the created files

 

  1. Developing the main form of application.

2.1. Title of application.

Set the title of application. To do this, you need to set the property “Caption” of main form into value “Gauss method“.

2.2. Set the properties of form.

 Select the form. In Object Inspector you need to set the value of the following properties:

– property BorderStyle = bsDialog;

– property Position = poScreenCenter;

– in the property Font you need select such parameters of font: font Tahoma, size 12 pt (Fig. 2).

05_01_00_011_02_

Fig. 2. Setting the parameters of font of main form of application

 As a result, the form of application will have view as shown in Figure 3.

05_01_00_011_03e

Fig. 3. Main form of application

 

2.3. Component of type TGroupBox.

 We place on the form a component of type TGroupBox from Tool Palette.

05_01_00_011_04e

Fig. 4. Component of type TGroupBox

 As a result, the system creates the object named “GroupBox1”.

 We change the size of component Groupbox1 over the entire width of the main form window.

 We set to value “The task” the property “Caption” of component GroupBox1. Application form will have view as shown in Figure 5.

05_01_00_011_05e

Fig. 5. Application form after placing component TGroupBox

 

2.4. Component of type TLabel.

 In the area of component “TGroupbox” we place the component of type TLabel (Fig. 6).

 The property WordWrap of component Label1 we set to value “true” (Fig. 6).

 With the help of mouse we are changing width of text output of component Label1 (Fig. 6).

05_01_00_011_06e

Fig. 6. Component Label1, property WordWrap

 Property Caption of component Label1 we set to value:

Solve the system of linear algebraic equations by Gauss method

As a result, the application form will have view, as shown in Figure 7.

05_01_00_011_07e

Fig. 7. Application form after placing component Label1

 

2.5. Components of type TButton.

 We place on the form components of type TButton. As a result, two objects named “Button1” and “Button2” will be created.

 For better visibility, we change the sizes of components as shown in Figure 8.

05_01_00_011_08e

Fig. 8. Application form with the placed components Button1 and Button2

 We set such properties of components Button1 and Button2:

– in the component Button1 property Caption = “Exit”;

– in the component Button2 property Caption = “Calculation >>”.

 As a result, the form of application will have the following view (Figure 9).

05_01_00_011_09e

Fig. 9. Main form of application after placing all components

 

  1. Programming the event of clicking on the button “Exit“.

 You need to call the event OnClick of component Button1 (button “Exit“) (Figure 10). The event is placed on the tab “Events” in Object Inspector.

 Process of programming the event OnClick is described here in details.

05_01_00_011_10e

Fig. 10. Calling the event OnClick of component Button1

 As a result, the window with program code of event handler will be opened. Between the braces { } we call the method Close().

 Method Close() closes the window of main form of application and realizes all needed operations to free the memory, resources and so on.

 Listing of event handler is the following:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Close();
}

 

  1. Developing the form of inputting the number of equations n.

 4.1. Placement of components on the form and setting up their.

The process of new form creating is described here in details.

To create new form, you need to call the command:

File -> New -> Form C++ Builder

 As a result, the new form will be created (Figure 11). Save the form under name “Unit2.cpp“. The corresponding files will be created:

– file “Unit2.h“, that contains declarations of global variables and including other modules;

– file “Unit2.cpp“, that contains realizations of methods of the form;

– file “Unit2.dfm“, that contains the description of the form on the screen (window size, coordinates of the form, colors and so on).

 The object with name Form2 corresponds to the newly created form. Using this name, you can have access to the properties and methods of the form Form2.

05_01_00_011_11_

Fig. 11. Newly created form Form2

 Let’s realize settings of the form Form2.

 First of all, we set such properties of the form:

– property Caption = “Set the number of equations”;

– property BorderStyle = bsDialog;

– property Position = poScreenCenter;

– in the property Font you need to select the following parameters of font: font Tahome, font size 12.

 We place on the form such components:

– component of type TGroupBox, which will correspond to the object GroupBox1;

– component of type TLabel, it is placed into the area of component GroupBox1. The object-variable Label1 corresponds to the component of type TLabel.

– component of type TEdit, it is placed into the area of component GroupBox1. The object named Edit1 corresponds to this component.

– two components of type TButton with names of their objects “Button1” and “Button2“.

Let’s set up the properties of components:

– in the component GroupBox1 the value of property Caption = “” (empty string);

– in the component Label1 the value of property Caption = “n = “;

– in the component Edit1 the value of property Text = “”;

– in the component Button1 the value of property Caption = “<< Back”;

– in the component Button2 the value of property Caption = “Next >>”.

 After placement the components and editing of form size, the form Form2 has the vies as shown in Figure 12.

05_01_00_011_12e

Fig. 12. Form Form2 after placing all components

 

4.2. Programming the event handlers of form Form2.

 In the form Form2 we program two event handlers:

– the event handler OnClick which corresponds to clicking on the button “<< Back“;

– the event handler OnClick which corresponds to clicking on the button “Next >>“.

 Listing of event handler of click on the Button1 (“<< Back“):

void __fastcall TForm2::Button1Click(TObject *Sender)
{
  ModalResult = mrNo;   
}

Listing of event handler of click on the Button2 (“Next >>“):

void __fastcall TForm2::Button2Click(TObject *Sender)
{
  ModalResult = mrOk;   
}

 Global variable ModalResult corresponds to state of the form. If ModalResult=0, it means that form is opened as a modal window. As soon as ModalResult value will be non-zero, then Form2 form will be closed with a return code, placed in ModalResult.

 Thus, if the user clicks on Button1 button, then Form2 form will be closed with return code mrNo. If the user clicks on button Button2, then Form2 will be closed with return code mrOk.

 

  1. Developing the form of inputting of coefficients in equations.

5.1. Placement the components on the form and their setting up.

  Creating a new form is a standard way which is described in p. 4.

We save the form under the name by default “Unit3.cpp“.

 After creating the form we will get the object named “Form3“. Using this object you can use methods and properties of form Form3.

 To given form corresponds such files: “Unit3.h“, “Unit3.cpp” и “Unit3.dfm“.

 First of all, set the following properties of form Form3, as described in p.4:

– property Caption = “Entering the coefficients of equations”;

– property BorderStyle = bsDialog;

– property Position = poScreenCenter;

– in the property Font you need to select parameters of font: font Tahoma, font size 12.

 After developing the form of entering of coefficients of equations, we need to use such components:

– two components of type TLabel. Automatically two objects named “label1” and “label2” will be created;

– component of type TStringGrid (Fig. 13) to entering the coefficients, which are placed in the left side of system of equations. Component TStringGrid is placed into tab Additional of panel “Tool Palette“. The object named StringGrid1 will be created;

– component of type TStringGrid (Fig. 13) to entering the coefficients, which are placed in the right side of system of equations. The object named StringGrid2 will be created;

– two components of type TButton (buttons “<< Back” and “Next >>“). Two objects named “Button1” and “Button2” will be created.

05_01_00_011_13e

Fig. 13. Component TStringGrid on the palette of components

 After placing the components and editing their sizes, form Form3 will have following view (Fig. 14).

05_01_00_011_14e

Fig. 14. Form “Form3

 We form the properties of components of form Form3:

– in the component Label1 property Caption = “The coefficients in the left side of the equation”;

– in the component Label2 property Caption = “Right side”;

– in the component Button1 property Caption = “<< Back”;

– in the component Button2 property Caption = “Next >>”.

 We form such properties of components of type TStringGrid:

– in the component StringGrid1 property FixedCols = 0 (the number of fixed columns);

– in the component StringGrid1 property FixedRows = 0 (the number of fixed rows);

– in the component StringGrid2 property FixedCols = 0;

– in the component StringGrid2 property FixedRows = 0;

– in the component StringGrid1 we select the tab “Options” and we set option “goEditing” to value “true” (Fig. 15);

– in the component StringGrid2 into tab “Options” the option goEditing = “true”.

05_01_00_011_15e

Fig. 15. Setting up the option goEditing into tab Options of component StringGrid1

 After changes, the form Form3 will have the following view (Figure 16).

05_01_00_011_16e

Fig. 16. Form “Form3

 

5.2. Programming of event handlers of form Form3.

 We are programming the event handlers OnClick of click on the buttons Button1 and Button2 of form Form3.

 Listing of event handlers is the following.

// button "<< Back"
void __fastcall TForm3::Button1Click(TObject *Sender)
{
  ModalResult = mrNo;
}

// button "Next >>"
void __fastcall TForm3::Button2Click(TObject *Sender)
{
  ModalResult = mrOk;
}

 

  1. Developing the result form.

 The last is a form, that will display the result of the calculation. The process of creating and saving the form is described here. When we save the form we leave the name of form by default as “Unit4.cpp“.

 As a result, the new object named Form4 is created.

The newly created form Form4 is described in file “Unit4.dfm“. Also, such files correspond to form: “Unit4.h” and “Unit4.cpp“.

 

6.1. Developing the form Form4.

First of all we set the properties of form Form4:

– property Caption = “Results”;

– property BorderStyle = bsDialog;

– property Position = poScreenCenter;

– in the property Font we need to select the following parameters of font: font Tahoma, font size 12. Also we correct the form size.

 The next step is the placement of components on the form. Place on the form following components (Fig. 17):

– one component of type TLabel;

– two components of type TStringGrid;

– one component of type TButton.

We correct size and positions of components for better displaying.

 After placement of components, such objects will be created: Label1, StringGrid1, StringGrid2, Button1. In the component StringGrid1 the numbers of variable values x in equation are displayed. In component StringGrid2 the results of solving of system of equation are displayed.

 

We configure the components of the form as follows:

– in the component Label1 property Caption = “Decision”;

– in the component Button1 property Caption = “OK”;

– in the component StringGrid1 properties FixedCols=0 and FixedRows=0;

– in the component StringGrid2 properties FixedCols=0 and FixedRows=0.

 After placing and setting the components, the form Form4 will have the view, as shown at Figure 17.

05_01_00_011_17e

Fig. 17. Form Form4 after forming

 

6.2. Programming the event of click on the button “OK” of form Form4.

 Listing of event handler of click on the button “OK” is the following:

 

void __fastcall TForm4::Button1Click(TObject *Sender)
{
  ModalResult = mrOk;   
}

 

  1. Writing the program code of calculation.

 7.1. Including the files “Unit2.h“, “Unit3.h“, “Unit4.h” into the module “Unit1.h“.

 To call the secondary forms from the main form (Form1) of application, you need to include their into module “Unit1.h“.

 Including the modules of forms Form2, Form3, Form4 to form “Form1” is carried out by a standard way for C/C++ language.

 First of all, we need to activate module “Unit1.h“.

Then, after the strings:

#ifndef Unit1H
#define Unit1H
//-------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

you need type such text:

#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"

 

7.2. Entering of variables and constants into the module “Unit1.h“.

Using “Project Manager” we activate the module “Unit1.h“.

 First of all, we enter constant Max, that defines the maximum of equations. To do this, after the strings:

...
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"

we type the text

#define Max 20

 In the section private of class TForm1 of form we enter following variables:

n – number of equations:

A – two dimensional array of size n*(n+1) of coefficients of the variables in the equations;

X – array of solutions of system of equitions;

– variable f_not of integer type, which indicates the presence or absence of the solutions in the system of equations.

 Also, in the section “public” we enter function Calc(), which will realize the calculation by the Gauss method.

 In general, the declaration of class in module “Unit1.h” is the following:

...
class TForm1 : public TForm
{
__published:  // IDE-managed Components
     TGroupBox *GroupBox1;
     TLabel *Label1;
     TButton *Button1;
     TButton *Button2;
     void __fastcall Button1Click(TObject *Sender);
private:     // User declarations
     int n; // number of equations
     float A[Max][Max+1]; // matrix of coefficients
     float X[Max]; // array of solutions
     bool f_not; // true - system not determined
public:       // User declarations
     __fastcall TForm1(TComponent* Owner);
     int Calc(void); // function of calculation by Gauss method  
};
...

 

7.3. Writing the function Calc() of solving the system of equation by Gausse method.

 Go to the “Unit1.cpp” file using the Project Manager.

In file “Unit1.cpp” we type the text of implementation of function Calc(), which is declared in module “Unit1.h“.

If the system of equations is not determined, function Calc() returns 0. Otherwise the return value is 1.

 Listing of method Calc() without explanation details of the algorithm is as follows:

int TForm1::Calc(void)
{
  int u,k,m,i,j;
  int f_exit;
  float t;

  u = 0;
  f_not = false;
  while (u!=n)
  {
    u++;
    k=u;
    while (A[k][u]==0)
    {
      k++;
      if (k>n)
      {
        f_not = true;   // system is not determined
        return 0;
      }
    }
    if (k!=u)
    {
      for (m = u; m<=n+1; m++)
      {
        t = A[u][m];
        A[u][m] = A[k][m];
        A[k][m] = t;  
      }
    }

    for (j = n+1; j >= u; j--)
      A[u][j] = A[u][j]/A[u][u];

    m = n + 1;
    if ((k+1)<=n)
      for (i = k+1; i <= n; i++)
        for (j = u+1; j <= m; j++)
          A[i][j] = A[i][j]-A[i][u] * A[u][j];
  }

  // Building the array of results
  for (i = n; i >= 1; i--)
  {
    X[i] = A[i][m];
    if (i!=1)
      for (k = i-1; k>=1; k--)
        A[k][m] = A[k][m] - A[k][i] * X[i];
  }
  return 1; // is solution
}

 

7.4. Programming the event of clicking on the button “Calculation“.

 Listing of event handler of clicking on the button “Calculation” (Button2) is the following:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  int i, j, res;
  Form1->Visible = false;
  if (Form2->ShowModal()==mrOk) // show the form Form2
  {
    n = StrToInt(Form2->Edit1->Text); // get the number of equations
    Form3->StringGrid1->ColCount = n; // number of columns
    Form3->StringGrid1->RowCount = n; // number of rows
    Form3->StringGrid2->RowCount = n; // right side
    Form3->StringGrid2->ColCount = 1;

    // building the result form
    Form4->StringGrid1->RowCount = n;
    Form4->StringGrid1->ColCount = 1;
    Form4->StringGrid2->RowCount = n;
    Form4->StringGrid2->ColCount = 1;
    for (i = 1; i <= n; i++)
      Form4->StringGrid1->Cells[0][i-1] = "X[" + IntToStr(i) + "]=";

    if (Form3->ShowModal()==mrOk) // show the form of output data
    {
      // Building the matrix A
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
          A[i][j] = StrToFloat(Form3->StringGrid1->Cells[j-1][i-1]);
      // Column B
      for (i = 1; i <= n; i++)
        A[i][n+1] = StrToFloat(Form3->StringGrid2->Cells[0][i-1]);
      Calc();
      if (!f_not)
      {
        // X - array of results
        for (i=1; i<=n; i++)
          Form4->StringGrid2->Cells[0][i-1] = FloatToStrF(X[i],ffFixed,8,2);
        Form4->ShowModal(); // show the result form
      }
      else
        MessageDlg("System is not determined!", mtConfirmation, TMsgDlgButtons() << mbOK, 0);
    }
  }
  Form1->Visible = true;
}

 Let’s explain some code snippets. The output of the form is carried out with the help of function ShowModal().

 Converting of integer type to string is realized by function IntToStr().

 Converting from string type to floating point type is realized by function StrToFloat().

 Converting from floating point type to string type is realized by function FloatToStrF().

To show the message about the undetermined system of equations, is using the function MessageDlg().