Home > Software engineering >  How to approach this coding challenge?
How to approach this coding challenge?

Time:05-23

Source

Create a function that performs an even-odd transform to an array, n times. Each even-odd transformation:

  1. Adds two ( 2) to each odd integer.
  2. 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?

CodePudding user response:

@Mat is right--the first choice is to us a std::vector instead of an array.

But if (for some insane reason) you're required to use use an array instead, you can still do the job if you pass the array by reference:

template <std::size_t N>
void evenOddTransform(int (&input)[N]) {
    for (std::size_t i=0; i<N; i  ) {
        // do your thing with input[i].
    }
}

This would be called something like this:

int inputs[] = { 3, 4, 9 };
oddEvenTransform(inputs);

CodePudding user response:

I suggest that you take a std::vector<int-type> by reference and do the transformation on that instead since array sizes need to be known at compile time.

Example:

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>

void evenOddTransform(std::vector<std::intmax_t>& vec, unsigned times) {
    times *= 2;
    std::transform(vec.begin(), vec.end(), vec.begin(),
                   [&times](std::intmax_t val) {
                       if (val % 2) return val   times; // odd
                       return val - times;              // even
                   });
}

int main() {
    std::vector<std::intmax_t> vec{3, 4, 9};

    evenOddTransform(vec, 3);

    for (auto val : vec) std::cout << val << ' '; // 9 -2 15
}

CodePudding user response:

Using C 20 ranges:

#include <algorithm>
#include <vector>
#include <ranges>
#include <iostream>

template <std::ranges::forward_range Range>
void fn(Range&& rng, int n)
{
    auto fn = [n](int val) { return val   ((val%2) ? 2*n : -2*n); };
    std::ranges::transform(rng, rng.begin(), fn);
}

int main()
{
    std::vector<int> v1{3, 4, 9}, v2{0, 0, 0};
    fn(v1, 3);
    fn(v2, 10);
    std::ranges::copy(v1, std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';
    std::ranges::copy(v2, std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';
    return 0;
}
  •  Tags:  
  • c
  • Related