Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ program named evenodd.cpp that reads in a text file containing numb

ID: 3594453 • Letter: C

Question

Create a C++ program named evenodd.cpp that reads in a text file containing numbers (separated by newline characters) and organizes them into two vectors, one containing even numbers, and the other containing odd numbers. The output of this program must print the elements of each vector and its size A sample text file and a sample run of the program is provided below: numbers.txt (input text file //sample execution Even numbers: 4, 6, 2, 8 Even number count:4 Odd numbers: 1, 3, 5, 9, 7 Odd number count: 5 Press any key to continue.. . The use of vectors and some of their member functions is a requirement for this lab: push back) pop back0 size0 emptyO

Explanation / Answer

#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main() {
//odd and even vectors
vector <int> odd;
vector <int> even;
// open file
ifstream inputFile("numbers.txt");

// check if open
if (inputFile) {
int value;

// read the elements in the file into a vector
while ( inputFile >> value ) {

//push even values in even vector and odd values in odd vector
if(value%2 == 0)
even.push_back(value);
else
odd.push_back(value);
}
}

//print even numbers and count
vector <int> :: iterator i;
cout<<"Even Numbers: ";
for (i = even.begin(); i != even.end(); ++i)
cout << *i <<" ";
cout<<endl;
cout<<"Even number count: "<<even.size()<<endl;


//print odd numbers and count
cout<<"Odd Numbers: ";
for (i = odd.begin(); i != odd.end(); ++i)
cout << *i <<" ";
cout<<endl;
cout<<"Odd number count: "<<odd.size()<<endl;
return 0;

}