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

Write a function name evenNumbers that accepts an input stream and an output str

ID: 3595291 • Letter: W

Question

Write a function name evenNumbers that accepts an input stream and an output stream as parameters.

8 THE evenNumbers PROBLEM Write a function named evenNumbers that accepts accepts an input stream and an output stream as parameters. The input stream represents an input file containing a series of integers, and report various statistics about the integers. You may assume that there is at least one integer in the file. Report the total number of numbers, the sum of the numbers, the count of even numbers and the percent of even numbers. For example, if an input stream named input, opened with the file numbers.txtcontains the following text: 57 28 9 10 12 98 7 14 20 22 then the call evenNumbers (input, cout); should produce the following output: 12 numbers, sum = 214 8 evens (66.67%)

Explanation / Answer

Defined a new function that will accept an input stream and an output stream as parameters and display their count and sum and also number of even numbers. You can further use vector to perform more operations as per your requirement:




void evenNumbers(istream& inputStream,ostream& outputStream)
{
string line;
int sum_of_elements = 0;
int count = 0;
if (inputStream.is_open())
{
std::vector<int> vector;
int number;
while(inputStream >> number)
vector.push_back(number);
inputStream.close();
}
for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it)
{
sum_of_elements += *it;
count++;
}
auto n = count_if(vector.begin(), vector.end(),
[](int n) { return (n % 2) == 0; });

outputStream<<count<<" numbers, Sum = "<<sum_of_elements<<endl;
outputStream<<n<<" evens";
return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote