Ask Question

9.18 LAB: Even/odd values in a vector Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Ex: If the input is 5 2 4 6 8 10, the output is: all even Ex: If the input is 5 1 3 5 7 9, the output is: all odd Ex: If the input is 5 1 2 3 4 5, the output is: not even or odd Your program must define and call the following two functions. IsVectorEven returns true if all integers in the array are even and false otherwise. IsVectorOdd returns true if all integers in the array are odd and false otherwise. bool IsVectorEven (vector myVec) bool IsVectorOdd (vector myVec)

+5
Answers (1)
  1. 29 January, 09:55
    0
    This program is written in C++. The explanation of code is given below in explanation section.

    Explanation:

    #include

    #include

    #include

    #include

    using namespace std;

    //function to check vector element is even or not

    bool IsEven (int n) {

    return (n % 2) = = 0;

    }

    //function to check vector element is odd or not

    bool IsOdd (int n)

    {

    if (n%2!=0) / / detection of odd value

    {

    return 1;

    }

    }

    bool IsVectorEven (vector vect) / / check even number in vector

    {

    auto countEven = count_if (vect. begin (), vect. end (), IsEven); //count total even numbers in vector

    int size = vect. size (); //count total number in vector/check vector size

    if (countEven==size) / /all counted even number to size of vector

    {

    cout<<"/nAll even"; //display the message

    //return 1;

    }

    else

    {

    cout<<"/nnot even or odd"; //else display the message

    / / return 0;

    }

    }

    bool IsVectorOdd (vector vect) / /check odd vector

    {

    auto countOdd = count_if (vect. begin (), vect. end (), IsOdd); / / count total odd number in vector

    int size = vect. size (); / / count total number in vector

    if (countOdd==size) / / if vector size is equal to vector counted odd number

    {

    cout<<"/nAll odd"; / / then vector has all odd number/elements

    //return 1;

    }

    else

    {

    cout<<"/nnot even or odd"; //else has odd and even numbers

    //return 0;

    }

    }

    int main ()

    {

    vector vec{ 4, 2, 4, 6, 8, 10}; //declaration of a vector

    IsVectorEven (vec); //check even number in vector

    IsVectorOdd (vec); //check odd number in vector.

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “9.18 LAB: Even/odd values in a vector Write a program that reads a list of integers, and outputs whether the list contains all even ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers