Computer Science, asked by BibekAgarwal7130, 1 year ago

Algorithm to solve system of linear equations in c programing

Answers

Answered by Snowden1738
0
Define a function det(int **A, int N) which calculates the determinant of a matrix A of order N.

Then, accept a square matrix A of order N containing the coefficients of the linear equations row-wise and N being the order of the matrix as well as the number of equations and the number of variables to solve for, a column matrix B of dimensions N by 1 containing the constant terms on the right of each equation.

For example, if the following system of equations need to solved:

2x + 3y + z = 5
5x - 7y + 11z = 2
4x - 7y + 5z = 4

Then, matrix A must be:
2 3 1
5 -7 11
4 -7 5

And matrix B must be:
5
2
4

And N obviously must be 3.

Now, run the following loop:
for(int k = 0; k < N; k++)
{
int temp[N][N];
for(int p = 0; p < n; p++)
{
if(p != k)
for(int i = 0; i < N; i++)
temp[i][p] = a[i][p];
else
for(int i = 0; i < N; i++)
temp[i][k] = b[i];
}
cout << "Variable " << k+1 << " = " << det(temp) / det(a) << endl;
}

Also, you must check for the consistency of the system of equations too.
Similar questions