1

I'm having some difficulty summing the values of a multidimensional array around the coordinates given below. Something is wrong with my logic with the index location. Any ideas would be very helpful and if more information is needed, please don't hesitate to ask me.

#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;
}
1
  • What, exactly, is the problem? Commented Apr 16, 2013 at 3:35

3 Answers 3

1

Your problem may be that you are using row/column -2 and row/column +2 as your adjacent directions, which is not correct. If (row-1, col-1) is your current cell, than you want row/col -2 and row/col +0.

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;

This can be done much more programmatically, however:

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]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

So if I'm understanding this correctly, the user inputs a row and column number, which corresponds to the value located in displayArray[row-1][column-1]

Thus, the coordinates around that cell should be.

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]

I'm not sure exactly why you're using the coordinates you are, but that is your problem.

Also, you're going to want to make sure your program doesn't crash if the user selects a cell on the border of the array. eg: If you try to search coordinates around the cell (0,0), the program will crash when trying to load a value for displayArray[-1][-1]

3 Comments

Thank you, how would you suggest preventing a crash?
The simplest way is with if statements. First declare all your coordinates to be equal to 0, and then use if statements before assigning them values. ex: if (column >= 2 && row >= 2) { //assign coordinate values} You would then need a few more if statements to catch if column was greater than 2, but not row, etc. Honestly, a code rework would be good (Southpaw Hare provides a good example), but you can do this too.
No problem! Good luck coding :)
0

Your mistake is in translating 0 based to 1 based indexing. The arrays are 0 based. You're having them enter in a 1 based row. Then you want to add the 8 numbers around it. If they entered 0 based numbers and entered N, you'd want to sum [n-1], n, and n+1 for each row/column. To deal with 0 based, you want to do n-2, n-1, and n. But you're doing n-2 and n+2. You're also not calculating the middle of the rows anywhere near right.

Best practice is not even to do the math like that. It would be to read in the row/column number, then immediately subtract 1 to make it 0 based, and deal with it as 0 based from then on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.