Sum of all neighboring cells 2d array

Apr 22, 2021 at 5:19pm
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 Apr 22, 2021 at 9:15pm
Apr 22, 2021 at 5:58pm
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;
}
Apr 22, 2021 at 6:21pm
Thank you lastchance, but whenever i run the program i dont get the right numbers. its always just a little off.
Apr 22, 2021 at 6:36pm
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.
Apr 22, 2021 at 8:56pm
I have tried quite a few different ways to code this but it still seems to not add to the correct numbers.
Apr 22, 2021 at 9:01pm
Please place the second of my codes in place of your function KingsSum() and show the complete output that you say is incorrect.
Apr 22, 2021 at 9:18pm
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;
}
Apr 22, 2021 at 9:29pm
SHOW the output that you claim is wrong. The whole output from your program.

-16 +80 -80 -50 +6 is ... -60
Last edited on Apr 22, 2021 at 9:32pm
Apr 22, 2021 at 9:52pm
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.
Apr 22, 2021 at 10:08pm
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;
}
Apr 23, 2021 at 5:13am
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 Apr 23, 2021 at 5:19am
Topic archived. No new replies allowed.