C++ Program to Solve Linear Equation

«
»
This is a C++ Program to solve linear equation. This problem shows, solution to linear equations of N variable in general.

Here is source code of the C++ Program to Solve any Linear Equation in One Variable. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. #if !defined(MATRIX_H)
2. #define MATRIX_H
3. #include <stdio.h>
4. #include <iostream>
5. #include <tchar.h>
6. #include <math.h>
7. #include <stdlib.h>
8.
9. class CMatrix
10. {
11.     private:
12.         int m_rows;
13.         int m_cols;
14.         char m_name[128];
15.         CMatrix();
16.     public:
17.         double **m_pData;
18.         CMatrix(const char *name, int rows, int cols) :
19.             m_rows(rows), m_cols(cols)
20.         {
21.             strcpy(m_name, name);
22.             m_pData = new double*[m_rows];
23.             for (int i = 0; i < m_rows; i++)
24.                 m_pData[i] = new double[m_cols];
25.             for (int i = 0; i < m_rows; i++)
26.             {
27.                 for (int j = 0; j < m_cols; j++)
28.                 {
29.                     m_pData[i][j] = 0.0;
30.                 }
31.             }
32.         }
33.         CMatrix(const CMatrix &other)
34.         {
35.             strcpy(m_name, other.m_name);
36.             m_rows = other.m_rows;
37.             m_cols = other.m_cols;
38.             m_pData = new double*[m_rows];
39.             for (int i = 0; i < m_rows; i++)
40.                 m_pData[i] = new double[m_cols];
41.             for (int i = 0; i < m_rows; i++)
42.             {
43.                 for (int j = 0; j < m_cols; j++)
44.                 {
45.                     m_pData[i][j] = other.m_pData[i][j];
46.                 }
47.             }
48.         }
49.         ~CMatrix()
50.         {
51.             for (int i = 0; i < m_rows; i++)
52.                 delete[] m_pData[i];
53.             delete[] m_pData;
54.             m_rows = m_cols = 0;
55.         }
56.         void SetName(const char *name)
57.         {
58.             strcpy(m_name, name);
59.         }
60.         const char* GetName() const
61.         {
62.             return m_name;
63.         }
64.         void GetInput()
65.         {
66.             std::cin >> *this;
67.         }
68.         void FillSimulatedInput()
69.         {
70.             static int factor1 = 1, factor2 = 2;
71.             std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: "
72.                     << m_rows << " Cols: " << m_cols << "\n";
73.             for (int i = 0; i < m_rows; i++)
74.             {
75.                 for (int j = 0; j < m_cols; j++)
76.                 {
77.                     std::cout << "Input For Row: " << i + 1 << " Col: " << j
78.                             + 1 << " = ";
79.                     int data = ((i + 1) * factor1) + (j + 1) * factor2;
80.                     m_pData[i][j] = data / 10.2;
81.                     std::cout << m_pData[i][j] << "\n";
82.                     factor1 += (rand() % 4);
83.                     factor2 += (rand() % 3);
84.                 }
85.                 std::cout << "\n";
86.             }
87.             std::cout << "\n";
88.         }
89.         double Determinant()
90.         {
91.             double det = 0;
92.             double **pd = m_pData;
93.             switch (m_rows)
94.             {
95.                 case 2:
96.                 {
97.                     det = pd[0][0] * pd[1][1] - pd[0][1] * pd[1][0];
98.                     return det;
99.                 }
100.                     break;
101.                 case 3:
102.                 {
103.                     /***
104.                      a b c
105.                      d e f
106.                      g h i
107.
108.                      a b c a b c
109.                      d e f d e f
110.                      g h i g h i
111.
112.
113.                      // det (A) = aei + bfg + cdh - afh - bdi - ceg.
114.                      ***/
115.                     double a = pd[0][0];
116.                     double b = pd[0][1];
117.                     double c = pd[0][2];
118.                     double d = pd[1][0];
119.                     double e = pd[1][1];
120.                     double f = pd[1][2];
121.                     double g = pd[2][0];
122.                     double h = pd[2][1];
123.                     double i = pd[2][2];
124.                     double det = (a * e * i + b * f * g + c * d * h); // - a*f*h - b*d*i - c*e*g);
125.                     det = det - a * f * h;
126.                     det = det - b * d * i;
127.                     det = det - c * e * g;
128.                     //std::cout << *this;
129.                     //std::cout << "deter: " << det << " \n";
130.
131.                     return det;
132.                 }
133.                     break;
134.                 case 4:
135.                 {
136.                     CMatrix *temp[4];
137.                     for (int i = 0; i < 4; i++)
138.                         temp[i] = new CMatrix("", 3, 3);
139.                     for (int k = 0; k < 4; k++)
140.                     {
141.                         for (int i = 1; i < 4; i++)
142.                         {
143.                             int j1 = 0;
144.                             for (int j = 0; j < 4; j++)
145.                             {
146.                                 if (k == j)
147.                                     continue;
148.                                 temp[k]->m_pData[i - 1][j1++]
149.                                         = this->m_pData[i][j];
150.                             }
151.                         }
152.                     }
153.                     double det = this->m_pData[0][0] * temp[0]->Determinant()
154.                             - this->m_pData[0][1] * temp[1]->Determinant()
155.                             + this->m_pData[0][2] * temp[2]->Determinant()
156.                             - this->m_pData[0][3] * temp[3]->Determinant();
157.                     return det;
158.                 }
159.                     break;
160.                 case 5:
161.                 {
162.                     CMatrix *temp[5];
163.                     for (int i = 0; i < 5; i++)
164.                         temp[i] = new CMatrix("", 4, 4);
165.                     for (int k = 0; k < 5; k++)
166.                     {
167.                         for (int i = 1; i < 5; i++)
168.                         {
169.                             int j1 = 0;
170.                             for (int j = 0; j < 5; j++)
171.                             {
172.                                 if (k == j)
173.                                     continue;
174.                                 temp[k]->m_pData[i - 1][j1++]
175.                                         = this->m_pData[i][j];
176.                             }
177.                         }
178.                     }
179.                     double det = this->m_pData[0][0] * temp[0]->Determinant()
180.                             - this->m_pData[0][1] * temp[1]->Determinant()
181.                             + this->m_pData[0][2] * temp[2]->Determinant()
182.                             - this->m_pData[0][3] * temp[3]->Determinant()
183.                             + this->m_pData[0][4] * temp[4]->Determinant();
184.                     return det;
185.                 }
186.                 case 6:
187.                 case 7:
188.                 case 8:
189.                 case 9:
190.                 case 10:
191.                 case 11:
192.                 case 12:
193.                 default:
194.                 {
195.                     int DIM = m_rows;
196.                     CMatrix **temp = new CMatrix*[DIM];
197.                     for (int i = 0; i < DIM; i++)
198.                         temp[i] = new CMatrix("", DIM - 1, DIM - 1);
199.                     for (int k = 0; k < DIM; k++)
200.                     {
201.                         for (int i = 1; i < DIM; i++)
202.                         {
203.                             int j1 = 0;
204.                             for (int j = 0; j < DIM; j++)
205.                             {
206.                                 if (k == j)
207.                                     continue;
208.                                 temp[k]->m_pData[i - 1][j1++]
209.                                         = this->m_pData[i][j];
210.                             }
211.                         }
212.                     }
213.                     double det = 0;
214.                     for (int k = 0; k < DIM; k++)
215.                     {
216.                         if ((k % 2) == 0)
217.                             det = det + (this->m_pData[0][k]
218.                                     * temp[k]->Determinant());
219.                         else
220.                             det = det - (this->m_pData[0][k]
221.                                     * temp[k]->Determinant());
222.                     }
223.                     for (int i = 0; i < DIM; i++)
224.                         delete temp[i];
225.                     delete[] temp;
226.                     return det;
227.                 }
228.                     break;
229.             }
230.         }
231.         CMatrix& operator =(const CMatrix &other)
232.         {
233.             if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
234.             {
235.                 std::cout
236.                         << "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";
237.             }
238.             for (int i = 0; i < m_rows; i++)
239.                 delete[] m_pData[i];
240.             delete[] m_pData;
241.             m_rows = m_cols = 0;
242.             strcpy(m_name, other.m_name);
243.             m_rows = other.m_rows;
244.             m_cols = other.m_cols;
245.             m_pData = new double*[m_rows];
246.             for (int i = 0; i < m_rows; i++)
247.                 m_pData[i] = new double[m_cols];
248.             for (int i = 0; i < m_rows; i++)
249.             {
250.                 for (int j = 0; j < m_cols; j++)
251.                 {
252.                     m_pData[i][j] = other.m_pData[i][j];
253.                 }
254.             }
255.             return *this;
256.         }
257.         CMatrix CoFactor()
258.         {
259.             CMatrix cofactor("COF", m_rows, m_cols);
260.             if (m_rows != m_cols)
261.                 return cofactor;
262.             if (m_rows < 2)
263.                 return cofactor;
264.             else if (m_rows == 2)
265.             {
266.                 cofactor.m_pData[0][0] = m_pData[1][1];
267.                 cofactor.m_pData[0][1] = -m_pData[1][0];
268.                 cofactor.m_pData[1][0] = -m_pData[0][1];
269.                 cofactor.m_pData[1][1] = m_pData[0][0];
270.                 return cofactor;
271.             }
272.             else if (m_rows >= 3)
273.             {
274.                 int DIM = m_rows;
275.                 CMatrix ***temp = new CMatrix**[DIM];
276.                 for (int i = 0; i < DIM; i++)
277.                     temp[i] = new CMatrix*[DIM];
278.                 for (int i = 0; i < DIM; i++)
279.                     for (int j = 0; j < DIM; j++)
280.                         temp[i][j] = new CMatrix("", DIM - 1, DIM - 1);
281.                 for (int k1 = 0; k1 < DIM; k1++)
282.                 {
283.                     for (int k2 = 0; k2 < DIM; k2++)
284.                     {
285.                         int i1 = 0;
286.                         for (int i = 0; i < DIM; i++)
287.                         {
288.                             int j1 = 0;
289.                             for (int j = 0; j < DIM; j++)
290.                             {
291.                                 if (k1 == i || k2 == j)
292.                                     continue;
293.                                 temp[k1][k2]->m_pData[i1][j1++]
294.                                         = this->m_pData[i][j];
295.                             }
296.                             if (k1 != i)
297.                                 i1++;
298.                         }
299.                     }
300.                 }
301.                 bool flagPositive = true;
302.                 for (int k1 = 0; k1 < DIM; k1++)
303.                 {
304.                     flagPositive = ((k1 % 2) == 0);
305.                     for (int k2 = 0; k2 < DIM; k2++)
306.                     {
307.                         if (flagPositive == true)
308.                         {
309.                             cofactor.m_pData[k1][k2]
310.                                     = temp[k1][k2]->Determinant();
311.                             flagPositive = false;
312.                         }
313.                         else
314.                         {
315.                             cofactor.m_pData[k1][k2]
316.                                     = -temp[k1][k2]->Determinant();
317.                             flagPositive = true;
318.                         }
319.                     }
320.                 }
321.                 for (int i = 0; i < DIM; i++)
322.                     for (int j = 0; j < DIM; j++)
323.                         delete temp[i][j];
324.                 for (int i = 0; i < DIM; i++)
325.                     delete[] temp[i];
326.                 delete[] temp;
327.             }
328.             return cofactor;
329.         }
331.         {
332.             CMatrix cofactor("COF", m_rows, m_cols);
334.             if (m_rows != m_cols)
336.             cofactor = this->CoFactor();
337.             // adjoint is transpose of a cofactor of a matrix
338.             for (int i = 0; i < m_rows; i++)
339.             {
340.                 for (int j = 0; j < m_cols; j++)
341.                 {
343.                 }
344.             }
346.         }
347.         CMatrix Transpose()
348.         {
349.             CMatrix trans("TR", m_cols, m_rows);
350.             for (int i = 0; i < m_rows; i++)
351.             {
352.                 for (int j = 0; j < m_cols; j++)
353.                 {
354.                     trans.m_pData[j][i] = m_pData[i][j];
355.                 }
356.             }
357.             return trans;
358.         }
359.         CMatrix Inverse()
360.         {
361.             CMatrix cofactor("COF", m_rows, m_cols);
362.             CMatrix inv("INV", m_rows, m_cols);
363.             if (m_rows != m_cols)
364.                 return inv;
365.             // to find out Determinant
366.             double det = Determinant();
367.             cofactor = this->CoFactor();
368.             // inv = transpose of cofactor / Determinant
369.             for (int i = 0; i < m_rows; i++)
370.             {
371.                 for (int j = 0; j < m_cols; j++)
372.                 {
373.                     inv.m_pData[j][i] = cofactor.m_pData[i][j] / det;
374.                 }
375.             }
376.             return inv;
377.         }
378.         CMatrix operator +(const CMatrix &other)
379.         {
380.             if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
381.             {
382.                 std::cout
383.                         << "Addition could not take place because number of rows and columns are different between the two matrices";
384.                 return *this;
385.             }
386.             CMatrix result("", m_rows, m_cols);
387.             for (int i = 0; i < m_rows; i++)
388.             {
389.                 for (int j = 0; j < m_cols; j++)
390.                 {
391.                     result.m_pData[i][j] = this->m_pData[i][j]
392.                             + other.m_pData[i][j];
393.                 }
394.             }
395.             return result;
396.         }
397.         CMatrix operator -(const CMatrix &other)
398.         {
399.             if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
400.             {
401.                 std::cout
402.                         << "Subtraction could not take place because number of rows and columns are different between the two matrices";
403.                 return *this;
404.             }
405.             CMatrix result("", m_rows, m_cols);
406.             for (int i = 0; i < m_rows; i++)
407.             {
408.                 for (int j = 0; j < m_cols; j++)
409.                 {
410.                     result.m_pData[i][j] = this->m_pData[i][j]
411.                             - other.m_pData[i][j];
412.                 }
413.             }
414.             return result;
415.         }
416.         CMatrix operator *(const CMatrix &other)
417.         {
418.             if (this->m_cols != other.m_rows)
419.             {
420.                 std::cout
421.                         << "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";
422.                 return *this;
423.             }
424.             CMatrix result("", this->m_rows, other.m_cols);
425.             for (int i = 0; i < this->m_rows; i++)
426.             {
427.                 for (int j = 0; j < other.m_cols; j++)
428.                 {
429.                     for (int k = 0; k < this->m_cols; k++)
430.                     {
431.                         result.m_pData[i][j] += this->m_pData[i][k]
432.                                 * other.m_pData[k][j];
433.                     }
434.                 }
435.             }
436.             return result;
437.         }
438.         bool operator ==(const CMatrix &other)
439.         {
440.             if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
441.             {
442.                 std::cout
443.                         << "Comparision could not take place because number of rows and columns are different between the two matrices";
444.                 return false;
445.             }
446.             CMatrix result("", m_rows, m_cols);
447.             bool bEqual = true;
448.             for (int i = 0; i < m_rows; i++)
449.             {
450.                 for (int j = 0; j < m_cols; j++)
451.                 {
452.                     if (this->m_pData[i][j] != other.m_pData[i][j])
453.                         bEqual = false;
454.                 }
455.             }
456.             return bEqual;
457.         }
458.         friend std::istream& operator >>(std::istream &is, CMatrix &m);
459.         friend std::ostream& operator <<(std::ostream &os, const CMatrix &m);
460. };
461. std::istream& operator >>(std::istream &is, CMatrix &m)
462. {
463.     std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: "
464.             << m.m_rows << " Cols: " << m.m_cols << "\n";
465.     for (int i = 0; i < m.m_rows; i++)
466.     {
467.         for (int j = 0; j < m.m_cols; j++)
468.         {
469.             std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1
470.                     << " = ";
471.             is >> m.m_pData[i][j];
472.         }
473.         std::cout << "\n";
474.     }
475.     std::cout << "\n";
476.     return is;
477. }
478. std::ostream& operator <<(std::ostream &os, const CMatrix &m)
479. {
480.     os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: "
481.             << m.m_cols << "\n\n";
482.     for (int i = 0; i < m.m_rows; i++)
483.     {
484.         os << " | ";
485.         for (int j = 0; j < m.m_cols; j++)
486.         {
487.             char buf[32];
488.             double data = m.m_pData[i][j];
489.             if (m.m_pData[i][j] > -0.00001 && m.m_pData[i][j] < 0.00001)
490.                 data = 0;
491.             sprintf(buf, "%10.2lf ", data);
492.             os << buf;
493.         }
494.         os << "|\n";
495.     }
496.     os << "\n\n";
497.     return os;
498. }
499. #endif
500. int main()
501. {
502.     CMatrix a("A", 6, 6);
503.     CMatrix b("B", 6, 1);
504.     a.FillSimulatedInput();
505.     b.FillSimulatedInput();
506.     std::cout << a << "\n Determinant : ";
507.     std::cout << a.Determinant() << "\n";
508.     std::cout << b << "\n Determinant : ";
509.     std::cout << b.Determinant() << "\n";
510.     CMatrix ainv = a.Inverse();
511.     CMatrix q = a * ainv;
512.     q.SetName("A * A'");
513.     std::cout << q << "\n";
514.     CMatrix x = ainv * b;
515.     x.SetName("X");
516.     std::cout << x << "\n";
517.     CMatrix y = a * x; // we will get B
518.     y.SetName("Y");
519.     std::cout << y << "\n";
520.     return 0;
521. }

