Search code examples
c++arraysalgorithmfor-loopfunction-definition

c++ calculate the sum of two array's and output a boolean '0' if result even else '1' if odd


enter image description here

I was hoping to find an alternative solution/method to mine for solving the above question given the set parameters. Essentially the way I went about it was to loop through the two arrays simultaneous and add the corresponding values. First place would have been to use a conditional operator.

#include <iostream>
#include <array>

using namespace std;

//an array of size 0 [no elements]
void myLists(int list1[], int list2[], int list3[], int size)
{
    for (int i = 0; i < size; i++)
    {
        if ((list1[i] + list2[i]) % 2 == 0) // return the modulo of the two array sums
        {
            cout << (list3[i] == true);
        }
        else
        {
            cout << (list3[i] == false);
        };
    }
}
int main()
{
    //initialize the two arrays
    int list1[5]{1, 2, 3, 4, 5};
    int list2[5]{0, 4, 6, 8, 10};
    int list3[5]{};
    int size{};

    size = sizeof(list1) / sizeof(list1[0]);

    myLists(list1, list2, list3, size);

    return 0;
}

RESULTS OF CODE:

10101


Solution

  • For starters the function should be declared like

    void myLists( const int list1[], const int list2[], int list3[], size_t size ;
    

    because neither the first parameter nor the second parameter are being changed within the function.

    The function does not set elements of the third array. As the third array is zero initialized

    int list3[5]{};
    

    then this expression list3[i] == true always evaluates to false and this expression list3[i] == false always evaluates to true.

    In fact you could write

    if ((list1[i] + list2[i]) % 2 == 0) // return the modulo of the two array sums
    {
        list3[i] = list3[i] == true;
    }
    else
    {
        list3[i] = list3[i] == false;
    };
    

    or

    list3[i] = (list1[i] + list2[i]) % 2 == 0 ? list3[i] == true : list3[i] == false;
    

    But this looks clumsy and moreover in general this approach is incorrect and can result in undefined behavior because the user can pass an uninitialized array as the third argument.

    It would be much better and correct to write

    list3[i] = ( list1[i] + list2[i] ) % 2;
    

    An alternative approach is to use the standard algorithm std::transform.

    Here is a demonstrative program.

    #include <iostream>
    #include <algorithm>
    
    void myLists( const int list1[], const int list2[], int list3[], size_t size )
    {
        std::transform( list1, list1 + size, list2, list3,
                        [] ( const auto &a, const auto &b )
                        {
                            return a % 2 ^ b % 2;
                        });
    }
    
    int main() 
    {
        const size_t size = 5; 
        int list1[size] = { 1, 2, 3, 4, 5 };
        int list2[size] = { 0, 4, 6, 8, 10 };
        int list3[size];
        
        myLists( list1, list2, list3, size );
        
        for ( const auto &item : list3 )
        {
            std::cout << item << ' ';
        }
        
        std::cout << '\n';
        
        return 0;
    }
    

    The program output is

    1 0 1 0 1
    

    In the return statement of the lambda expression there is used the logical XOR operator instead of the expression ( a + b ) % 2 to avoid integer overflow.