### Sum of all neighboring cells 2d array Hello all,
I have an assignment where I have to add all neighboring elements of a 2D array (all 4 adjacent elements)(Left and right elements)(Top and bottom elements) and the position where it is at. I have code written that adds numbers and it adds it pretty close to the sum, but is not the exact numbers. Can anyone tell me what i am doing wrong?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include #include #include using namespace std; int KingsSum(int, int, int); // DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL int main() { int row, col; int board; string colIndices = " 0 1 2 3 4 5 6 7"; // Generate a random board full of numbers in the range of -100 to 100. // Used as a visual for students so they can check if their algorithm in // the KingsSum function is correct. cout << right << endl << endl << colIndices << endl; cout << " ----------------------------------------" << endl; for (int i = 0; i < 8; ++i) { cout << setw(5) << i << " |"; for (int j = 0; j < 8; ++j) { board[i][j] = (rand() % 201) - 100; cout << setw(5) << board[i][j]; } if (i < 7) cout << endl << " |" << endl; else cout << endl << endl << endl; } // Change these values to test your algorithm row = 5; col = 3; cout << "The sum of all the squares at and around the King at (" << row << ", " << col << ") is: "; cout << KingsSum(board, row, col) << endl; } // End of main // DO NOT write any cout statements in here. Only your algorithm int KingsSum(int board, int row, int col) { int sum = board[row][col]; if (row > 0) sum += board[row - 1][col]; if (row < 7) sum += board[row + 1][col]; if (col > 0) sum += board[row][col - 1]; if (col < 7) sum += board[row][col + 1]; return sum; }``````
Last edited on ``123456789`` ``````int KingsSum(int board, int row, int col) { int sum = 0; if ( row > 0 ) sum += board[row-1][col]; if ( row < 7 ) sum += board[row+1][col]; if ( col > 0 ) sum += board[row][col-1]; if ( col < 7 ) sum += board[row][col+1]; return sum; }`````` Thank you lastchance, but whenever i run the program i dont get the right numbers. its always just a little off. ``123456789`` ``````int KingsSum(int board, int row, int col) { int sum = board[row][col]; if ( row > 0 ) sum += board[row-1][col]; if ( row < 7 ) sum += board[row+1][col]; if ( col > 0 ) sum += board[row][col-1]; if ( col < 7 ) sum += board[row][col+1]; return sum; }``````

I hadn't realised you wanted the value AT the King's position as well. I have tried quite a few different ways to code this but it still seems to not add to the correct numbers. Please place the second of my codes in place of your function KingsSum() and show the complete output that you say is incorrect. I am not trying to be a pain, I apologize, but the total that it outputs is not the actual total as the program said -60, but my calculator said -85.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include #include #include using namespace std; int KingsSum(int, int, int); // DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL int main() { int row, col; int board; string colIndices = " 0 1 2 3 4 5 6 7"; // Generate a random board full of numbers in the range of -100 to 100. // Used as a visual for students so they can check if their algorithm in // the KingsSum function is correct. cout << right << endl << endl << colIndices << endl; cout << " ----------------------------------------" << endl; for (int i = 0; i < 8; ++i) { cout << setw(5) << i << " |"; for (int j = 0; j < 8; ++j) { board[i][j] = (rand() % 201) - 100; cout << setw(5) << board[i][j]; } if (i < 7) cout << endl << " |" << endl; else cout << endl << endl << endl; } // Change these values to test your algorithm row = 5; col = 3; cout << "The sum of all the squares at and around the King at (" << row << ", " << col << ") is: "; cout << KingsSum(board, row, col) << endl; } // End of main // DO NOT write any cout statements in here. Only your algorithm int KingsSum(int board, int row, int col) { int sum = board[row][col]; if (row > 0) sum += board[row - 1][col]; if (row < 7) sum += board[row + 1][col]; if (col > 0) sum += board[row][col - 1]; if (col < 7) sum += board[row][col + 1]; return sum; }`````` SHOW the output that you claim is wrong. The whole output from your program.

-16 +80 -80 -50 +6 is ... -60
Last edited on excuse me... In the original post, i had stated the 4 adjacent elements as well. not just the top, bottom, left, right. but the TopLeft, TopCenter, TopRight, Left, AtPosition, Right, BottomLeft, BottomCenter, BottomRight. Got it working

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include #include using namespace std; int KingsSum(int, int, int); // DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL int main() { int row, col; int board; string colIndices = " 0 1 2 3 4 5 6 7"; // Generate a random board full of numbers in the range of -100 to 100. // Used as a visual for students so they can check if their algorithm in // the KingsSum function is correct. cout << right << endl << endl << colIndices << endl; cout << " ----------------------------------------" << endl; for (int i = 0; i < 8; ++i) { cout << setw(5) << i << " |"; for (int j = 0; j < 8; ++j) { board[i][j] = (rand() % 201) - 100; cout << setw(5) << board[i][j]; } if (i < 7) cout << endl << " |" << endl; else cout << endl << endl << endl; } // Change these values to test your algorithm row = 1; col = 1; cout << "The sum of all the squares at and around the King at (" << row << ", " << col << ") is: "; cout << KingsSum(board, row, col) << endl; } // End of main // DO NOT write any cout statements in here. Only your algorithm int KingsSum(int board, int row, int col) { int sum = board[row][col]; if (((row == row - 1) || (row == row + 1)) && ((col == col - 1) || (col == col + 1))) { sum += board[row][col]; } if (row > 0, col > 0) sum += board[row - 1][col - 1]; if (row < 7, col > 0) sum += board[row + 1][col - 1]; if (row > 0, col < 7) sum += board[row - 1][col + 1]; if (row < 7, col < 7) sum += board[row + 1][col + 1]; if (row > 0) sum += board[row - 1][col]; if (row < 7) sum += board[row + 1][col]; if (col > 0) sum += board[row][col - 1]; if (col < 7) sum += board[row][col + 1]; return sum; }`````` ccorkran,
You stated the 4 adjacent elements, plus the king's position itself. So that is what I coded. If you wanted (a maximum of) 9 elements then you should have said so.

If you change row to 0 and col to 0 you will find that your code produces nonsense. (You also retain a line which does precisely nothing above the lines that cause a problem).
Last edited on