欢迎来到天天文库
浏览记录
ID:29961262
大小:48.01 KB
页数:5页
时间:2018-12-25
《高斯列主元消去法》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、3.高斯列主元消去法,求解其次线性方程组C/C++code#include#include#defineN20intmain(){intn,i,j,k;intmi,tmp,mx;floata[N][N],b[N],x[N];printf("Inputn:");scanf("%d",&n);if(n>N){printf("Theinputnshouldin(0,N)!");getch();return1;}if(n<=0){printf("Theinputnshouldin(0,N)!");getch(
2、);return1;}printf("Nowinputa(i,j),i,j=0...%d:",n-1);for(i=0;imx){mi=j;mx=fa
3、bs(a[j][i]);}if(i=0;i--){x[i]=b[i];for(j=i+1;j4、;j++)x[i]-=a[i][j]*x[j];x[i]/=a[i][i];}for(i=0;i#include#defineNUMBER20#defineEsc0x1b#defineEnter0x0dfloatA[NUMBER][NUMBER+1],ark;intflag,n;exchange(intr,intk);floatmax(intk);message();main(5、){floatx[NUMBER];intr,k,i,j;charcelect;clrscr();printf("UseGauss.");printf("1.JiepleasepressEnter.");printf("2.ExitpressEsc.");celect=getch();if(celect==Esc)exit(0);printf("inputn=");scanf("%d",&n);printf("InputmatrixAandB:");for(i=1;i<=n;i++){printf("6、Inputa%d1--a%d%dandb%d:",i,i,n,i);for(j=1;j<=n+1;j++)scanf("%f",&A[i][j]);}for(k=1;k<=n-1;k++){ark=max(k);if(ark==0){printf("It'swrong!");message();}elseif(flag!=k)exchange(flag,k);for(i=k+1;i<=n;i++)for(j=k+1;j<=n+1;j++)A[i][j]=A[i][j]-A[k][j]*A[i][k]/A[k][k];}x[n]=A[n][7、n+1]/A[n][n];for(k=n-1;k>=1;k--){floatme=0;for(j=k+1;j<=n;j++){me=me+A[k][j]*x[j];}x[k]=(A[k][n+1]-me)/A[k][k];}for(i=1;i<=n;i++){printf("x%d=%f",i,x[i]);}message();}exchange(intr,intk){inti;for(i=1;i<=n+1;i++)A[0][i]=A[r][i];for(i=1;i<=n+1;i++)A[r][i]=A[k][i];for(i=1;i<=8、n+1;i++)A[k][i]=A[0][i];}floatmax(intk){inti;floattemp=0;for(i=k;i<=
4、;j++)x[i]-=a[i][j]*x[j];x[i]/=a[i][i];}for(i=0;i#include#defineNUMBER20#defineEsc0x1b#defineEnter0x0dfloatA[NUMBER][NUMBER+1],ark;intflag,n;exchange(intr,intk);floatmax(intk);message();main(
5、){floatx[NUMBER];intr,k,i,j;charcelect;clrscr();printf("UseGauss.");printf("1.JiepleasepressEnter.");printf("2.ExitpressEsc.");celect=getch();if(celect==Esc)exit(0);printf("inputn=");scanf("%d",&n);printf("InputmatrixAandB:");for(i=1;i<=n;i++){printf("
6、Inputa%d1--a%d%dandb%d:",i,i,n,i);for(j=1;j<=n+1;j++)scanf("%f",&A[i][j]);}for(k=1;k<=n-1;k++){ark=max(k);if(ark==0){printf("It'swrong!");message();}elseif(flag!=k)exchange(flag,k);for(i=k+1;i<=n;i++)for(j=k+1;j<=n+1;j++)A[i][j]=A[i][j]-A[k][j]*A[i][k]/A[k][k];}x[n]=A[n][
7、n+1]/A[n][n];for(k=n-1;k>=1;k--){floatme=0;for(j=k+1;j<=n;j++){me=me+A[k][j]*x[j];}x[k]=(A[k][n+1]-me)/A[k][k];}for(i=1;i<=n;i++){printf("x%d=%f",i,x[i]);}message();}exchange(intr,intk){inti;for(i=1;i<=n+1;i++)A[0][i]=A[r][i];for(i=1;i<=n+1;i++)A[r][i]=A[k][i];for(i=1;i<=
8、n+1;i++)A[k][i]=A[0][i];}floatmax(intk){inti;floattemp=0;for(i=k;i<=
此文档下载收益归作者所有