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 | Show 7 more comments2 Answers
Reset to default 1As 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;
}
column
. Then you call another function, passign the "default" originalcolumn
value as an argument, and this new function does the recursive calculations needed. – Some programmer dude Commented Feb 5 at 7:270
... – Jarod42 Commented Feb 5 at 8:23