Ask Question
Today, 03:02

Input numbers and segregate them by placing their values into even or odd vectors based upon their value being even or odd. Then fill a 2-D array with the first column containing the contents of the even vector and the second column containing the odd vector. Output the result and display the contents of the vectors and array.

+2
Answers (1)
  1. Today, 04:32
    0
    The solution code is written in Python

    input_numbers = [5, 7, 1, 12, 9, 34, 22, 97, 112] evenVec = [] oddVec = [] for x in input_numbers: if x % 2 = = 0: evenVec. append (x) else: oddVec. append (x) num2D = [evenVec, oddVec] print (num2D)

    Explanation:

    Let's create a sample input numbers that mixed with odd and even values (Line 1)

    Next, create evenVec and oddVec variables to hold the even and odd numbers from the input_numbers (Line 3 - 4).

    Create a for-loop to traverse through the input_numbers and check if current number, x, is divisible by 2 using the modulus operator %. If so, x % 2 will be evaluated to 0 and append the current x to evenVec or else append to oddVec (Line 6 - 10).

    Next, create a variable num2D to hold the two dimensional arrays with first column is evenVec and second column is oddVec (Line 12).

    When printing the num2D (Line 14), we shall get

    [[12, 34, 22, 112], [5, 7, 1, 9, 97]]
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Input numbers and segregate them by placing their values into even or odd vectors based upon their value being even or odd. Then fill a 2-D ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers