The file deliveryManager.cpp is used to create a program that reads in a file ca
ID: 3571315 • Letter: T
Question
The file deliveryManager.cpp is used to create a program that reads in a file called deliveryData.txt. The file deliveryData.txt contains 10 columns, the first five columns are AM delivery counts for Mon - Fri and the last five columns are PM delivery counts for Mon - Fri. The deliveryData.txt file has 10 rows in total.
Once the data has been read in the user can perform the following tasks based on selecting a choice from a menu:
1. get the average AM delivery counts
2. get the average PM delivery counts
3. get the average AM delivery counts for each day of week
4. get the average PM delivery counts for each day of week
5. quit the program
The program has been written using 10 single dimensional arrays. The program has been commented so you can follow the logic. Using 10 single dimensional arrays is very inefficient, you will notice that many lines of code are repeated.
Your task is to make the program more efficient by using multi-dimensional arrays and enums. If you want to create more functions, you are welcome to do that.
deliveryData.txt:
5 6 9 4 10 2 18 8 19 12
9 3 6 5 9 19 10 10 6 10
2 7 1 4 3 8 14 18 14 13
2 4 8 0 2 2 4 4 1 4
7 3 7 8 10 17 8 3 7 16
9 2 2 9 4 14 5 7 5 8
2 4 9 0 3 0 19 11 1 13
7 10 5 6 10 13 14 5 19 7
7 1 10 4 1 1 11 7 9 1
5 8 8 0 6 4 10 5 15 16
deliveryManager.cpp includes:
//Purpose: the following program reads in a file called deliveryData.txt
//The file deliveryData.txt contains 10 columns
//The first five columns are AM delivery counts for Mon - Fri
//The last five colums are PM delivery counts for Mon - Fri
//The data file has 10 rows
//Once the data has been read in the user can perform the following tasks:
//1. get the average AM delivery counts
//2. get the average PM delivery counts
//3. get the average AM delivery counts for each day of week
//4. get the average PM delivery counts for each day of week
//5. quit the program
//The program has been written using 10 single dimensional arrays. This is not efficient.
//Your task is to make the program more efficient using multi-dimensional arrays and enums
//If you want to create more functions, you are welcome to do that.
#include<iostream>
#include<fstream>
const int MAX_WEEKS = 10;
//Function prototype to show the user menu
void menu();
int main()
{
//Ten arrays to store delivery counts for 5 days in the week and 2 delivery periods
int monAMArray[MAX_WEEKS] = { 0 };
int tueAMArray[MAX_WEEKS] = { 0 };
int wedAMArray[MAX_WEEKS] = { 0 };
int thursAMArray[MAX_WEEKS] = { 0 };
int friAMArray[MAX_WEEKS] = { 0 };
int monPMArray[MAX_WEEKS] = { 0 };
int tuePMArray[MAX_WEEKS] = { 0 };
int wedPMArray[MAX_WEEKS] = { 0 };
int thursPMArray[MAX_WEEKS] = { 0 };
int friPMArray[MAX_WEEKS] = { 0 };
//Create input handler
std::ifstream inputHandler;
//Used to store index position for each array
int arrayCounter = 0;
//Used to store the menu selection from the user
int userSelection;
//Store the AM and PM total
int AMTotal = 0;
int PMTotal = 0;
int monAMTotal = 0;
int tueAMTotal = 0;
int wedAMTotal = 0;
int thurAMTotal = 0;
int friAMTotal = 0;
int monPMTotal = 0;
int tuePMTotal = 0;
int wedPMTotal = 0;
int thurPMTotal = 0;
int friPMTotal = 0;
//Store the AM and PM average
float AMAverage = 0.0;
float PMAverage = 0.0;
float AMMonAverage = 0.0;
float AMTueAverage = 0.0;
float AMWedAverage = 0.0;
float AMThursAverage = 0.0;
float AMFriAverage = 0.0;
float PMMonAverage = 0.0;
float PMTueAverage = 0.0;
float PMWedAverage = 0.0;
float PMThursAverage = 0.0;
float PMFriAverage = 0.0;
//Open deliveryData.txt file
inputHandler.open("deliveryData.txt");
//Check if the inputHandler failed, if so display warning and terminate program
if (!inputHandler)
{
std::cout << "ERROR: Failed to open file. Please make sure file is called deliveryData.txt and stored in the same folder as your executable." << std::endl;
inputHandler.close();
return -1;
}
//inputHandler is valid, begin reading data
else
{
//Tell the user that we are reading through the file
std::cout << "Starting file processing." << std::endl;
//Read and store the first line of data into the 10 arrays
inputHandler >> monAMArray[arrayCounter] >> tueAMArray[arrayCounter] >> wedAMArray[arrayCounter] >> thursAMArray[arrayCounter] >> friAMArray[arrayCounter] >> monPMArray[arrayCounter] >> tuePMArray[arrayCounter] >> wedPMArray[arrayCounter] >> thursPMArray[arrayCounter] >> friPMArray[arrayCounter];
while (!inputHandler.eof())
{
arrayCounter++;
//Read and store the next line of data into the 10 arrays
inputHandler >> monAMArray[arrayCounter] >> tueAMArray[arrayCounter] >> wedAMArray[arrayCounter] >> thursAMArray[arrayCounter] >> friAMArray[arrayCounter] >> monPMArray[arrayCounter] >> tuePMArray[arrayCounter] >> wedPMArray[arrayCounter] >> thursPMArray[arrayCounter] >> friPMArray[arrayCounter];
}
//Close the input handler
inputHandler.close();
//Inform user the file has been processed
std::cout << "Finished file processing." << std::endl;
//Show the user the menu and ask for input
menu();
std::cin >> userSelection;
while (userSelection < 1 || userSelection > 5)
{
//User made an error, ask for selection again
std::cout << "ERROR: Please selection again." << std::endl;
menu();
std::cin >> userSelection;
}
//User wants average of all AM deliveries
if (userSelection == 1 || userSelection == 3)
{
//Loop through all five AM arrays and sum their values
for (int x = 0; x < MAX_WEEKS; x++)
{
monAMTotal = monAMTotal + monAMArray[x];
tueAMTotal = tueAMTotal + tueAMArray[x];
wedAMTotal = wedAMTotal + wedAMArray[x];
thurAMTotal = thurAMTotal + thursAMArray[x];
friAMTotal = friAMTotal + friAMArray[x];
}
//If the user wants the AM average counts
if (userSelection == 1)
{
//Store the grand total of all AMTotal values in AMTotal
AMTotal = monAMTotal + tueAMTotal + wedAMTotal + thurAMTotal + friAMTotal;
//Print out the average by dividing AMTotal by (MAX_WEEKS * 5)
AMAverage = float(AMTotal) / float((MAX_WEEKS * 5));
std::cout << "The average for all AM deliveries is: " << AMAverage << std::endl;
}
//The user wants the AM average for each day of the week
else
{
//Create the individal day of week averages by dividing the day of week total by MAX_WEEKS
AMMonAverage = float(monAMTotal) / float(MAX_WEEKS);
AMTueAverage = float(tueAMTotal) / float(MAX_WEEKS);
AMWedAverage = float(wedAMTotal) / float(MAX_WEEKS);
AMThursAverage = float(thurAMTotal) / float(MAX_WEEKS);
AMFriAverage = float(friAMTotal) / float(MAX_WEEKS);
//Print out the results
std::cout << "The average for all Monday AM deliveries is: " << AMMonAverage << std::endl;
std::cout << "The average for all Tuesday AM deliveries is: " << AMTueAverage << std::endl;
std::cout << "The average for all Wednesday AM deliveries is: " << AMWedAverage << std::endl;
std::cout << "The average for all Thursday AM deliveries is: " << AMThursAverage << std::endl;
std::cout << "The average for all Friday AM deliveries is: " << AMFriAverage << std::endl;
}
return 0;
}
//User wants average of all PM deliveries
else if (userSelection == 2 || userSelection == 4)
{
//Loop through all five PM arrays and sum their values
for (int x = 0; x < MAX_WEEKS; x++)
{
monPMTotal = monPMTotal + monPMArray[x];
tuePMTotal = tuePMTotal + tuePMArray[x];
wedPMTotal = wedPMTotal + wedPMArray[x];
thurPMTotal = thurPMTotal + thursPMArray[x];
friPMTotal = friPMTotal + friPMArray[x];
}
//If the user wants the PM average counts
if (userSelection == 2)
{
//Store the grand total of all PMTotal values in PMTotal
PMTotal = monPMTotal + tuePMTotal + wedPMTotal + thurPMTotal + friPMTotal;
//Print out the average by dividing PMTotal by (MAX_WEEKS * 5)
PMAverage = float(PMTotal) / float((MAX_WEEKS * 5));
std::cout << "The average for all PM deliveries is: " << PMAverage << std::endl;
}
//The user wants the PM average for each day of the week
else
{
//Create the individal day of week averages by dividing the day of week total by MAX_WEEKS
PMMonAverage = float(monPMTotal) / float(MAX_WEEKS);
PMTueAverage = float(tuePMTotal) / float(MAX_WEEKS);
PMWedAverage = float(wedPMTotal) / float(MAX_WEEKS);
PMThursAverage = float(thurPMTotal) / float(MAX_WEEKS);
PMFriAverage = float(friPMTotal) / float(MAX_WEEKS);
//Print out the results
std::cout << "The average for all Monday PM deliveries is: " << PMMonAverage << std::endl;
std::cout << "The average for all Tuesday PM deliveries is: " << PMTueAverage << std::endl;
std::cout << "The average for all Wednesday PM deliveries is: " << PMWedAverage << std::endl;
std::cout << "The average for all Thursday PM deliveries is: " << PMThursAverage << std::endl;
std::cout << "The average for all Friday PM deliveries is: " << PMFriAverage << std::endl;
}
return 0;
}
//User wants to exit the program
else
{
std::cout << "Program terminated!" << std::endl;
return 0;
}
}
}
//Function: menu
//Purpose: show the user their choices for interacting with the program
//Inputs: none
//Outputs: non
//Errors: none
void menu()
{
std::cout << std::endl;
std::cout << "Press 1 and then enter to average AM delivery counts." << std::endl;
std::cout << "Press 2 and then enter to average PM delivery counts." << std::endl;
std::cout << "Press 3 and then enter to average AM delivery counts and show by day of week." << std::endl;
std::cout << "Press 4 and then enter to average PM delivery counts and show by day of week." << std::endl;
std::cout << "Press 5 and then enter to exit." << std::endl;
std::cout << "Please make your selection: " << std::endl;
}
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
const int MAX_WEEKS = 10;
//Function prototype to show the user menu
void menu();
int main()
{
//Ten arrays to store delivery counts for 5 days in the week and 2 delivery periods
int aMArray[MAX_WEEKS][10] = { 0 };
int aMTotal[10] = {0};
float aMAverage[10] = {0.0};
//Create input handler
std::ifstream inputHandler;
//Used to store index position for each array
int arrayCounter = 0;
//Used to store the menu selection from the user
int userSelection;
//Store the AM and PM total
int AMTotal = 0;
int PMTotal = 0;
//Store the AM and PM average
float AMAverage = 0.0;
float PMAverage = 0.0;
//Open deliveryData.txt file
inputHandler.open("deliveryData.txt");
//Check if the inputHandler failed, if so display warning and terminate program
if (!inputHandler)
{
std::cout << "ERROR: Failed to open file. Please make sure file is called deliveryData.txt and stored in the same folder as your executable." << std::endl;
inputHandler.close();
return -1;
}
//inputHandler is valid, begin reading data
else
{
//Tell the user that we are reading through the file
std::cout << "Starting file processing." << std::endl;
while (!inputHandler.eof())
{
for(int i =0; i<10;i++){
//Read and store the next line of data into the 10 arrays
inputHandler >> aMArray[arrayCounter][i] ;
}
arrayCounter++;
}
//Close the input handler
inputHandler.close();
//Inform user the file has been processed
std::cout << "Finished file processing." << std::endl;
//Show the user the menu and ask for input
menu();
std::cin >> userSelection;
while (userSelection < 1 || userSelection > 5)
{
//User made an error, ask for selection again
std::cout << "ERROR: Please selection again." << std::endl;
menu();
std::cin >> userSelection;
}
//User wants average of all AM deliveries
if (userSelection == 1 || userSelection == 3)
{
//Loop through all five AM arrays and sum their values
for(int y = 0; y<5 ;y++)
{
float sum =0;
for (int x = 0; x < MAX_WEEKS; x++)
{
sum = sum + aMArray[x][y];
}
aMTotal[y] = sum;
}
//If the user wants the AM average counts
if (userSelection == 1)
{
int sum = 0;
for(int y = 0; y<5 ;y++)
{
sum = sum + aMTotal[y];
}
//Store the grand total of all AMTotal values in AMTotal
AMTotal = sum;
//Print out the average by dividing AMTotal by (MAX_WEEKS * 5)
AMAverage = float(AMTotal) / float((MAX_WEEKS * 5));
std::cout << "The average for all AM deliveries is: " << AMAverage << std::endl;
}
//The user wants the AM average for each day of the week
else
{
for(int y = 0; y<5 ;y++)
{
aMAverage[y] = float(aMTotal[y]) / float(MAX_WEEKS);
}
//Print out the results
std::cout << "The average for all Monday AM deliveries is: " << aMAverage[0] << std::endl;
std::cout << "The average for all Tuesday AM deliveries is: " << aMAverage[1] << std::endl;
std::cout << "The average for all Wednesday AM deliveries is: " << aMAverage[2] << std::endl;
std::cout << "The average for all Thursday AM deliveries is: " << aMAverage[3] << std::endl;
std::cout << "The average for all Friday AM deliveries is: " << aMAverage[4] << std::endl;
}
return 0;
}
//User wants average of all PM deliveries
else if (userSelection == 2 || userSelection == 4)
{
//Loop through all five AM arrays and sum their values
for(int y = 5; y<10 ;y++)
{
float sum =0;
for (int x = 0; x < MAX_WEEKS; x++)
{
sum = sum + aMArray[x][y];
}
aMTotal[y] = sum;
}
//If the user wants the PM average counts
if (userSelection == 2)
{
int sum = 0;
for(int y = 5; y<10 ;y++)
{
sum = sum + aMTotal[y];
}
//Store the grand total of all PMTotal values in PMTotal
PMTotal = sum;
//Print out the average by dividing PMTotal by (MAX_WEEKS * 5)
PMAverage = float(PMTotal) / float((MAX_WEEKS * 5));
std::cout << "The average for all PM deliveries is: " << PMAverage << std::endl;
}
//The user wants the PM average for each day of the week
else
{
for(int y = 5; y<10 ;y++)
{
aMAverage[y] = float(aMTotal[y]) / float(MAX_WEEKS);
}
//Print out the results
std::cout << "The average for all Monday PM deliveries is: " << aMAverage[5] << std::endl;
std::cout << "The average for all Tuesday PM deliveries is: " << aMAverage[6] << std::endl;
std::cout << "The average for all Wednesday PM deliveries is: " << aMAverage[7] << std::endl;
std::cout << "The average for all Thursday PM deliveries is: " << aMAverage[8] << std::endl;
std::cout << "The average for all Friday PM deliveries is: " << aMAverage[9] << std::endl;
}
return 0;
}
//User wants to exit the program
else
{
std::cout << "Program terminated!" << std::endl;
return 0;
}
}
}
//Function: menu
//Purpose: show the user their choices for interacting with the program
//Inputs: none
//Outputs: non
//Errors: none
void menu()
{
std::cout << std::endl;
std::cout << "Press 1 and then enter to average AM delivery counts." << std::endl;
std::cout << "Press 2 and then enter to average PM delivery counts." << std::endl;
std::cout << "Press 3 and then enter to average AM delivery counts and show by day of week." << std::endl;
std::cout << "Press 4 and then enter to average PM delivery counts and show by day of week." << std::endl;
std::cout << "Press 5 and then enter to exit." << std::endl;
std::cout << "Please make your selection: " << std::endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.