#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
//This function reads the integers from the file
void readFile(int a[], int arraySize);
//This function calculates the number of odd even and total numbers
void calculation(int a[], int arraySize);
//This function description the program to user
void descriptionToUser();
//constant declaration
const int arraySize = 10;
int main()
{
//This function is a description the program to user
descriptionToUser();
//variable declaration
string ans;
int intergerArray[arraySize] = { 3,-2,0,1,0,2,0,5,0,2 };
//display to user whether user want to read the array from file
cout << "\nPress \"1\" to calculate from the array{ 3,-2,0,1,0,2,0,5,0,2}\n"
<< "Press \"2\" to read the number from file\n";
cin >> ans;
if (ans == "1")//if-else loop to get whether the user chooses to read integers from file
{
cout << "\nYou choose to do the calculation from the array{ 3,-2,0,1,0,2,0,5,0,2}\n";
calculation(intergerArray, arraySize);
}
else if (ans == "2")
{
//This function reads the integers from the file
readFile(intergerArray, arraySize);
//This function calculates the number of odd even and total numbers
calculation(intergerArray, arraySize);
}
else
//Invalid input
cout << "invalid input! Please only press \"1\" or \"2\"\n";
}
void descriptionToUser()//This function description the program to user
{
//display to user what is this program use for
cout << "This program reads a list of 10 integers from a file or use a list defined by the programmer (hard coded)."
<<"Then the program calculates the total and average for the following numbers:"
<<"all numbers, odd numbers, and even numbers,"
<<"and displays the results to screen.\n";
}
void readFile(int a[], int arraySize)//this function reads the integers from the file
{
//variable declaration
ifstream fin;
ofstream fout;
char fileName[16];
//display to user if they choose to read the list from file
cout << "You choose read the list from file\n"
<< "Enter your file name";
//input file name
cin >> fileName;
//open the input file
fin.open(fileName);
if (fin.fail())//If-else loop to make sure whether the file exist
{
cout << "input file opening failed\n";
exit(1);
}
for (int i = 0; i < arraySize; i++)//for loop read the array list from file
{
fin >> a[i];
}
//close the input file
fout.close();
}
void calculation(int a[], int arraySize)//This function calculates the number of odd even and total numbers
{
//variable declaration
double average, oddAverage, evenAverage;
//initialize variable
double sum = 0;
double evenSum = 0;
double oddSum = 0;
int oddCount = 0;
int evenCount = 0;
int totalCount = 0;
for (int i = 0; i < arraySize; i++)//for loop to
{
//calculate value of sum
sum += a[i];
//total count increment
totalCount++;
if (a[i] % 2 == 0)//if-else loop to make sure wheter the number is even number
{
//even numbers count increment
.
1. #include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
//This function reads the integers from the file
void readFile(int a[], int arraySize);
//This function calculates the number of odd even and total
numbers
void calculation(int a[], int arraySize);
//This function description the program to user
void descriptionToUser();
//constant declaration
const int arraySize = 10;
int main()
{
//This function is a description the program to user
descriptionToUser();
//variable declaration
string ans;
int intergerArray[arraySize] = { 3,-2,0,1,0,2,0,5,0,2 };
//display to user whether user want to read the array from
file
cout << "nPress "1" to calculate from the array{ 3,-
2. 2,0,1,0,2,0,5,0,2}n"
<< "Press "2" to read the number from filen";
cin >> ans;
if (ans == "1")//if-else loop to get whether the user
chooses to read integers from file
{
cout << "nYou choose to do the calculation from the
array{ 3,-2,0,1,0,2,0,5,0,2}n";
calculation(intergerArray, arraySize);
}
else if (ans == "2")
{
//This function reads the integers from the file
readFile(intergerArray, arraySize);
//This function calculates the number of odd even and
total numbers
calculation(intergerArray, arraySize);
}
else
//Invalid input
cout << "invalid input! Please only press "1" or
"2"n";
}
void descriptionToUser()//This function description the program
to user
{
//display to user what is this program use for
cout << "This program reads a list of 10 integers from a
file or use a list defined by the programmer (hard coded)."
<<"Then the program calculates the total and average
for the following numbers:"
<<"all numbers, odd numbers, and even numbers,"
3. <<"and displays the results to screen.n";
}
void readFile(int a[], int arraySize)//this function reads the
integers from the file
{
//variable declaration
ifstream fin;
ofstream fout;
char fileName[16];
//display to user if they choose to read the list from file
cout << "You choose read the list from filen"
<< "Enter your file name";
//input file name
cin >> fileName;
//open the input file
fin.open(fileName);
if (fin.fail())//If-else loop to make sure whether the file
exist
{
cout << "input file opening failedn";
exit(1);
}
for (int i = 0; i < arraySize; i++)//for loop read the array
list from file
{
fin >> a[i];
}
//close the input file
fout.close();
}
4. void calculation(int a[], int arraySize)//This function calculates
the number of odd even and total numbers
{
//variable declaration
double average, oddAverage, evenAverage;
//initialize variable
double sum = 0;
double evenSum = 0;
double oddSum = 0;
int oddCount = 0;
int evenCount = 0;
int totalCount = 0;
for (int i = 0; i < arraySize; i++)//for loop to
{
//calculate value of sum
sum += a[i];
//total count increment
totalCount++;
if (a[i] % 2 == 0)//if-else loop to make sure wheter
the number is even number
{
//even numbers count increment
evenCount++;
//calculate sum of even numeber
evenSum += a[i];
}
else//if-else loop to make sure wheter the number is
odd number
{
//odd numbers count increment
oddCount++;
//calculate sum of odd numeber
5. oddSum += a[i];
}
}
////calculate the average of all number
average = sum / totalCount;
//calculate all the odd number average
oddAverage = oddSum / oddCount;
//calculate all the even number average
evenAverage = evenSum / evenCount;
//let the result in currency format
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//display all result output to the user
cout << "The total sum of the 10 number is: " << sum <<
endl
<< "The total average of the 10 numbers is: " <<
average << endl
<< "The sum of the even numbers is: " << evenSum
<< endl
<< "The average of the even numbers is: " <<
evenAverage << endl
<< "The sum of the odd numbers is: " << oddSum <<
endl
<< "The average of the odd numbers is: " <<
oddAverage << endl;
}
6. Part Two:
I always think about it and know that I was sophomoric. I was
overconfident and conceited. There was, however, a better side
of me. I was very diligent, organized, and hardworking. I was
mostly reserved…and many people thought of me as intelligent.
The day put to test my abilities in a general, and what I would
call an unfairly fate-filled manner. I…we had longed for the day
to come for the past three months. Here it came. This was going
to be my last class as a sophomore, there is a final semester
examination.
Comparatively, the class activity was simpler for me compared
to the previous ones. All the other six members of Group B,
except me, were already seated in the classroom. They were
patiently waiting for me to arrive because I was their only hope.
As the general rule, the international law lecturer had given
each of the six groups a question to research work on as part of
the continuous assessment test (CAT). We were then to choose
one member to do a class presentation while others provided a
supportive role. Unfortunately for me, I was both the group
leader and the presenter.
Now, most students never take group work that seriously.
Everyone took a component of the question, read, poorly
scribbled something on it, and passed it for me for compilation.
The night before, I had to stay awake the whole night to work
on the final draft to send to the members for approval, awaiting
the presentation at 10.00 am on a fateful day – the 7th day of
May 2017. Since I knew that I still had about 50 minutes before
the start of the presentation, I foolishly went back to sleep, and
that is how my trouble began.
The next thing I heard was my phone ringing at exactly 10.02
am. On the line was Becky.
‘‘Where are you?’’ He desperately enquired.
‘‘I am coming?’’ I half-heartedly responded. I took my bag and
started to run.
As I approached the classroom, I could see Becky, Sammy,
7. Jorge, and Stephen seated on one side of the corner through the
half-open door. They stared at me with much concern and
desperation at the same time. I could never tell whether they
sympathized with me or them. I never cared anyway. The
classroom was dirty, with papers sticking everywhere on the
wall. I picked my way through the empty spaces and took a seat
near Becky. I could hear scanty laughter in the background. I
did not care either. The humid airs swept through my nostrils
and swarm my lungs and caused my veins to tingle as if they
were being chocked. I did not care.
‘‘Why are you late? You know you are late, and we needed to
discuss a few points’’ Becky inquired again. I thought that her
concerns were inconsequential. I was confident.
‘‘Group B…it is your time, ten minutes,’’ the strong voice
swept through the room.
As I took to the front of the class, I could feel warm rolls of
sweat stream from my face and watered my shirt. My armpits
and pants were already soaked. Stage fright and anxiety can
destroy a student’s performance. Everyone stared at me in awe.
It was the greatest and most memorable day on campus.
2
55 5 90 0 1 11 -8