0% found this document useful (0 votes)
31 views2 pages

Gauss Piv Partiala

This C++ program uses Gaussian elimination to solve a system of linear equations. It takes in the coefficients of the equations from the user, stores them in a 2D array, and performs Gaussian elimination on the array. It then back-substitutes the results to output the solutions for each variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Gauss Piv Partiala

This C++ program uses Gaussian elimination to solve a system of linear equations. It takes in the coefficients of the equations from the user, stores them in a 2D array, and performs Gaussian elimination on the array. It then back-substitutes the results to output the solutions for each variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <cstdlib>
#include <math.h>
using namespace std;
int a[100][100], i,j,k,lin,piv,n, aux, s;
int main()
{

cout<<"n = ";
cin>>n;
for (i=1;i<=n;i++)
for (j=1; j<=n+1;j++) {
cout<<"a["<<i<<"]["<<j<<"] = ";
cin>>a[i][j];
}

for (k=1; k<=n-1; k++) {


piv = fabs(a[k][k]);
lin = k;
for (i=k+1; i<=n; i++) {
if (piv<fabs(a[i][k])) {
piv = fabs(a[i][k]);
lin = i;

if (piv == 0) {
cout<<"Sistemul nu are solutie unica";
}

if (lin != k) {
for (j=k;j<=n+1; j++) {
aux = a[k][j];
a[k][j]=a[lin][j];
a[lin][j] = aux;
}
}
}
}

for (i=k+1; i<=n;i++) {


a[i][k] = a[i][k]/a[k][k];

for (j=k+1;j<=n+1; j++) {


a[i][j] = a[i][j] - a[i][k] * a[k][j];
}
}
}

if (a[n][n] == 0) {
cout<<"Sistemul nu are solutie unica";
}

a[n][n+1]= a[n][n+1]/a[n][n];

for (i=n-1;i>=1;i--) {
s=0;
for (j=i+1;j<=n;j++) {
s=s+a[i][j]*a[j][n+1];
}
a[i][n+1] = (a[i][n+1]-s)/a[i][i];
}

for (i=1; i<=n; i++)


cout<<"x["<<i<<"] = "<<a[i][n+1]<<endl;

return 0;
}

You might also like