-1

I have this problem where I need to compute the number of ways in which elements that are consecutive in a matrix can be summed in order to obtain a certain number. Let me give you an example : Searched number = 42

Matrix :

  • 00 03 07 09 10
  • 09 13 20 05 20
  • 12 11 33 00 12
  • 17 39 22 03 18
  • 12 15 31 01 17

    There are 4 possibilities to obtain 42 : 42 = 10+20+12 = 9 + 13 + 20 = 9 + 11 + 22 = 20 + 0 + 22.

I don't succeed in checking the sum diagonally .I succeeded in transversing the matrix diagonally but my algorithm is not doing the correct sum:

int getNumbers(int **arr,int length,int number){
int count=0;


//check sum each row / horizontally
for(int i=0;i<length;i++){
    for(int j=0;j<length;j++){
        int sum=0;
        int k=j;
        do{
            if(arr[k][j]!=number){
                sum=sum+arr[i][k];
            }
            k++;
        }while(sum<number && k<length);
        if(sum==number){
            count++;
        }
    }
}


//check sum each column/ vertically
for(int j=0;j<length;j++){
    for(int i=0;i<length;i++){
        int sum=0;
        int k=i;
        do{
            if(arr[k][j]!=number){
                sum=sum+arr[k][j];
            }
            k++;
        }while(sum<number && k<length);
        if(sum==number){
            count++;
        }
    }
}

//check sum each diagonally
for (int j = 0; j < 2 * length - 1; j++) {
    for (int i = 0; i < length; i++) {
        int row = i;
        int col = j - i;
        int sum=0;
        if (col >= 0 && col <= length - 1) {
            do{
                if(arr[row][col]!=number){
                    sum=sum+arr[row][col];
                }
            }while(sum<number && row<length && col<length);
            if(sum==number){
                count++;
            }
        }       
    }
}

for (int j = 0; j < 2 * length - 1; j++) {
    int z = (j < length) ? 0 : j - length + 1;
    int len = j + 1 - 2 * z;
    for (int i = 0; i < len; i++) {
        int sum=0;
        int row = length - 1 - i - z;
        int col = j - i - z;
        do{
            if(arr[row][col]!=number){
                    sum=sum+arr[row][col];
            }
        }while(sum<number && row<length && col<length);
        if(sum==number){
            count++;
        }
    }
}

return count;
 }
  • Please demonstrate your success in traversing diagonally and what the difference to then (un-)successfully doing the adding up is. – Yunnosch Oct 08 '17 at 15:39
  • Do all groups of adjacent elements have exactly three summands? (If not, 20 + 00 + 18 + 01 + 03 would also be valid.) – M Oehm Oct 08 '17 at 15:43
  • If you replace do-while and if(sum==number) in both last for loops with a printf it will print the matrix diagonally from both directions.As for the result if it used for another similar matrix in which replacing that 00 with 1 and 12 with 42 it will give me that I have 7 possible sums instead of 4 – CaptainBadger Oct 08 '17 at 15:48
  • The elements need to be on the same diagonal. 20 - 00 -18 are on the same diagonal but 03 and 01 are in different locations.Also there can be more than 3 elements that can be summed in order to obtain the result but no less than 2( so if an element is 42 in the matrix it shouldn't be counted). – CaptainBadger Oct 08 '17 at 15:51
  • Your diagonal traversing `do { … } while (…)` loops don't seem to have any increment in them. Either they're executed once because the condition is false on the first iteration or they run indefinitely because the condition is true and that never changes. – Jonathan Leffler Oct 08 '17 at 16:18

1 Answers1

0

For a working solution it is enough to replicate the stretegy that you used for the horizontal and vertical cases, that is:

  • Consider each cell as a possible starting point, except the ones at the borders
  • Traverse in the desired direction

A solution for left-right and top-bottom diagonals would be:

for(int j=0; j < length-1; j++)
{
    for(int i=0; i < length-1; i++)
    {
        if(arr[i][j] == number)
            continue;  

        int sum=0;
        int localColumn = i;
        int localRow = j;
        do{
            sum += arr[localRow][localColumn];
            localColumn++;
            localRow++;
        }
        while( sum < number && localRow < length && localColumn < length);
        if(sum==number){
            count++;
        }
    }
}

Note that I moved the check for the single 42 cell out of the do while loop: your code just skips it during the sum, making a sequence like 20 42 22 valid.

rzippo
  • 979
  • 12
  • 22