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 153 154 155 156
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void evenOdd(ifstream &, int[], int[], int[]);
void align(int[], int);
float Sum(int[], int);
int avg(int[], float);
int main()
{
ifstream infile;
ofstream evenfile;
ofstream oddfile;
int evenArray[50] = {0};
int oddArray[50] = {0};
int Array[50] = {0};
int num = 0;
int max = 0;
int min = 0;
int count = 0;
int sum = 0;
int average = 0;
cout << "Start of program." << endl;
cout << "Opening infile..." << endl;
infile.open("array.txt");
if (!infile)
{
cout << "Cannot open file, terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
cout << "Opening evenfile..." << endl;
evenfile.open("even.txt");
if (!evenfile)
{
cout << "Cannot open file, terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
cout << "Opening oddfile..." << endl;
oddfile.open("odd.txt");
if (!oddfile)
{
cout << "Cannot open file, terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
while (!infile.eof())
{
evenOdd(infile, evenArray, oddArray, Array);
cout << "Even Sum:\t" << Sum(evenArray, 50) << endl;
cout << "Odd Sum:\t" << Sum(oddArray, 50) << endl;
cout << "Numbers over even:" << avg(evenArray, average) <<endl;
cout << "Numbers over odd:" << avg(oddArray, average)<< endl;
cout << endl;
}
cout << endl;
cout << "End of program." << endl;
infile.close();
evenfile.close();
oddfile.close();
return 0;
}
/***************************************************************/
void evenOdd(ifstream & infile, int evenArray[], int oddArray[], int array[])
{
int a = 0;
int b = 0;
for (int i = 0; infile >> array[i]; i++)
{
if (array[i] % 2 == 0)
{
evenArray[a] = array[i];
a++;
}
else
{
oddArray[b] = array[i];
b++;
}
}
cout << endl;
cout << "Even Array" << endl;
align(evenArray, a);
cout << "Odd Array" << endl;
align(oddArray, b);
}
/***************************************************************/
void align(int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << setw(3) << array[i];
if ((i + 1) % 10 == 0)
cout << endl;
}
cout << endl;
}
/***************************************************************/
float Sum(int array[], int size)
{
int max = array[0];
int min = array[0];
for (int i = 0; i < 50; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
float sum = array[0];
float average;
for (int i = 1; i < size; i++) {
sum += array[i];
}
average = sum / size;
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "Min: " << min << endl;
cout << "Max: " << max << endl;
return average;
}
/***************************************************************/
int avg(int array[], float average)
{
while (array[0] > average)
return array[0];
}
|