ep5_4.cpp
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:2k
- /* 5.4 将[例5.5]改用一维数组,附加行、列参数,实现通用算法。*/
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void inverse(int [], int [],int,int);//注意数组最高维可缺省,例5.5因初学未省
- void multi(int [], int [], int [],int,int,int);
- void output(int [],int,int);
- int main(){
- int middle[6*3], result[6*4];//注意写作6*3等可清楚看出矩阵的行列
- int matrix1[3*6]={8,10,12,23,1,3,5,7,9,2,4,6,34,45,56,2,4,6};
- int matrix2[3*4]={3,2,1,0,-1,-2,9,8,7,6,5,4};
- output(matrix1,3,6);
- inverse(matrix1,middle,3,6);
- output(middle,6,3);
- output(matrix2,3,4);
- multi(middle,matrix2,result,6,3,4);
- output(result,6,4);
- return 0;
- }
- void inverse(int matrix1_1[],int middle_1[],int a,int b){
- int i,j;
- for (i=0;i<a;i++)
- for (j=0;j<b;j++)
- middle_1[i+j*a]=matrix1_1[i*b+j];
- return;
- }
- void multi(int middle_1[],int matrix2_1[],int result_1[],int a,int b,int c){
- int i,j,k;
- for (i=0;i<a;i++){
- for (j=0;j<c;j++){
- result_1[i*c+j] = 0;
- for (k=0;k<b;k++)
- result_1[i*c+j]+=middle_1[i*b+k]*matrix2_1[k*c+j];
- }
- }
- return;
- }
- void output(int max_1[],int a,int b){
- for (int i=0;i<a;i++){
- for (int j=0;j<b;j++)
- cout <<setw(4)<<max_1[i*b+j]<<" ";
- cout<<'n';
- }
- cout<<endl;
- return;
- }