Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

행렬 형식의 선형 방정식을 나타내는 C++ 프로그램

<시간/>

선형방정식을 행렬 형태로 표현하는 C++ 프로그램입니다.

알고리즘

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

예시

#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;
}

출력

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