# 坐标周围的C ++数组总和值C++ Array Sum Values Around Coordinate

``````#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;
}
``````

## 3 个回复3

### ===============>>#1 票数：1 已采纳

``````int coord1=displayArray[row-2][column-2],
coord2=displayArray[row-2][column-1],
coord3=displayArray[row-2][column],
coord4=displayArray[row-1][column-2],
coord5=displayArray[row-1][column],
coord6=displayArray[row][column-2],
coord7=displayArray[row][column-1],
coord8=displayArray[row][column];
int sum=coord1+coord2+coord3+coord4+coord5+coord6+coord7+coord8;
``````

``````int sum = 0;
for (int x=row-2; x<=row; x++) {
for (int y=column-2; y<=column; y++) {
if ((x != row-1) || (y != column-1)) { //Avoids "center" cell (self)
sum += displayArray[x][y]
}
}
}
``````

### ===============>>#2 票数：1

``````displayArray[row][column]
displayArray[row-1][column]
displayArray[row-2][column]
displayArray[row][column-1]
displayArray[row-2][column-1]
displayArray[row][column-2]
displayArray[row-1][column-2]
displayArray[row-2][column-2]
``````

1回复

4回复

6回复

2回复

2回复

2回复

2回复

2回复

3回复

4回复