In: Computer Science
Write a C++ program, all the answers so far on this site have been wrong when I compiled them.. Desired outputs at the end.
--In this exercise, you are to modify the Classified Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (terminal screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements:
a) Data to the program is input from a file of an unspecified length named Ch06_Ex20Data.txt; that is , the program does not know in advance how many numbers are in the file.
b) Save the output of the program in a file named Ch06_Ex20Out.txt.
c) Modify the void function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends the number read to the function main. Print only 10 numbers per line. Assume all numbers are between -9999 and 9999.
d) Have the program find the sum and average of the numbers.
e) Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum of the numbers
Outputs in the program should look like this:
Processing Data
There are 27 evens, which includes 12 zeros
Total number of odds are: 16
The sum of numbers = 558
The average is 12
press any key to exit.
The OutFile should look like this
E:\FolderName\Lab\Ch06_Ex20> type Ch06_Ex20Out.txt
43 67 82 0 35 28 -64 7 -87 0
0 0 0 12 23 45 7 -2 -8 -3
-9 4 0 1 0 -7 23 -24 0 0
12 62 100 101 -203 -340 500 0 23 0
54 0 76
There are 27 evens, which includes 12 zeros
Total number of odds are: 16
The sum of numbers = 558
The average is 12
E:\FolderName\Lab\Ch06_Ex20>
// do comment if any problem arises
//code
#include <iostream>
#include <fstream>
using namespace std;
int Size = 0;
int odds = 0;
int evens = 0;
int zeroes = 0;
// read numbers from input file and print then then return in form of given array
void getNumber(ifstream &input, ofstream &output, int *numbers)
{
int i = 0;
int n = 0;
// read data and write to output file
while (input >> numbers[i])
{
Size++;
if (n == 10)
{
output << endl;
n = 0;
}
if (numbers[i] % 2 == 0)
{
if (numbers[i] == 0)
zeroes++;
evens++;
}
else
{
odds++;
}
output << numbers[i] << " ";
n++;
i++;
}
}
// this function returns sum of numbers in array
int find_sum(int numbers[])
{
int sum = 0;
for (int i = 0; i < Size; i++)
{
sum += numbers[i];
}
return sum;
}
// this function returns average of numbers in array
int average(int numbers[])
{
return find_sum(numbers) / Size;
}
// this function prints result
void printResult(int sum, int avg)
{
cout << "There are " << evens << " evens, which includes " << zeroes << " zeroes\n";
cout << "Total number of odds are: " << odds << endl;
cout << "The sum of numbers = " << sum << endl;
cout << "The average is " << avg << endl;
}
int main()
{
// open input file
ifstream input("Ch06_Ex20Data.txt");
// open output file
ofstream output("Ch06_Ex20Out.txt");
// if input file has not been opened
if (!input.is_open())
{
cout << "Can't open input file!";
return 0;
}
int numbers[1000];
cout << "Processing Data\n";
getNumber(input, output, numbers);
int sum = find_sum(numbers);
int avg = average(numbers);
// write results to output file
output << endl;
output << "There are " << evens << " evens, which includes " << zeroes << " zeroes\n";
output << "Total number of odds are: " << odds << endl;
output << "The sum of numbers = " << sum << endl;
output << "The average is " << avg << endl;
printResult(sum, avg);
input.close();
output.close();
}
Output:
output file: