2017-04-27 4 views
-5
#include<iostream.h> 
#include<conio.h> 
class matrix 
{ 
    public: 
     int a[3][3]; 

     void getdata() 
     { 
      for(int i=1;i<=3;i++) 
      { 
       for(int j=1;j<=3;j++) 
       { 
        cout<<"Enter numbers for ["<<i<<"]["<<j<<"]"; 
        cin>>a[i][j]; 
       } 
      } 
     } 

     void putdata() 
     { 
      for(int i=1;i<=3;i++) 
      { 
       for(int j=1;j<=3;j++) 
       { 
        cout<<a[i][j]; 
       } 
       cout<<endl; 
      } 
     } 
     friend void add(matrix,matrix); 
}; 

void add(matrix m1,matrix m2) 
{ // matrix temp; 
    int a,i,j; 
    for(i=1;i<=3;i++) 
    { 
     a[3][3]=0; 
     for(j=1;j<=3;j++) 
     { 
      // temp.a[i][j]=x.a[i][j]+y.a[i][j]; 
      cout<<m1.a[i][j]+m2.a[i][j]; 
     } 
     cout<<endl; 
    } 
    // return temp; 
} 

int main() 
{ 
    clrscr(); 
    matrix m1,m2,m3; 
    m1.getdata(); 
    m2.getdata(); 
    add(m1,m2); 
    m3.putdata(); 
    getch(); 
    return 0; 
} 

答えて

2

、アレイ素子3にインデックス1を使用してアクセスされていないが、0〜2

for(int i=0; i<3; ++i) 

for(int j=0; j<3; ++j) 
0

配列はゼロベースの要素であるオブジェクト

enter image description here

ので以下3 0〜2 1 2次元配列ではない:

a[3][3]=0; 
    for(int i=1;i<=3;i++) 
     { 
      for(int j=1;j<=3;j++) 
      { 

...そのままインデックス0に関連する要素を残しています
関連する問題