-3

I'm trying to solve a problem without using a loop but I don't find a way...

Let take this array for example: (assume there is randomize values)

1, 2, 3, 4, 5
2, 3, 4, 5, 6
3, 4, 5, 6, 7
4, 5, 6, 7, 8
5, 6, 7, 8, 9

By sending (row: 2, column: 1) I want to get the sum of:

1, 2
2, 3
3, 4

I write this recursion function to solve this problem:

static int Func(int[,] matrix, int row, int column)
{
    if (row == -1 || column == -1)
        return 0;

    int result = 0;

    for (int i = 0; i <= column; i++)
    {
        result += matrix[row, i];
    }

    return result + Func(matrix, row - 1, column);
}

That works, but I want replace the loop with extra call to function...

Siavash
  • 2,813
  • 4
  • 29
  • 42
Hazan
  • 323
  • 3
  • 11
  • 2
    can we ask why? – pm100 Oct 12 '18 at 16:21
  • @pm100 Just for learning... – Hazan Oct 12 '18 at 16:24
  • 2
    Why not learn studying scenarios where recursion is actually the right tool? Solving this with recursion doesn’t seem to be even close to a good idea... – InBetween Oct 12 '18 at 16:27
  • 1
    well this is doesnt feel like a naturally recursive thing, if you want to learn to write recursive things go for more naturally recursive operations (factorial, fib, tree search,...) – pm100 Oct 12 '18 at 16:28
  • you need to add some `innerFunc(matrix, row, col, originalrow, originalcol)` with two more arguments, that will be guards for `column` and `row`, and for every recursion call you will be do something like that `return matrx[row,col] + innerFunc(matrix, newrow, newcol, originalrow, originalcol)` for newrow and newcol you will decide what to change based on guard values – Егор Лебедев Oct 12 '18 at 16:29
  • @InBetween Umm I understand, but it's possible? (without changing the function arguments) – Hazan Oct 12 '18 at 16:32
  • @ЕгорЛебедев Thanks, but I asked to solve it without change the arguments of Func, I tried to follow the flow with many combination and I can't find a solution. – Hazan Oct 12 '18 at 16:35
  • You mean only recursively call `Func` with its current signature and still make it work without loops of any kind? I’d say no, not posible – InBetween Oct 12 '18 at 16:35
  • 1
    You seem to be wasting time in a pretty useless problem. Is this some kind of internet challenge or are you simplifying a real world problem that you seem bent on solving in a way that seems unnecessarily hard? – InBetween Oct 12 '18 at 16:39
  • @InBetween there is a challenge in the internet, maybe I waste my time but I really don't know it yet, so I ask you guys... – Hazan Oct 12 '18 at 16:43
  • Well if there is a challenge then there’s a pretty good chance there is a solution but frankly, there are better ways to spend your time learning than trying to solve this one. Good luck though. – InBetween Oct 12 '18 at 16:45
  • @Hazan can a secondary function be used to solve this problem, or should it be solved using only a single function? – vinicius.ras Oct 12 '18 at 16:54

2 Answers2

0

You can always try simplifying recursions like that by thinking of functions that process a single entry and then leave the rest of the entries to be processed by the next recursive call.

Basic idea which can solve your case: try summing the numbers from the right-bottom towards the top-left of the matrix (this way you can use negative indices for rows/columns to verify when you have reached the borders of the matrix).

So there are three key points to that idea in your specific case:

  • (A) The function should return the number located at the given (row,col) position of the matrix, plus the sum of the sequence starting from the next number in the summing sequence: that is sum(row, col) = mat[row,col] + sum(row, col-1).
  • (B) By doing the (A) recursive call repeatedly, at some point the column will be negative... When this happens, we should go to the row above the current line we're processing, and sum all the columns on that line.
  • (C) At some point, all of the matrix will be summed and the row number will be negative. That is when the algorithm needs to end the recursion, as the program has computed the whole input it needed to compute.

So you can write it like this:

static int Func(int[,] matrix, int row, int column, int maxColumn)
{
    // (C) All rows have been processed successfully: stop the recursion.
    if (row < 0)
        return 0;

    // (B) All columns in the current line have been processed: go to the next row
    // which you need to sum
    if (column < 0)
        return Func(matrix, row - 1, maxColumn, maxColumn);

    // (A) The basic definition of your recursion
    return matrix[row, column] + Func(matrix, row, column - 1, maxColumn);
}

In your input example, you can simply call this as:

Func(yourMatrix, 2, 1, 1);

Notice that for that algorithm to work, you need to pass an extra variable maxColumn for the function to know the number of the column it should use when going to the next row it needs to process. The maxColumn and column parameters need obviously to always be equal on the first time you call the Func() function.

vinicius.ras
  • 1,516
  • 3
  • 14
  • 28
  • OP states in comments that changing the signature of the method is not an option. – InBetween Oct 12 '18 at 16:47
  • @InBetween I see, I've just read your comments on that and I also think it is not possible with at least an extra argument, as the function would need to know to which row or column it should fall back during recursion – vinicius.ras Oct 12 '18 at 16:53
0
   public int SumRegion(int row1, int col1, int row2, int col2)
        {
            counter += 1;
            if (counter == 1)
            {   sum=0;
                j = row1;k = col1;
            }
        if (row1 > row2){
            counter=0;
            return sum;
        }
        else if (col1 > col2)
        {
            return SumRegion(++row1, k, row2, col2);
        }
        else if (col1 <= col2)
        {
            sum += matrix1[row1][col1];
            return SumRegion(row1, ++col1, row2, col2);
        }
        else
            return sum;
        }
    }
SRawat
  • 45
  • 8
  • 3
    Please don’t just leave blocks of code as answers. Instead, [edit] your answer to include an explanation of what you’re doing and why you believe it’s the best solution. – Jeremy Caney Jun 10 '22 at 18:26