C++ Program to Represent Linear Equations in Matrix Form

This is a C++ Program to represent a set of linear equations in matrix form.

Here is source code of the C++ Program to Represent Linear Equations in Matrix Form. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<conio.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8.     char var[] = { 'x', 'y', 'z', 'w' };
  9.     cout << "Enter the number of variables in the equations: ";
  10.  
  11.     int n;
  12.     cin >> n;
  13.     cout << "\nEnter the coefficients of each variable for each equations";
  14.     cout << "\nax + by + cz + ... = d";
  15.     int mat[n][n];
  16.     int constants[n][1];
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         for (int j = 0; j < n; j++)
  20.         {
  21.             cin >> mat[i][j];
  22.         }
  23.         cin >> constants[i][0];
  24.     }
  25.  
  26.     cout << "Matrix representation is: ";
  27.     for (int i = 0; i < n; i++)
  28.     {
  29.         for (int j = 0; j < n; j++)
  30.         {
  31.             cout << " " << mat[i][j];
  32.         }
  33.         cout << "  " << var[i];
  34.         cout << "  =  " << constants[i][0];
  35.         cout << "\n";
  36.     }
  37.     return 0;
  38. }

Output:

$ g++ MatrixRepOfEquations.cpp
$ a.out
 
Enter the number of variables in the equations: 3
 
Enter the coefficients of each variable for each equations
ax + by + cz + ... = d
1 2 3 4
1 2 6 8
2 3 9 8
Matrix representation is:  
 1 2 3  x  =  4
 1 2 6  y  =  8
 2 3 9  z  =  8

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

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.