Output:

\$ g++ SolveLinearEquation.cpp
\$ a.out

Enter Input For Matrix : A Rows: 6 Cols: 6
Input For Row: 1 Col: 1 = 0.294118
Input For Row: 1 Col: 2 = 0.980392
Input For Row: 1 Col: 3 = 1.86275
Input For Row: 1 Col: 4 = 2.84314
Input For Row: 1 Col: 5 = 3.62745
Input For Row: 1 Col: 6 = 5.58824

Input For Row: 2 Col: 1 = 2.94118
Input For Row: 2 Col: 2 = 4.11765
Input For Row: 2 Col: 3 = 5.88235
Input For Row: 2 Col: 4 = 8.43137
Input For Row: 2 Col: 5 = 10.3922
Input For Row: 2 Col: 6 = 12.3529

Input For Row: 3 Col: 1 = 8.13725
Input For Row: 3 Col: 2 = 9.70588
Input For Row: 3 Col: 3 = 12.0588
Input For Row: 3 Col: 4 = 15.098
Input For Row: 3 Col: 5 = 17.8431
Input For Row: 3 Col: 6 = 20.5882

Input For Row: 4 Col: 1 = 14.902
Input For Row: 4 Col: 2 = 18.2353
Input For Row: 4 Col: 3 = 21.4706
Input For Row: 4 Col: 4 = 24.7059
Input For Row: 4 Col: 5 = 27.549
Input For Row: 4 Col: 6 = 31.1765

