Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double** mac(const int n){
- double** a=new double*[n];
- for(int i=0;i<n;i++){
- a[i]=new double[n];
- }
- return a;
- }
- double** man(double** a,const int n){
- double s;
- cout<<"Podaj macierz:\n";
- for(int i=0;i<n;i++){
- cout<<"Wiersz "<<i+1<<".: ";
- for(int j=0;j<n;j++){
- cin>>s;
- a[i][j]=s;
- }
- }
- return a;
- }
- void clr(double** a,const int n){
- for(int i=0;i<n;i++){
- delete[] a[i];
- }
- delete[] a;
- }
- double* wyn(const int n){
- double* w=new double[n];
- double s;
- cout<<"Podaj macierz wynikową: ";
- for(int i=0;i<n;i++){
- cin>>s;
- w[i]=s;
- }
- return w;
- }
- void w1(double** A,double* Y,const int n){
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- cout<<setw(4)<<setprecision(3)<<A[i][j]<<" ";
- }
- if(i==n/2){
- cout<<" * ";
- }
- else{
- cout<<" ";
- }
- cout<<"X"<<i+1;
- if(i==n/2){
- cout<<" = ";
- }
- else{
- cout<<" ";
- }
- cout<<setw(4)<<setprecision(3)<<Y[i]<<endl;
- }
- }
- bool domr(double** A,const int n){
- double s;
- for(int i=0;i<n;i++){
- s=0;
- for(int j=0;j<n;j++){
- if(i!=j) s+=A[i][j];
- }
- if(abs(A[i][i])<=s){
- cerr<<"Wyrazy głównej przekątnej nie są silnie dominujące!\n";
- return false;
- }
- }
- return true;
- }
- void gsei(double** A,double* B,double* X,const int n,const int iter){
- double* Xk=new double[n];
- for(int k=0;k<iter;k++){
- for(int i=0;i<n;i++){
- Xk[i]=0;
- double s1=0,s2=0;
- for(int j=0;j<i;j++){
- s1+=A[i][j]*Xk[j];
- }
- for(int j=n-1;j>i;j--){
- s2+=A[i][j]*X[j];
- }
- Xk[i]=(1/A[i][i])*(B[i]-s1-s2);
- }
- cout<<"Iteracja "<<k+1<<":";
- for(int l=0;l<n;l++){
- cout<<" X"<<l+1<<": "<<setw(6)<<setprecision(3)<<Xk[l];
- X[l]=Xk[l];
- }
- cout<<endl;
- }
- delete[] Xk;
- }
- int main(){
- int n,it;
- cout<<"Metoda Gaussa-Seidla\n\n";
- cout<<"Podaj stopień macierzy: ";
- cin>>n;
- double** a=man(mac(n),n);
- double* b=wyn(n);
- double* x=new double[n];
- cout<<"Podaj początkowe wartości x: ";
- for(int i=0;i<n;i++){
- cin>>x[i];
- }
- cout<<"Podaj liczbę iteracji: ";
- cin>>it;
- cout<<"Podany do rozwiązania układ:\n";
- w1(a,b,n);
- if(domr(a,n)){
- gsei(a,b,x,n,it);
- }
- clr(a,n);
- delete[] b;
- delete[] x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement