In: Computer Science
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c ++
Gauss Seidel Method
Gauss-Seidel Method is used to solve the linear system Equations. This method is named after the German Scientist Carl Friedrich Gauss and Philipp Ludwig Siedel. It is a method of iteration for solving n linear equation with the unknown variables. This method is very simple and uses in digital computers for computing.
PROGRAM TO SOLVE MATRICES BY METHOD OF GAUSS-SEIDAL:
#include<iostream>
#include<conio.h>
using namespace std;
int main(void)
{
float a[10][10], b[10], x[10], y[10];
int n = 0, m = 0, i = 0, j = 0;
cout << "Enter size of 2d array(Square matrix) : ";
cin >> n;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << "Enter values no :(" << i << ", "
<< j << ") ";
cin >> a[i][j];
}
}
cout << "\nEnter Values to the right side of
equation\n";
for (i = 0; i < n; i++)
{
cout << "Enter values no :(" << i << ", "
<< j << ") ";
cin >> b[i];
}
cout << "Enter initial values of x\n";
for (i = 0; i < n; i++)
{
cout << "Enter values no. :(" << i<<"):";
cin >> x[i];
}
cout << "\nEnter the number of iteration : ";
cin >> m;
while (m > 0)
{
for (i = 0; i < n; i++)
{
y[i] = (b[i] / a[i][i]);
for (j = 0; j < n; j++)
{
if (j == i)
continue;
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
x[i] = y[i];
}
printf("x%d = %f ", i + 1, y[i]);
}
cout << "\n";
m--;
}
return 0;
}
output:
output with different array size: