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

  alternate, c++

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

Source: Windows Questions C++

LEAVE A COMMENT