+1 (315) 557-6473 

3 D Printer Homework Solution using C++


shortest, average, and longest time in 3D Printer

Question -

The workshop staffs want to know the following:

• How frequently is each 3D printer used in a month?

• What is the shortest, average, and longest time each 3D printer is operated for? You are required to write a C++ program that answers these questions using the data provided

Printer 1 –

start: 2021-03-01 09:00:00, stop: 2021-03-01 14:32:31

start: 2021-03-01 15:32:31, stop: 2021-03-01 23:31:21

start: 2021-03-02 00:31:21, stop: 2021-03-02 06:01:43

start: 2021-03-02 07:01:43, stop: 2021-03-02 17:59:41

start: 2021-03-02 18:59:41, stop: 2021-03-02 23:31:49

start: 2021-03-03 00:31:49, stop: 2021-03-03 05:40:37

start: 2021-03-03 06:40:37, stop: 2021-03-03 09:20:28

start: 2021-03-03 10:20:28, stop: 2021-03-03 16:19:02

Printer 2 –

start: 2021-03-03 09:00:00, stop: 2021-03-03 21:51:38

start: 2021-03-03 22:51:38, stop: 2021-03-04 02:10:44

start: 2021-03-04 03:10:44, stop: 2021-03-04 15:15:41

start: 2021-03-04 16:15:41, stop: 2021-03-05 03:37:11

start: 2021-03-05 04:37:11, stop: 2021-03-05 13:43:33

start: 2021-03-05 14:43:33, stop: 2021-03-05 22:04:12

start: 2021-03-05 23:04:12, stop: 2021-03-06 11:02:25

start: 2021-03-06 12:02:25, stop: 2021-03-06 21:32:53

start: 2021-03-06 22:32:53, stop: 2021-03-07 07:48:56

start: 2021-03-07 08:48:56, stop: 2021-03-07 18:46:56

start: 2021-03-07 19:46:56, stop: 2021-03-07 22:33:49

start: 2021-03-07 23:33:49, stop: 2021-03-08 07:19:41

start: 2021-03-08 08:19:41, stop: 2021-03-08 19:59:41

start: 2021-03-08 20:59:41, stop: 2021-03-09 07:31:33

Printer 3 –

start: 2021-03-08 09:00:00, stop: 2021-03-08 15:15:46

start: 2021-03-08 16:15:46, stop: 2021-03-08 23:01:41

start: 2021-03-09 00:01:41, stop: 2021-03-09 02:14:39

Solution:

Report

Key Stages Flowchart

There are 3 printer files and each of these files follows the same flow.

Flowchart

Load Stage

Description

This stage will open the printer file then read each line of usage from file. All the lines are loaded into an array for processing later on.

Code Snippet


void loadPrinterUsage(const string &filename, array&usage) { int numUses = 0; ifstreaminFile(filename.c_str()); if (!inFile.is_open()) return; string line; while (getline(inFile, line)) usage[numUses++] = line; inFile.close(); }

Calculate Frequency Stage

Description

Counts how many times the printer has been used.

Code Snippet


int frequency(const array&usage) { int count = 0; for (int i = 0; i< (int) usage.size(); i++) { if (usage[i] == "") break; count++; } return count; }

Calculate Shortest Time Stage

Description

This stage will look at each and every usage from an array of printer usages. For each use there is a start and stop time. This stage will measure the length of time between the start and stop time for each transaction and keep the transaction that yields to the shortest time.

The transaction format is given as a date and a time. To measure the time between the two dates, other helper and utility methods were developed to count the number of seconds starting from a start time to reach the stop time. The number of seconds is the unit used to measure the shortest time.

Code Snippet


int shortest(const array&usage) { int shortestSeconds = -1; for (int i = 0; i< frequency(usage); i++) { // Extract the start and stop time arraystartDate, stopDate; parseStartAndStopDate(usage[i], startDate, stopDate); // Track the shortest time int secondsDifference = dateDifference(startDate, stopDate); if (shortestSeconds == -1 || secondsDifference

Calculate Average Time Stage

Description

This stage will sum up all elapsed time of a printer’s usage. The total is then divided to the number of usages to arrive at an average. To do the elapsed time measurement, this utilizes the methods discussed in the “Calculate Shortest Time Stage”.

Code Snippet


int average(const array&usage) { int totalSeconds = 0; for (int i = 0; i< frequency(usage); i++) { // Extract the start and stop time arraystartDate, stopDate; parseStartAndStopDate(usage[i], startDate, stopDate); // Track the shortest time totalSeconds += dateDifference(startDate, stopDate); } return totalSeconds / frequency(usage); }

Calculate Longest Time Stage

Description

This stage’s explanation is the same as the “Calculate Shortest Time Stage” except that it keeps the longest elapsed time rather than the shortest elapsed time.

Code Snippet


int longest(const array&usage) { int longestSeconds = -1; for (int i = 0; i< frequency(usage); i++) { // Extract the start and stop time arraystartDate, stopDate; parseStartAndStopDate(usage[i], startDate, stopDate); // Track the shortest time int secondsDifference = dateDifference(startDate, stopDate); if (longestSeconds == -1 || secondsDifference>longestSeconds) longestSeconds = secondsDifference; } return longestSeconds; }

Output Stage

Description

This stage will collate all calculated results from the previous stages and displays them in a neatly formatted order.

Code Snippet


// Display the time in the form of hh:mm:ss void printElapsedTime(int seconds) { // Convert to hours, minutes seconds int minutes = seconds / 60; seconds %= 60; int hours = minutes / 60; minutes %= 60; cout<

Program Output

Output