Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main(){
- int n,i,j,k;
- cout.precision(4);
- cout.setf(ios::fixed);
- cout<<"podaj liczbe niewiadomych: "<<endl;
- cin>>n;
- float a[n][n+1], x[n];
- cout<<"wpisz niewiadome: "<<endl;
- for(i=0;i<n;i++)
- for(j=0;j<=n;j++)
- cin>>a[i][j];
- for(i=0;i<n;i++)
- for(k=i+1;k<n;k++)
- if(a[i][j]<a[k][i])
- for (j=0;j<=n;j++){
- double temp = a[i][j];
- a[i][j] = a[k][j];
- a[k][j]=temp;
- }
- cout<<"Macierz po wykonaniu zmiany rzedow:"<<endl;
- for(i=0;i<n;i++){
- for(j=0;j<=n;j++)
- cout<<a[i][j]<<setw(16);
- cout<<"\n";
- }
- for(i=0;i<n;i++)
- for(k=i+1;k<n;k++){
- double t = a[k][i]/a[i][i];
- for(j=0;j<=n;j++){
- a[k][j]=a[k][j]-t*a[i][j];
- }
- }
- cout<<"macierz po eliminacji gaussa: "<<endl;
- for(i=0;i<n;i++){
- for(j=0;j<=n;j++)
- cout<<a[i][j]<<setw(16);
- cout<<"\n";
- }
- for (i=n-1;i>=0;i--) //back-substitution
- { //x is an array whose values correspond to the values of x,y,z..
- x[i]=a[i][n]; //make the variable to be calculated equal to the rhs of the last equation
- for (j=i+1;j<n;j++)
- if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
- x[i]=x[i]-a[i][j]*x[j];
- x[i]=x[i]/a[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
- }
- cout<<"\nThe values of the variables are as follows:\n";
- for (i=0;i<n;i++)
- cout<<x[i]<<endl; // Print the values of x, y,z,....
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement