越界怎么也可以???
程序代码:
#include <iostream> using namespace std; int A[3][3]; int B[3][3]; int C[10][10]; int ar=0,ac=0,br=0,bc=0,cr=0,cc=0; int i=0,j=0,k=0; int main() { cout<<"输入第一个矩阵"<<endl; cout<<"How many rows :"; cin>>ar; cout<<"How many cols :"; cin>>ac; for(i=0;i<ac;i++) { for(j=0;j<ar;j++) { cout<<"输入"<<i+1<<"行"<<j+1<<"列"<<"元素"; cin>>A[i][j]; } } cout<<"输入第二个矩阵"<<endl; cout<<"How many rows :"; cin>>br; cout<<"How many cols :"; cin>>bc; for(i=0;i<bc;i++) { for(j=0;j<br;j++) { cout<<"输入"<<i+1<<"行"<<j+1<<"列"<<"元素"; cin>>B[i][j]; } } if(ac!=br) cout<<"矩阵不符合"; else { cr = ar;cc = bc; for( i = 0; i < ar; i++) for( j = 0; j < bc; j++) for( k = 0; k < ac; k++) C[i][j]+=A[i][k]*B[k][j]; } for(i=0;i<cr;i++) { for(j=0;j<cc;j++) cout<<C[i][j]<<endl; } system("Pause"); return 0; }