Classifying Numbers

Hello all. I am having trouble with my homework problem. Below I've attached the requirements and the code I have so far. Any help would be appreciated. Thank you.

a. Data to the program is input from a file (Problem01Data.txt) of an
unspecified length; 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.
c. Modify the 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.
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 and average of the numbers.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include <iostream> 
#include <iomanip>

using namespace std;

const int N = 20;   

    
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount, 
                    int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

int main ()
{
        
    int counter; 
    int number; 
    int zeros;   
    int odds;    
    int evens;   

    initialize(zeros, odds, evens);                

    cout << "Please enter " << N << " integers."
         << endl;                                   
    cout << "The numbers you entered are: "
         << endl;

    for (counter = 1; counter <= N; counter++)    
    {
        getNumber(number);                          
        cout << number << " ";                      
        classifyNumber(number, zeros, odds, evens); 
    }

    cout << endl;

    printResults(zeros, odds, evens);               

    return 0;
}

void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
    zeroCount = 0;
    oddCount = 0;
    evenCount = 0;
}

void getNumber(int& num)
{
    cin >> num;
}

void classifyNumber(int num, int& zeroCount, int& oddCount,
                    int& evenCount)
{
    switch (num % 2)
    {
    case 0: 
        evenCount++;  
        if (num == 0)
            zeroCount++;  
        break;
   case 1: 
   case -1: 
       oddCount++;
   } 
} 

void printResults(int zeroCount, int oddCount, int evenCount)
{ 
    cout << "There are " << evenCount << " evens, "
         << "which includes " << zeroCount << " zeros"
         << endl;

    cout << "The number of odd numbers is: " << oddCount
         << endl;
} 
Topic archived. No new replies allowed.