0

In my program im trying to add the values of a sub-part of my 2d array, a small box of 3 by 3 from a 9 by 9 matrix. Im choosing that small box by the % of rows and columns by 3(modulus 3). (meaning it will take cells like [0][0], [0][3], and so on ) which i want those to be my top right corner of the box and then add 2 more rows and columns making it for instance if we started at [0][0] we will add [0-2][0-2] (3 by 3 box). Im calculating that through a function (as a practice to use functions). The problem is that the program seems to only take in the value of the first cell from that small box, and when i try to loop on the rest of that small box, and add their values, it doesnt take the values correctly (or at all) i want to know if my parameters are wrong, or im giving the function wrong parameters. any help would be appreciated.

//------------including section-----------
#include <iostream>
#include <cstdlib>
//------------using section---------------
using std::cin;
using std::cout;
using std::endl;
//-----our constants and variables---------
const int N=3; //initializing our rows and cols as constants
int counter=0, arr[N*N][N*N];
int sumofrow=0, sumofcol=0,sumsquare=0;
//-------prototypes----------------
void READ_MATRIX(int arr[][N*N]);
bool issquare(int arr[][N*N],int row, int col);
//-------main-------------
int main()
{
    //calling on the function to input our matrix
    READ_MATRIX(arr);

    //checking what functions returned
    if(counter==0)
        cout<<1;
    else
        cout <<0;
    return EXIT_SUCCESS;
}
//-----functions--------
//----readmatrix------
void READ_MATRIX(int arr[][N*N])
{
    for (int row=0; row<N*N; row++)
        for (int col=0; col<N*N; col++) {
            cin >> arr[row][col];
            if (row%3==0&&col%3==0)
                issquare(arr, row, col);
        }
}
//---------issquare-------------
bool issquare(int arr[][N*N],int row, int col)
{
    sumsquare=0;
    for (int r=0;r<3;r++) //trying to loop on values of array
        for (int c=0;c<3;c++)//trying to loop {
            //r+row(because row is passed into the function at 0,3,6)
            //same for col. 
            sumsquare+=arr[r+row][c+col]; // this is where it goes wrong
        }
    //checking to see if sum reached a certain value..
    if (sumsquare==45)
        return true;
    else {
        counter++;
        return false;
    }
}

3 Answers 3

1

You are adding values before you accept them. For example, when row = 0 and col = 0 in function READ_MATRIX(), you call issquare() before all values under that 3x3 box are accepted. In case you have initialized all values to zero, the only value contributing to your sum is the first value i.e. arr[0][0].

What you need to do is trigger issquare() function for row = 2,4,8 and col = 2,4,8. Inside the function issquare(), index the array as arr[row-r][col-c].

Sign up to request clarification or add additional context in comments.

2 Comments

thats great!! i realized that it was only outputting the first number in that small box, but i will make those changes !! thanks dude, very much appreciated.
I don't think that combining the reading with the measuring of properties (issquare) within the same function is a good idea (unless, these properties are important for the reading of further data, which does not apply here). Therefore, such muddling of concepts (reading and measuring) should be avoided.
1

The error is that issquare() is called before the values it uses have been assigned/read. At the call to issquare(), of all values used in that function, only arr[row][col] is known yet.

What you have to do is to first read the data completely, and only then look at their properties.

1 Comment

ah! what you are trying to say is that when for instance when row=0 and col=0 is passed in, only that value in cell [0][0] is defined and rest is still not defined/intialized? Thanks!! that makes sense !!
0

Your function issquared is getting called on the first element of your array.

>>> 0 % 3
0

So you are trying to access values outside of your array. CLARIFICATION: They are not initialised yet so they do not belong to your array just yet (this is an over-simplification, the issue is related to memory allocation which I don't know if you have started yet)

Change the line:

if (row%3==0&&col%3==0)

To:

if ((row != 0 && col != 0) && (row%3 == 0 && col%3 == 0))

Also I'd suggested doing a similar check for the last element just to make sure your smaller box is within the boundaries of your matrix.

1 Comment

yeah that makes perfect sense, cause the rest of my 2d array is still not intialized. thanks! if i change that, then i will never be able to check the left upper corner of my 2d array that starts with 0 0! but i understand what you meant and your help is appreciadted!!

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.