我在对下面给出的坐标周围的多维数组的值求和时遇到了一些困难。我的索引位置逻辑有问题。任何想法都会非常有帮助,如果需要更多信息,请随时问我。
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int displayArray[11][11];
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(int column=1;column<10;column++)
{
cout<<"\t "<<column;
}
cout<<"\n\n";
//fill array with random numbers
for(int i=0;i<9;i++)
{
for(int m=0;m<9;m++)
{
displayArray[i][m]=rand()%10;
}
}
cout<<"\n\n\n";
//main data printout
for(int k=0;k<9;k++)
{
cout<<"Row "<<k+1<<"\t ";
for(int l=0;l<9;l++)
{
cout<<displayArray[k][l]<<"\t";
}
cout<<endl<<endl;
}
cout<<"\n\n";
int row=0, column=0;
cout<<"What array cell would you like to see? (Press enter after each entry)\n";
cout<<"Row = ";
cin>>row;
cout<<"Column = ";
cin>>column;
cout<<"\nThe number "<<displayArray[row-1][column-1]<<" is in cell "<<row<<","<<column;
int coord1=displayArray[row-2][column+2],
coord2=displayArray[row+2][column+2],
coord3=displayArray[row-2][column-2],
coord4=displayArray[row-2][column+2],
coord5=displayArray[row-2][column+2],
coord6=displayArray[row-2][column+2],
coord7=displayArray[row-2][column+2],
coord8=displayArray[row-2][column+2];
int sum=coord1+coord2+coord3+coord4+coord5+coord6+coord7+coord8;
cout<<"\n\nThe sum of the cells surrounding "<<row<<","<<column<<" is "<<sum;
cin.get();
cin.get();
return 0;
}