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 147 148 149 150 151 152
|
#include <iostream>
#include <fstream>
using namespace std;
void oddOrEven(int[], int[], int[], ifstream &, int &, int &, int &);
void calcData(int[], int[], double &, double &, double &, double &, int &, int &);
int main()
{
int i, a, b;
double total, average, max, min;
int array[75] = { 0 };
int evenArray[50] = { 0 };
int oddArray[50] = { 0 };
ifstream fileIn;
fileIn.open("file.txt");
if (!fileIn) {
cout << "\nError, could not open file.\n";
}
oddOrEven(array, evenArray, oddArray, fileIn, i, a, b);
calcData(evenArray, oddArray, total, average, min, max, a, b);
fileIn.close();
return 0;
}
void oddOrEven(int array[], int evenArray[], int oddArray[], ifstream & fileIn, int &i, int &a, int &b)
{
i = 0;
a = 0;
b = 0;
while (fileIn >> i) {
fileIn >> array[i];
if (array[i] % 2 == 0) {
evenArray[a] = array[i];
a++;
}
else {
oddArray[b] = array[i];
b++;
}
}
cout << "\nEven Numbers: \n\n";
for (int z = 0; z < a; z++) {
cout << evenArray[z] << " ";
}
cout << endl;
cout << "\nOdd Numbers: \n\n";
for (int x = 0; x < b; x++) {
cout << oddArray[x] << " ";
}
cout << endl;
}
void calcData(int evenArray[], int oddArray[], double &total, double &average, double &min, double &max, int &a, int &b)
{
//Find min, max, total and average for Even Array.
for (int i = 0; i < a; i++) {
total += evenArray[i];
if (i == 0) {
min = evenArray[i];
max = evenArray[i];
}
if (evenArray[i] < min)
min = evenArray[i];
if (evenArray[i] > max)
max = evenArray[i];
}
average = total / a;
cout << "\nMinimum is " << min;
cout << "\nMaximum is " << max;
cout << "\nTotal is " << min;
cout << "\nAverage is " << average;
//Numbers higher than the average for Even Array:
cout << "\n\nNumbers greater than the average for evenArray: \n\n";
//Re-Initialize total and average to 0 for next loop;
average = 0;
total = 0;
for (int k = 0; k < a; k++) {
if (evenArray[k] > average / 2)
cout << evenArray[k] << " ";
}
cout << endl;
//Find min, max, total and average for Odd Array.
for (int i = 0; i < a; i++) {
total += oddArray[i];
if (i == 0) {
min = oddArray[i];
max = oddArray[i];
}
if (oddArray[i] < min)
min = oddArray[i];
if (oddArray[i] > max)
max = oddArray[i];
}
average = total / b;
cout << "\nMinimum is " << min;
cout << "\nMaximum is " << max;
cout << "\nTotal is " << min;
cout << "\nAverage is " << average;
//Numbers higher than the average for Odd Array:
cout << "\n\nNumbers greater than the average for oddArray: \n\n";
for (int k = 0; k < b; k++) {
if (oddArray[k] > average / 2)
cout << oddArray[k] << " ";
}
cout << endl;
}
|