Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double det(double** A, int n){
- double d=0;
- double** sub=new double*[n];
- for(int i=0;i<n;i++){
- sub[i]=new double[n];
- }
- if(n==2){
- return((A[0][0]*A[1][1])-(A[1][0]*A[0][1]));
- }
- else{
- for(int x=0;x<n;x++){
- int subi=0;
- for (int i=1;i<n;i++){
- int subj=0;
- for (int j=0;j<n;j++){
- if(j==x) continue;
- sub[subi][subj]=A[i][j];
- subj++;
- }
- subi++;
- }
- d+=(pow(-1,x)*A[0][x]*det(sub,n-1));
- }
- }
- for(int i=0;i<n;i++){
- delete[] sub;
- }
- delete[] sub;
- return d;
- }
- 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,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;
- }
- }
- void w2(double* Y,int n){
- cout<<"Rozwiązanie:\n";
- for(int i=0;i<n;i++){
- cout<<"X"<<i+1<<": "<<setw(3)<<setprecision(3)<<Y[i]<<" ";
- }
- cout<<endl;
- }
- double* gauss(double** A,double *B,int n){
- for(int i=0;i<n;i++){
- if(A[i][i]==0){
- if(A[i+1][i+1]!=0&&i<n-1){
- double* h=new double[n+1];
- for(int j=0;j<n;j++){
- h[j]=A[i][j];
- A[i][j]=A[i+1][j];
- A[i+1][j]=h[j];
- }
- h[n+1]=B[i];
- B[i]=B[i+1];
- B[i+1]=h[n+1];
- delete[] h;
- gauss(A,B,n);
- }
- else{
- cerr<<"Sprzeczny układ równań!";
- exit(0);
- }
- }
- else{
- double h=A[i][i];
- if(h!=0){
- for(int j=i;j<n;j++){
- A[i][j]/=h;
- }
- B[i]/=h;
- }
- for(int j=i+1;j<n;j++){
- h=A[j][i];
- if(h!=0){
- for(int k=0;k<n;k++){
- A[j][k]-=h*A[i][k];
- }
- B[j]-=h*B[i];
- }
- }
- }
- }
- cout<<"Układ po pierwszym przekształceniu:\n";
- w1(A,B,n);
- for(int i=n-1;i>=0;i--){
- for(int j=i-1;j>=0;j--){
- double h=A[j][i];
- B[j]-=h*B[i];
- }
- }
- for(int i=0;i<n;i++){
- double s=0;
- for(int j=0;j<n;j++){
- s+=A[i][j];
- }
- if(s==0) return 0;
- }
- return B;
- }
- int main(){
- int n;
- cout<<"Podaj stopień macierzy: ";
- cin>>n;
- double** a=man(mac(n),n);
- double* b=wyn(n);
- cout<<"Podany do rozwiązania układ:\n";
- w1(a,b,n);
- if(det(a,n)!=0){
- w2(gauss(a,b,n),n);
- }
- else{
- cout<<"Macierz jest osobliwa!\n";
- }
- clr(a,n);
- delete[] b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement