#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 .