0

This is the text of the program that I need to write: Write a function "ExtractElements" with two parameters, the first of which is a vector of integers (type "int"), and the second a logical value "true" or "false". As a result, the function should return a new vector consisting of those elements whose sum of digits is even or odd, depending on whether the second parameter has the value "true" or "false". The elements in the newly created vector should be in the same relative order as they were in the original vector. Use the written function in a test program that asks to first enter the natural number , and then the elements of the vector "a" which has of integer elements. The program should then create two new vectors "b" and "c", and write all the numbers from the vector "a" with an even sum of digits into the vector "b", and all the numbers from the vector "a" with an odd sum into the vector "c" by the sum of the digits. Finally, the program should print numbers with an even number of digits in one line, and numbers with an odd number of digits in the second line (it is possible for a line to remain empty, if there is not a single number with the required property). My program just prints zero on screen instead of the required vectors.

#include<iostream>
#include<vector>
#include<cmath>

std::vector<int> ExtractElements(std::vector<int>v, bool p)
{
    std::vector<int>even,odd;
    int sum=0,m;
    for(int i=0;i<v.size();i++)
    {
        while(int(i))
        {
            m=i%10;
            sum=sum+m;
            i=i/10;
        }
        if(sum%2==0) 
        {
            even.push_back(i);
        }
        else odd.push_back(i);
    }
    if(p=true) return even;
    if(p=false) return odd;
}
int main()
{
    int n;
    bool parity=true;
    std::vector<int>a,b,c;
    std::cout<<"How many elements do you want to input: ";
    std::cin>>n;
    std::cout<<"Enter elements: ";
    for(int i=0;i<n;i++)
    {
        std::cin>>i;
        a.push_back(i);
    }
    b=ExtractElements(a,parity);
    parity=false;
    c=ExtractElements(a,parity);
    for(int i=0;i<b.size();i++)
    {
        std::cout<<b[i];
    }
    for(int i=0;i<c.size();i++)
    {
        std::cout<<c[i];
    }
    return 0;
}
ginasi
  • 1
  • 2

0 Answers0