Input For Row: 5 Col: 1 = 24.902
Input For Row: 5 Col: 2 = 27.9412
Input For Row: 5 Col: 3 = 32.451
Input For Row: 5 Col: 4 = 36.0784
Input For Row: 5 Col: 5 = 39.7059
Input For Row: 5 Col: 6 = 43.9216

Input For Row: 6 Col: 1 = 36.3725
Input For Row: 6 Col: 2 = 39.6078
Input For Row: 6 Col: 3 = 43.8235
Input For Row: 6 Col: 4 = 47.2549
Input For Row: 6 Col: 5 = 51.3725
Input For Row: 6 Col: 6 = 55.2941

Enter Input For Matrix : B Rows: 6 Cols: 1
Input For Row: 1 Col: 1 = 9.41176

Input For Row: 2 Col: 1 = 15.7843

Input For Row: 3 Col: 1 = 22.7451

Input For Row: 4 Col: 1 = 29.902

Input For Row: 5 Col: 1 = 37.1569

Input For Row: 6 Col: 1 = 44.6078

Matrix : A Rows: 6 Cols: 6

|       0.29       0.98       1.86       2.84       3.63       5.59 |
|       2.94       4.12       5.88       8.43      10.39      12.35 |
|       8.14       9.71      12.06      15.10      17.84      20.59 |
|      14.90      18.24      21.47      24.71      27.55      31.18 |
|      24.90      27.94      32.45      36.08      39.71      43.92 |
|      36.37      39.61      43.82      47.25      51.37      55.29 |

Determinant : -11.9339

Matrix : B Rows: 6 Cols: 1

|       9.41 |
|      15.78 |
|      22.75 |
|      29.90 |
|      37.16 |
|      44.61 |

Determinant : -1.#IND

Matrix : A * A' Rows: 6 Cols: 6

|       1.00       0.00       0.00       0.00       0.00       0.00 |
|       0.00       1.00       0.00       0.00       0.00       0.00 |
|       0.00       0.00       1.00       0.00       0.00       0.00 |
|       0.00       0.00       0.00       1.00       0.00       0.00 |
|       0.00       0.00       0.00       0.00       1.00       0.00 |
|       0.00       0.00       0.00       0.00       0.00       1.00 |

Matrix : X Rows: 6 Cols: 1

|       0.82 |
|       3.47 |
|      -9.38 |
|       7.71 |
|      -5.76 |
|       3.98 |

Matrix : Y Rows: 6 Cols: 1

|       9.41 |
|      15.78 |
|      22.75 |
|      29.90 |
|      37.16 |
|      44.61 |

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.