C++ Program to Represent Linear Equations in Matrix Form


This is a C++ program to represent Linear Equations in matrix form.

Algorithm

Begin
   1) Take the no of variables n and the coefficients of each variable as input.
   2) Declare a matrix[n][n] and constant[n][1].
   3) Make for loops i = 0 to n-1 and j = 0 to n-1
   to take the coefficients of each variable as the elements of the matrix.
   4) Display the matrix by using nested for loops.
End

Example

#include<iostream>
using namespace std;
int main(void) {
   char variable[] = { 'x', 'y', 'z', 'd' };
   cout << "Enter the number of variables in the
   equations: ";
   int n;
   cin >> n;
   cout << "\nEnter the coefficients of each variable for
   each equation, ax + by + cz + ... = d:";
   int matrix[n][n];
   int constant[n][1];
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         cin >> matrix[i][j];
      }
      cin >> constant[i][0];
   }
   cout << "Matrix representation is: "<<endl;
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         cout << " " << matrix[i][j];
      }
      cout << " " << variable[i];
      cout << " = " << constant[i][0];
      cout << "\n";
   }
   return 0;
}

Output

Enter the number of variables in the equations: 3
Enter the coefficients of each variable for each equation,
ax + by + cz + ... = d:
1 2 3 4
5 6 7 9
8 5 2 1
Matrix representation is:
1 2 3 x = 4
5 6 7 y = 9
8 5 2 z = 1

Updated on: 30-Jul-2019

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements