#include <iostream> using namespace std; int main() {inta[2][3]={{1,2,3},人4,5,6}: int b[3][2],ij; cout<<"array a:"<<endl; for(i=0i<=1i++) {forj=0j<=2j++) cout<<a[i][j]<<"" b[j][i]=a[i][]} cout<<endl; cout<<"array b:"<<endl; for(i=0;i<=2i++) {for(j=0j<=1j++) cout<<b[i][j]<<""; cout<<endl; return 0;}
#include <iostream> using namespace std; int main( ) {int a[2][3]={{1,2,3},{4,5,6}}; int b[3][2],i,j; cout<<″array a:″<<endl; for (i=0;i<=1;i++) { for (j=0;j<=2;j++) { cout<<a[i][j]<<″ ″; b[j][i]=a[i][j];} cout<<endl; } cout<<″array b:″<<endl; for (i=0;i<=2;i++) {for(j=0;j<=1;j++) cout<<b[i][j]<<″ ″; cout<<endl; } return 0;}
例:有一个3×4的矩阵,要求编程序求出其中值最大的 那个元素的值,以及其所在的行号和列号。 #include <iostream> using namespace std; int main() int i,j,row=0,colum=0,max; inta[3][4]={{5,12,23,56,X19,28,37,46 {-12,-34,6,8}; max=a[O][O]; for(i=0i<=2;i++) for (j=0;j<=3;j++) if (a[i]j]>max) {max=a[i][j]; row=i; colum=j; cout<<"max="<<max<<"row="<<row <<"colum="<<colum <<endl; return O; }
例:有一个3×4的矩阵,要求编程序求出其中值最大的 那个元素的值,以及其所在的行号和列号。 #include <iostream> using namespace std; int main( ) { int i,j,row=0,colum=0,max; int a[3][4]={{5,12,23,56},{19,28,37,46}, {-12,-34,6,8}}; max=a[0][0]; for (i=0;i<=2;i++) for (j=0;j<=3;j++) if (a[i][j]>max) {max=a[i][j]; row=i; colum=j; } cout<<″max=″<<max<<″,row=″<<row <<″,colum=″<<colum <<endl; return 0; }
求一个3*3矩阵对角线之和 】 #include <iostream> using namespace std; int main() {inta[3][3]={{1,2,3}{4,5,6X7,8,9}: int i,s; for(i=0;i<3;i++) s=s+a[i][i]; cout<<"s="<<s<<endl return 0;
求一个3*3矩阵对角线之和 1 2 3 a= 4 5 6 7 8 9 #include <iostream> using namespace std; int main( ) {int a[3][3]={{1,2,3},{4,5,6},{7,8,9}}; int i, s; for(i=0;i<3;i++) s=s+a[i][i]; cout<<”s=”<<s<<endl; return 0; }
5.4用数组名作晶数参数 1.用数组元素作函数实参 数组元素也可以作为函数的实参,与用变 量作实参一样,将数组元素的值传送给形参变 量。 例:用函数处理上例。 #include <iostream> using namespace std; int main() int max_value(int x,int max); int i j,row=0,colum=0,max; inta[3][4]={{5,12,23,56}K19,28,3746, {-12,-34,6,8}i
5.4 用数组名作函数参数 1. 用数组元素作函数实参 数组元素也可以作为函数的实参,与用变 量作实参一样,将数组元素的值传送给形参变 量。 例:用函数处理上例。 #include <iostream> using namespace std; int main( ) { int max_value(int x,int max); int i,j,row=0,colum=0,max; int a[3][4]={{5,12,23,56},{19,28,37,46}, {-12,-34,6,8}};
max=a[O][O]; for(i=0i<=2;i++) for(j=0;j<=3;j++) max=max_value(a[i][j],max); if(max==a[i][j]) [row=i; colum=j; } } cout<<"max="<<max<<",row="<<row <<"colum="<<colum<<endl; } int max_value(int x,int max) if(x>max)return x; else return max;
max=a[0][0]; for (i=0;i<=2;i++) for (j=0;j<=3;j++) { max=max_value(a[i][j],max); if(max==a[i][j]) {row=i; colum=j; } } cout<<″max=″<<max<<″,row=″<<row <<″,colum=″<<colum<<endl; } int max_value(int x,int max) {if(x>max) return x; else return max; }