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?
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>
usingnamespace std;
int KingsSum(int[8][8], int, int);
// DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL
int main() {
int row, col;
int board[8][8];
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[8][8], 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;
}
int KingsSum(int board[8][8], 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;
}
int KingsSum(int board[8][8], 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.
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>
usingnamespace std;
int KingsSum(int[8][8], int, int);
// DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL
int main() {
int row, col;
int board[8][8];
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[8][8], 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;
}
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.
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>
usingnamespace std;
int KingsSum(int[8][8], int, int);
// DO NOT CHANGE ANYTHING INSIDE OF MAIN EXCEPT THE VALUES OF ROW AND COL
int main() {
int row, col;
int board[8][8];
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[8][8], 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).