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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>

using namespace 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;
}
Last edited on
1
2
3
4
5
6
7
8
9
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;
}
Thank you lastchance, but whenever i run the program i dont get the right numbers. its always just a little off.
1
2
3
4
5
6
7
8
9
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.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>

using namespace 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;
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cmath>
#include <ctime>
#include <string>
#include <iomanip>
#include <iostream>

using namespace 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).
Last edited on
Topic archived. No new replies allowed.