2016-01-25 117 views
-1
#include<iostream> 
#include<conio.h> 
#include<fstream> 

using namespace std; 

int replace(int n[3][3]) 
{ 
for(int i=0;i<3;i++) 
{ 
    for(int j=0;j<3;j++) 
    { 
     if ((i+j)<3) 
     { 
      n[i][j]=0; 
     } 

    } 
} 
cout<<"new array = : \n"; 
    for(int i=0;i<3;i++) 
    { 
     for(int j=0;j<3;j++) 
     { 
      cout<<n[i][j]<<" "; 
     } 
     cout<<"\n"; 
    } 
    return 0; 

    } 

    int main() 
    { 
     int n[3][3],i,j; 
     for(i=0;i<3;i++) 
     { 
      for(j=0;j<3;j++) 
      { 
       cout<<" \n enter number :"<<i<<" row and "<<j<<" column :"; 
       cin>>n[3][3]; 
      } 
     } 
      replace(n); 
return 0; 
getch(); 
} 

該程序應該將所有這些元素替換爲0,其索引總和小於3.替換髮生的很好,但未替換的數字不會顯示,而是一些垃圾值在輸出中顯示。給定代碼輸出錯誤

`unexpected output of above program

回答

2

cin>>n[3][3];應該是cin>>n[i][j];

n[3][3]無法訪問。