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
|
void classify(int n, int& z, int& o, int& e)
{
if (n == 0)
{
z++;
}
else
{
switch (n % 2)
{
case 0:
e++;
break;
case 1:
case -1:
o++;
break;
}
}
}
void counting(std::ifstream& file, int n, int& countr)
{
file >> n;
if (n > 0 || n < 0)
{
countr++;
}
}
int main()
{
int zero = 0, odd = 0, even = 0, count = 0, sum = 0;
int num = 0;
double average;
std::ifstream ifile;
std::ofstream ofile;
ifile.open("input.txt");
ofile.open("output.txt");
while (!ifile.eof())
{
counting(ifile, num, count);
}
std::cout << count; //shows that there are 10 numbers in the file
int arr[100];
for (int i = 0; i < count; i++)
{
ifile >> num;
std::cout << num;
classify(num, zero, odd, even);
arr[i] = num;
}
for (int i = 0; i < count; i++)
{
sum = sum + arr[i];
}
average = static_cast<double>(sum / count);
ofile << "Number of zeros: " << zero << "\t odd: " << odd << "\teven: " << even << "\nSum = " << sum << "\nAverage = " << average;
ifile.close();
ofile.close();
|