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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 10;
void input(const int a[], int size);
int getTotal(const int a[], int size);
double getAvg(const int a[], int size);
double getEvenAvg(const int a[], int size);
double getOddAvg(const int a[], int size);
int main()
{
ifstream fin;
int a[10] = {4,6,8,10,2,3,5,7,9,11}, b[10];
char ans, file_name[16];
cout << "This Program will Calculate the Total, Average, \n"
<< "Average of Odd numbers, and Average of Even numbers all from a list of 10 integers from file or this Program.\n";
cout << "\nDo You want the list of 10 integers provided by the program? (Type 'Y' or 'y')\n";
cin >> ans;
if(ans =='Y' || ans == 'y')
{
input(a, SIZE);
}
else
{
cout << "You Chose to Calculate using the 10 integers from a file!\n";
cout << "Please Enter the name of the Input File? (Maximun of 15 characters):(ex. test.txt)\n";
cin >> file_name;
fin.open(file_name);
if(fin.fail())
{
cout << "Input File Opening Failed.\n";
system("PAUSE");
exit(1);
}
while(fin >> b[10])
{
fin >> b[10];
getTotal(b, SIZE);
getAvg(b, SIZE);
getEvenAvg(b, SIZE);
getOddAvg(b, SIZE);
}
cout << "The Total sum of the integer from the file is: " << getTotal(b, SIZE) << endl;
cout << "The Average of the integer from the file is: " << getAvg(b, SIZE) << endl;
cout << "The Average of Even Numbers from the file is: " << getEvenAvg(b, SIZE) << endl;
cout << "The Average of Odd Numbers from the file is: " << getOddAvg(b, SIZE) << endl;
fin.close();
}
cout << "Program Terminated!\n";
system("PAUSE");
return 0;
}
void input(const int a[], int size)
{
cout << "You Chose to Calculate using the integers provided by the program!\n";
getTotal(a, SIZE);
getAvg(a, SIZE);
getEvenAvg(a, SIZE);
getOddAvg(a, SIZE);
cout << "The Total sum of the integer is: " << getTotal(a, SIZE) << endl;
cout << "The Average of the integer is: " << getAvg(a, SIZE) << endl;
cout << "The Average of Even Numbers is: " << getEvenAvg(a, SIZE) << endl;
cout << "The Average of Odd Numbers is: " << getOddAvg(a, SIZE) << endl;
}
int getTotal(const int a[], int size)
{
int total = 0;
for(int i=0; i < size; i++)
{
total+= a[i];
}
return total;
}
double getAvg(const int a[], int size)
{
double total = 0;
for(int i=0; i < size; i++)
{
total+= a[i];
}
return (total/size);
}
double getEvenAvg(const int a[], int size)
{
int count = 0;
double total=0;
for(int i=0; i< size; i++)
{
if(a[i] % 2==0)
{
total+= a[i];
count++;
}
}
return (total/count);
}
double getOddAvg(const int a[], int size)
{
double total = 0;
int count = 0;
for(int i=0; i< size; i++)
{
if(a[i] %2 != 0)
{
total+=a[i];
count++;
}
}
return (total/count);
}
|