Modular program utilizing array with at least 3 functions

I am completely lost with this assignment. I have been searching the internet and forums trying to piece together something that resembles a code but I am completely stumped

I need to grab numbers from a text file (5 rows 10 columns of numbers), then store the even numbers in one array and the odd in another. Then determine the average, highest, lowest for each array.
I then have to output the contents of each array, the high, low, average for each and a listing of the values higher than the average in one text file.

I MUST write one function to find the highest, lowest, and average for an array and then use that function for both the even and odd arrays. I also need one function to output numbers higher than the average and use this once for the even and once for the odd arrays.


This is what I have so far. Please be kind its a train wreck, I know.

Thanks in advance


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

//Functions
void openFILES (ifstream&, ofstream&);
int calcEVEN (int [], double&, int&, int&, ofstream&);
int calcODD (int [], double&, int&, int&, ofstream&);

const int SIZE(1000);

int main ()
{

//variables and Arrays
    ifstream getData;
    ifstream outdata ("outFile.txt");
    ofstream outFile;
    double data, remainder;
    int rand_NUM [SIZE];
    string line;
    int count_all = 1;
    int even = 0;
    int odd = 0;
    int even_NUM [SIZE];
    int odd_NUM [SIZE];
    double even_avg;
    int total_even = 0;
    int total_odd = 0;
    double odd_avg;
    int low, high;

    openFILES (getData, outFile);

    while (getData >> data)
    {
        //This is where you can see each entry
        outFile << data << '\t' << endl;
        rand_NUM [count_all] = data;
        remainder = rand_NUM [count_all] % 2;
        count_all += 1;

        if (remainder == 0)
        {
            calcEVEN (even_NUM, data, even, total_even, outFile);
        }
        else
        {
            calcODD (odd_NUM, data, odd, total_odd, outFile);
        }
    
    }

    outFile << "low = " << low << '\t';
    outFile << "high = " << high << '\t';
    even_avg = double (total_even) / even;
    outFile << even_avg << " even = " << even << " sum = " << total_even;

    odd_avg = double (total_odd) / odd;
    outFile << odd_avg << " odd = " << odd << " sum = " << total_odd;



    cout << "File Completed.\n" << endl;

    //Used to read the output file onto the user screen
    if (outdata.is_open())
    {
    int temp = 1;
        while (getline (outdata,line))
        {

        cout << line << "test " << rand_NUM[temp] << endl;
        temp++; 

        }
    }
    else 
        cout << "Unable to open file." << endl;
    

    getData.close();
    outFile.close();
    outdata.close();
    
    return 0;
}
/******************************************************************************
******************************************************************************/
void openFILES (ifstream& getData, ofstream& outFile, ofstream& evenFile, ofstream& oddFile)
{
    getData.open ("dataFile.txt");
    outFile.open ("outFile.txt");
    

    if (!getData)
    {
        cout << "Cannot open file, terminating program." << endl;
        exit(0);
    }

    if (!outFile)
    {
        cout << "Cannot open file, terminating program." << endl;
        exit(0);
    }
    
}
/******************************************************************************
******************************************************************************/
int calcEVEN (int evenNUM [], double& evenData, int& count, int& total, ofstream& outFile)
{
    int high, low, lowest, highest;
    int data;

    evenNUM [count] = evenData;
    evenFile << evenNUM [count] << endl;

    if (evenNUM [count] >= evenData)
    {
        high = evenNUM [count];
    }

    outFile << "high = " << high << endl;
        
    total = total + evenNUM [count];

    count++;

    return total;
}
/******************************************************************************
******************************************************************************/
int calcODD (int oddNUM [], double& oddData, int& count, int& total, ofstream& outFile)
{
    oddNUM [count] = oddData;
    outFile << oddNUM [count] << endl;
    total = total + oddNUM [count];
    count++;

    return total;
}
Last edited on
Topic archived. No new replies allowed.