最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Checking the bounds of a 2D Dynamic Array in C++ - Stack Overflow

programmeradmin13浏览0评论

Issue with Recursive 2D Array Traversal Without Built-in Functions

I'm implementing a recursive function to sum even and odd numbers separately in a 2D dynamic array. The function signature is predefined as follows:

int sum(int **array, int row, int column, int &evenSum, int &oddSum);

Constraints:

  • Must use recursion.

  • No built-in functions allowed.

  • Function prototype cannot be changed.

Issue: I'm trying to traverse the array backwards, summing even and odd numbers into separate variables (evenSum, oddSum). However, when I reach the start of a row (column = 0), I need to reset column to the original column size to continue processing the previous row. The problem is that the function signature does not provide the original column size, so I cannot reset it.

Current Implementation:

int sum(int **array, int row, int column, int &evenSum, int &oddSum)
{
    //If we reach the last row, stop recursion
    if (row < 0)
    {
        return 0;
    }

    if (array[row][column] % 2 == 0)
    {
        evenSum += array[row][column];
    }
    else
    {
        oddSum += array[row][column];
    }

    if (column == 0) // If start of a row, move to the previous row
    {
        return sum(array, row - 1, column, evenSum, oddSum);
    }
    else // Move to the previous column in the same row
    {
        return sum(array, row, column - 1, evenSum, oddSum);
    }
}

Question: How can I properly reset the column index when moving to the previous row without modifying the function signature? Would there be a better way to traverse the array recursively under these constraints?

Issue with Recursive 2D Array Traversal Without Built-in Functions

I'm implementing a recursive function to sum even and odd numbers separately in a 2D dynamic array. The function signature is predefined as follows:

int sum(int **array, int row, int column, int &evenSum, int &oddSum);

Constraints:

  • Must use recursion.

  • No built-in functions allowed.

  • Function prototype cannot be changed.

Issue: I'm trying to traverse the array backwards, summing even and odd numbers into separate variables (evenSum, oddSum). However, when I reach the start of a row (column = 0), I need to reset column to the original column size to continue processing the previous row. The problem is that the function signature does not provide the original column size, so I cannot reset it.

Current Implementation:

int sum(int **array, int row, int column, int &evenSum, int &oddSum)
{
    //If we reach the last row, stop recursion
    if (row < 0)
    {
        return 0;
    }

    if (array[row][column] % 2 == 0)
    {
        evenSum += array[row][column];
    }
    else
    {
        oddSum += array[row][column];
    }

    if (column == 0) // If start of a row, move to the previous row
    {
        return sum(array, row - 1, column, evenSum, oddSum);
    }
    else // Move to the previous column in the same row
    {
        return sum(array, row, column - 1, evenSum, oddSum);
    }
}

Question: How can I properly reset the column index when moving to the previous row without modifying the function signature? Would there be a better way to traverse the array recursively under these constraints?

Share Improve this question edited Feb 5 at 12:24 PaulMcKenzie 35.4k4 gold badges26 silver badges48 bronze badges asked Feb 5 at 7:13 Hafiz AbdullahHafiz Abdullah 11 silver badge1 bronze badge 12
  • 3 I assume this is some kind of homework or similar assignment? Otherwise your limitations would be pretty ridiculous. – Some programmer dude Commented Feb 5 at 7:24
  • 1 The constraints do not say that you're not allowed to add another function. – molbdnilo Commented Feb 5 at 7:27
  • 1 As for your problem, the answer is helper functions. The function you show is called, with the original start column. Then you call another function, passign the "default" original column value as an argument, and this new function does the recursive calculations needed. – Some programmer dude Commented Feb 5 at 7:27
  • 1 What is the int returned by the function? it is currently unconditionally 0... – Jarod42 Commented Feb 5 at 8:23
  • 1 @kiner_shah Unfortunately that makes the function non-reentrant, and it can only be called once. – Some programmer dude Commented Feb 5 at 8:37
 |  Show 7 more comments

2 Answers 2

Reset to default 1

As mentioned in comments, you can use a helper function. You cannot change the signature of the function called by the user, but you can write a different function with different signature:

int sum_impl(int **array, int row, int column, int row_end, int col_end, int &evenSum, int &oddSum) {
       // implement recursion here
}


// this is called by the user 
int sum(int **array, int row, int column, int &evenSum, int &oddSum) {
    return sum_impl(array,0,0,row,col,evenSum,oddSum);
}

Note that I changed the recursion to start at array[0][0].

The restriction on specifically built-in functions usually means that you're expected to add at least one function of your own.

I would add a "row-wise" function and use that.
Something like this:

void sumrow(int *array, int element, int &evenSum, int &oddSum)
{
    if (element >= 0) {
        if (array[element] % 2 == 0) {
            evenSum += array[element];
        }
        else {
            oddSum += array[element];
        }
        sumrow(array, element - 1, evenSum, oddSum);
    }
}

int sum(int **array, int row, int column, int &evenSum, int &oddSum)
{
    if (row >= 0) {
        sumrow(array[row], column, evenSum, oddSum);
        sum(array, row - 1, column, evenSum, oddSum);
    }
    return 0;
}
发布评论

评论列表(0)

  1. 暂无评论