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
|
#include <fstream>
#include <iomanip>
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void pulldata(int , int , int , int&, int&, int&, int&, int&, int&, float&, float&);
void output(int, int, int, int, int, int);
ifstream inputfile;
ofstream outputfile("C:\\Temp\\Lab7B Evens and Odds.txt");
int main()
{
int evenmax=0, evenmin=0, oddmax=0, oddmin=0, currentnum=0;
float oddavg=0, evenavg=0;
int odd[50] = { 0 }, even[50] = { 0 }; //arrays
int evencount=0, oddcount=0;
string openprompt;
inputfile.open("C:\\Temp\\Lab7 Input.txt");
if (!inputfile)
{
cout << "There was an error opening the Input File. Please verify it is in the correct location" << endl << "Would you like to open the root folders? Yes or No" << endl;
cin >> openprompt;
if (openprompt == "Yes" || openprompt == "yes" || openprompt == "y" || openprompt == "Y")
{
system("start explorer C:\\Temp");
}
exit(0);
}//end of input file check
cout << "Reading input file, please wait";
do
{
pulldata(odd[50], even[50], currentnum, evenmax, oddmax, evencount, oddcount, evenmin, oddmin, evenavg, oddavg);
output(odd[50], even[50], evencount, oddcount, oddavg, evenavg);
} while (inputfile.eof());
inputfile.close();
outputfile.close();
system("pause");
}
void pulldata(int odd[], int even[], int currentnum, int &evenmax, int &oddmax, int & evencount, int &oddcount, int &evenmin, int &oddmin, float&evenavg, float&oddavg)
{
int count;
int oddtot=0, eventot=0;
for (count = 0; count < 50; count++)
{
inputfile >> currentnum;
if (currentnum % 2 == 0)//check to see if the number is even. Zero is an even number.
{
even[evencount] = currentnum;
evencount++;
eventot = eventot + currentnum;
if (currentnum < evenmin)
{
evenmin = currentnum;
}
if (currentnum > evenmax)
{
evenmax = currentnum;
}
}
else
{
odd[oddcount] = currentnum;
oddcount++;
oddtot = oddtot + currentnum;
if (currentnum < oddmin)
{
oddmin = currentnum;
}
if (currentnum > oddmax)
{
oddmax = currentnum;
}
}
}//end of odd and even array assignment
//generate avg of evens and odds
evenavg = eventot / evencount;
oddavg = oddtot / oddcount;
}
void output(int odd[], int even[], int evencount, int oddcount, int oddavg, int evenavg)
{
outputfile << "Total number of even numbers is " << evencount << " and total number of odd numbers is " << oddcount;
}
|