Create a function that performs an even-odd transform to an array, n times. Each even-odd transformation:
- Adds two (+2) to each odd integer.
- Subtracts two (-2) from each even integer.
Examples:
evenOddTransform([3, 4, 9], 3) ➞ [9, -2, 15]
// Since [3, 4, 9] => [5, 2, 11] => [7, 0, 13] => [9, -2, 15]
evenOddTransform([0, 0, 0], 10) ➞ [-20, -20, -20]
evenOddTransform([1, 2, 3], 1) ➞ [3, 0, 5]
It seems to me that in order to do this challenge, I will need to define a new function evenOddTransform
which takes an array and an integer as input. Within evenOddTransform
I will also need to define a for
loop which scans to the size of the passed array (for (i = 0, i < sizeofarray, i=i+2)
in pseudocode).
However, C++ is apparently unable to calculate the size of the array when it's passed to a function. In other words, I need to calculate the size of the array first and then pass that size together the array to evenOddTransform
. But if I do that, then evenOddTransform
needs at least three inputs, while the examples only take two inputs.
How should I approach this coding challenge?