CIT-237Lab 8.1
Selection Sort
The goal of this lab is to help you get familiar with sorting data in an array.
Due Date
You must demonstrate the solution to this lab exercise to the instructor by Monday, February 24, 2020, in order
to receive full credit for this work.
Lab Setup
1. Create the project using Visual Studio.
2. Download the ZIP file for Lab 8.1 from Moodle.
3. Copy the sample input files, firstTen.txt, and presidents.txt from the Lab 8.1 ZIP file
to the same folder where your source file is located.
Programming Exercise
This lab exercise involves writing a short program to do the following:
Prompt the user for an input file name. Open this file for input. The file contains the names of people
(one per line).
Read the contents of the file into an array of string objects. (The maximum number of names for the
array should be at least 50.)
Display the input names, in the order they appear in the file.
Modify the selection sort algorithm from the textbook so that it works with string objects, instead of
integers. (This may require some research: check the textbook index and/or www.cplusplus.com.)
Display the sorted list.
Add Descriptive Output Statements
Add code to the selection sort that describes each swap operation that the algorithm executes. The format of each
line of this output should be:
Swap [indexA] stringA with [indexB]
stringB
(The sample output on the next page illustrates this output.)
Sample Data Files
The ZIP file for this lab exercise contains two sample data files, which can be used to test your program:
Input file: firstTen.txt
Washington, George
Adams, John
Jefferson, Thomas
Madison, James
Monroe, James
Adams, John Quincy
Jackson, Andrew
Van Buren, Martin
Harrison, William Henry
Tyler, John
CTI237_Lab08.1_Selection_Sort_20200212.docx
2/12/2020 8:16 AM
page 1 of 2
CIT-237
Lab 8.1
Selection Sort
The program output for firstTen.txt is shown below. (In this example, the text that the user types is shown in
BOLD font. The actual input / output will all be displayed in the same font.)
Output from processing the firstTen.txt file
Enter name of input file: firstTen.txt
10 lines of text read from input file.
Here are the unsorted names:
————————-[ 0] Washington, George
[ 1] Adams, John
[ 2] Jefferson, Thomas
[ 3] Madison, James
[ 4] Monroe, James
[ 5] Adams, John Quincy
[ 6] Jackson, Andrew
[ 7] Van Buren, Martin
[ 8] Harrison, William Henry
[ 9] Tyler, John
Swap
Swap
Swap
Swap
Swap
Swap
Swap
Swap
Swap
[
[
[
[
[
[
[
[
[
1]
5]
8]
6]
8]
6]
8]
9]
9]
Adams, John
Adams, John Quincy
Harrison, William Henry
Jackson, Andrew
Jefferson, Thomas
Madison, James
Monroe, James
Tyler, John
Van Buren, Martin
with
with
with
with
with
with
with
with
with
[
[
[
[
[
[
[
[
[
0]
1]
2]
3]
4]
5]
6]
7]
8]
Washington, George
Washington, George
Jefferson, Thomas
Madison, James
Monroe, James
Washington, George
Washington, George
Van Buren, Martin
Washington, George
Here are the names sorted:
————————-[ 0] Adams, John
[ 1] Adams, John Quincy
[ 2] Harrison, William Henry
[ 3] Jackson, Andrew
[ 4] Jefferson, Thomas
[ 5] Madison, James
[ 6] Monroe, James
[ 7] Tyler, John
[ 8] Van Buren, Martin
[ 9] Washington, George
Press any key to continue . . .
The second sample data file, presidents.txt, contains the names of all U.S. Presidents. (This file and its
associated text output are too large to include in the lab document.)
CTI237_Lab08.1_Selection_Sort_20200212.docx
2/12/2020 8:16 AM
page 2 of 2
Washington, George
Adams, John
Jefferson, Thomas
Madison, James
Monroe, James
Adams, John Quincy
Jackson, Andrew
Van Buren, Martin
Harrison, William Henry
Tyler, John
Washington, George
Adams, John
Jefferson, Thomas
Madison, James
Monroe, James
Adams, John Quincy
Jackson, Andrew
Van Buren, Martin
Harrison, William Henry
Tyler, John
Polk, James K.
Taylor, Zachary
Fillmore, Millard
Pierce, Franklin
Buchanan, James
Lincoln, Abraham
Johnson, Andrew
Grant, Ulysses S.
Hayes, Rutherford B.
Garfield, James B.
Arthur, Chester A.
Cleveland, Grover
Harrison, Benjamin
Cleveland, Grover
McKinley, McKinley
Roosevelt, Theodore
Taft, William Howard
Wilson, Woodrow
Harding, Warren G.
Coolidge, Calvin
Hoover, Herbert
Roosevelt, Franklin D.
Truman, Harry S.
Eisenhower, Dwight D.
Kennedy, John F.
Johnson, Lyndon B.
Nixon, Richard
Ford, Gerald
Carter, Jimmy
Reagan, Ronald
Bush, George H. W.
Clinton, Bill
Bush, George W.
Obama, Barack
Trump, Donald
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
This programming project is due on Wednesday, February 19 at 10:00 p.m. The best approach is to plan to have
the solution submitted BEFORE the due date. Then, if you experience any last-minute difficulty, you will still
meet the deadline.
Be sure that you read and understand this entire document before you begin writing your code.
If you have questions, please ask during class or send me an e-mail at my BHCC e-mail address
(pmorgan@bhcc.edu).
Overview
Automobile Speed/Distance Application Program
This programming project involves creating a C++ program that simulates the speed of a car and calculates the
distance traveled over a period of time. This must be an interactive program that accepts keyboard input from the
user and produces text output on the console. Each command entered by the user must execute one “time
interval”. For example, the “a” (accelerate) command must increase the speed by 5 miles per hour (MPH) over a
time interval of 1 second. Similarly, the “b” (brake) command must decrease the speed by 5 MPH over a time
interval of 1 second, and the “c” (cruise) command must keep the speed constant for 1 second. All of these
operations include calculation of how far the car travels during the 1 second interval, and the total distance
traveled since the program started. A separate “d” (demo) command outputs a sequence of steps, similar to that
shown in the sample data, simply by executing the other operations in a predetermined sequence.
This document discusses the requirements for the program, as well as possible implementation details. Be sure
you understand this document before you begin coding your final solution.
General Discussion of the Project
Imagine a car which starts out “Stopped”, at some initial position. We could say that the car is at “position zero”,
and its current speed is zero Miles-per-hour. If the driver of the car does nothing, then the car will remain at its
initial position. However, if the driver steps on the gas pedal, the car will “accelerate”: its speed will increase by
some amount. Now that the car is moving, its position will change over time.
How much will the position change? This depends on the speed, and how much time has elapsed. It turns out
that a car moving at a constant speed of 25 miles per hour (36.7 feet per second) will travel 367 feet in 10
seconds. (Multiply 36.7 feet per second times 10 seconds.)
But what if the speed is not constant? In this case we need to know the average speed over the specified time
interval. If the speed changes at a constant rate, perhaps 5 miles-per-hour each second, then the average speed is
the speed at the beginning of the time interval (previousSpeed), plus the speed at the end of the time interval
(currentSpeed), divided by 2:
averageSpeed = (previousSpeed + currentSpeed) / 2.0;
averageSpeed_FeetPerSecond = averageSpeed * 5280.0/3600.0;
intervalFeetTraveled = averageSpeed_FeetPerSecond * timeInterval;
For convenience, we can keep the timeInterval value fixed at one second.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 1 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
The calculations described in the previous paragraphs mention speed in “miles per hour” and distance in “feet”.
This is deliberate, for aesthetic reasons: people are accustomed to thinking of the speed of a car in “miles per
hour”, at least in the United States. Yet, if we were to express the distance traveled over a few seconds in miles,
then the numbers would be tiny fractions of a mile, and would therefore not be intuitively appropriate to the
person using the program. This is why the averageSpeed value is converted from miles per hour (MPH)
to feet per second (FPS). In general, the formula for converting miles per hour (MPH) to feet per second (FPS) is:
𝐹𝑃𝑆 = 𝑀𝑃𝐻 (
5280.0
)
3600.0
Possible Program Variables Description
These variable names are a suggestion, intended to get you thinking about the problem. The implementation that
you choose may be different.
The contents of each variable to represent the car could be as follows:
Variable
currentSpeed
previousSpeed
totalFeetTraveled
intervalFeetTraveled
timeInterval
currentState
delta
Description
Current speed of the car (miles per hour)
Speed of the car at the end of the “previous” time interval (miles per hour)
Total feet traveled by the car (since the program started)
Total feet traveled during the most recent time interval
The amount of elapsed time for each calculation interval (fixed at 1 second).
Current state of the car’s motion. Valid values are: “Stopped”, “Accelerating”,
“Cruising” (moving at a steady speed), or “Braking”.
The amount that the speed of the car will increase (during acceleration) or decrease
(during braking). By default, this value must be 5 miles per hour.
The “accelerate”, “brake”, and “cruise” operations can be calculated as shown. (In this table, the default value of
“delta” is 5 MPH.)
Operation
accelerate
Calculation
brake
previousSpeed = currentSpeed;
currentSpeed = currentSpeed – delta;
cruise
previousSpeed = currentSpeed;
previousSpeed = currentSpeed;
currentSpeed = currentSpeed + delta;
Design Note:
You must write separate functions for the accelerate, brake, and cruise operations. Not only
does this improve the modularity of the program, but it will also make it very easy for you to write the code for
the demo operation described later in this document.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 2 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
Possible Function Descriptions
Below is a list of possible functions, and their descriptions.
Function
outputStatusHeader
outputStatus
updateDistanceTraveled
Member Function Description
Output column headings to the console (see sample output on the next page).
Output current data values to the console.
Calculate the intervalFeetTraveled (over the most recent calculation interval) and
totalFeetTraveled (since the program started) using the following formula:
averageSpeed = (previousSpeed + currentSpeed) / 2.0;
averageSpeed_FeetPerSecond = averageSpeed * 5280.0/3600.0;
intervalFeetTraveled = averageSpeed_FeetPerSecond * timeInterval;
totalFeetTraveled = totalFeetTraveled + intervalFeetTraveled;
accelerate
brake
cruise
demo
Remember to convert Miles-per-hour to feet-per-second before calculating the distance.
You must write separate functions for accelerate, brake, and cruise. This will make it
easier for you to write the code for the demo command.
Execute a predetermined sequence of the accelerate, brake, and cruise operations. (See
the Sample Output section of this document for one possible sequence.)
Some Thoughts about Modeling a Physical System
Whenever writing code to simulate the operation of a physical system, keep in mind how the real physical system
would behave and try to make your simulation match that behavior as closely as reasonably possible. In this
project, this issue comes up when maintaining the “currentState” variable. For example, if the car is
currently “Stopped”, and the main program executes the “cruise” command, or the “brake” command, then the car
must remain “Stopped”.
Similarly, if the car is moving and the main program executes the “brake” command, then the car must enter the
“Braking” state and the speed must decrease. If the main program continues to execute the “brake” command
repeatedly, then eventually the speed will reach zero. At this time, the car must transition to the “Stopped” state,
and the car must remain “Stopped” even if the “brake” command continues to get executed again and again. This
is how a real car would behave, so it is how our simulation should hopefully behave. (This is the reason why the
State Transition Diagram on the next page has two variations of the “brake” command: “Bf” is a brake
command when the initial speed is greater than the “delta” value. “Bs” is a brake command when the initial
speed is less than or equal to the “delta” value. The “delta” value is the amount that the speed changes,
whenever the accelerate or brake command is used. By default, “delta” must be 5 MPH.)
An example of incorrect behavior would be for the car to start moving backwards (speed < 0) because someone
kept his foot on the brake pedal after the car has come to a complete stop.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 3 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
State Transition Diagram
If we use a Finite State Machine in our design, then the State Transition Diagram might look like the following:
Events (inputs):
A
= “Accelerate” Command.
Bs
= “Brake-slow” Command (“Brake” command when speed delta).
C
= “Cruise” Command.
Bf15
Bs, C3
Initial1
Bs14
Stopped
Braking
A12
A2
Bs6
C13
Bf7
A4
4
Bf11
Bs10
A8
Accelerating
Cruising
C5
C9
In this State Transition Diagram we have left off the Actions associated with each transition. The reason for this
is to prevent the diagram from becoming too messy and difficult to read. The Actions are included in the State
Transition Table, which is on the next page. (The numeric subscript on each transition label of this diagram refers
to the row number in the State Transition Table.)
Study the State Transition Diagram and State Transition Table carefully, to be sure you are familiar with the
required behavior of the program.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 4 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
State Transition Table
The Finite State Machine shown in the State Transition Diagram (previous page) can also be expressed as a State
Transition Table. Both are accurate representations of the behavior of the final program.
Previous State
1
Event
Initial
2
Stopped
Accelerate command
3
4
Stopped
Accelerating
Brake or Cruise command
Accelerate command
5
Accelerating
Cruise command
6
Accelerating
Brake command,
currentSpeed delta
8
Cruising
Accelerate command
9
Cruising
Cruise command
10
Cruising
Brake command,
currentSpeed delta
12
Braking
Accelerate command
13
Braking
Cruise command
14
Braking
Brake command,
currentSpeed delta
CIT237_Project1_20200127.docx
Action
currentSpeed = 0 (MPH),
totalFeetTraveled=0,
delta = 5 (MPH).
previousSpeed = currentSpeed;
currentSpeed += delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed += delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed =0;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed -= delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed += delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed = 0;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed -= delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed += delta;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed = 0;
Call updateDistanceTraveled().
previousSpeed = currentSpeed;
currentSpeed -= delta;
Call updateDistanceTraveled().
1/27/2020 11:15 AM
New State
Stopped
Accelerating
Stopped
Accelerating
Cruising
Stopped
Braking
Accelerating
Cruising
Stopped
Braking
Accelerating
Cruising
Stopped
Braking
page 5 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
Sample Output
An example of valid output is shown in the table below. Note: this example includes the output from the “d”
command (demo). When the demo command is executed in this example, the car was previously “Stopped”, as
shown by the three “Cruise” steps at the beginning. Next the car accelerates for 5 seconds, travels at constant
speed for 5 seconds, then brakes until stopped.
Sample “demo” output:
Function
Command: d
Cruise
Cruise
Cruise
Accelerate
Accelerate
Accelerate
Accelerate
Accelerate
Cruise
Cruise
Cruise
Cruise
Cruise
Brake
Brake
Brake
Brake
Brake
Brake
Brake
Brake
Command: q
Current State Current Speed
Stopped
Stopped
Stopped
Accelerating
Accelerating
Accelerating
Accelerating
Accelerating
Cruising
Cruising
Cruising
Cruising
Cruising
Braking
Braking
Braking
Braking
Stopped
Stopped
Stopped
Stopped
Interval Distance
0
0
0
5
10
15
20
25
25
25
25
25
25
20
15
10
5
0
0
0
0
Total Feet (and miles) traveled
0.0
0.0
0.0
3.7
11.0
18.3
25.7
33.0
36.7
36.7
36.7
36.7
36.7
33.0
25.7
18.3
11.0
3.7
0.0
0.0
0.0
0.0
0.0
0.0
3.7
14.7
33.0
58.7
91.7
128.3
165.0
201.7
238.3
275.0
308.0
333.7
352.0
363.0
366.7
366.7
366.7
366.7
(0.000)
(0.000)
(0.000)
(0.001)
(0.003)
(0.006)
(0.011)
(0.017)
(0.024)
(0.031)
(0.038)
(0.045)
(0.052)
(0.058)
(0.063)
(0.067)
(0.069)
(0.069)
(0.069)
(0.069)
(0.069)
The next example shows a session where the user enters various commands:
Sample interactive output:
Function
Current State Current Speed
Command: h
Supported commands:
a
b
c
d
h
q
Command: a
Accelerate
Command: a
Accelerate
Command: c
Cruise
Command: c
Cruise
Command: c
Interval Distance
Total Feet (and miles) traveled
accelerate.
brake.
cruise.
demo.
print this help text.
quit (end the program).
Accelerating
5
3.7
3.7 (0.001)
Accelerating
10
11.0
14.7 (0.003)
Cruising
10
14.7
29.3 (0.006)
Cruising
10
14.7
44.0 (0.008)
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 6 of 10
CIT237 C++ Programming
Cruise
Command: c
Cruise
Command: a
Accelerate
Command: a
Accelerate
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: a
Accelerate
Command: b
Brake
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: b
Brake
Command: b
Brake
Command: b
Brake
Command: b
Brake
Command: b
Brake
Command: a
Accelerate
Command: a
Accelerate
Command: a
Accelerate
Command: a
Accelerate
Command: a
Accelerate
Command: a
Accelerate
Command: a
Accelerate
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Command: c
Cruise
Programming Project 1
Due date: February 19, 2020
Cruising
10
14.7
58.7 (0.011)
Cruising
10
14.7
73.3 (0.014)
Accelerating
15
18.3
91.7 (0.017)
Accelerating
20
25.7
117.3 (0.022)
Cruising
20
29.3
146.7 (0.028)
Cruising
20
29.3
176.0 (0.033)
Cruising
20
29.3
205.3 (0.039)
Cruising
20
29.3
234.7 (0.044)
Accelerating
25
33.0
267.7 (0.051)
Braking
20
33.0
300.7 (0.057)
Cruising
20
29.3
330.0 (0.063)
Cruising
20
29.3
359.3 (0.068)
Cruising
20
29.3
388.7 (0.074)
Cruising
20
29.3
418.0 (0.079)
Cruising
20
29.3
447.3 (0.085)
Cruising
20
29.3
476.7 (0.090)
Braking
15
25.7
502.3 (0.095)
Braking
10
18.3
520.7 (0.099)
Braking
5
11.0
531.7 (0.101)
Stopped
0
3.7
535.3 (0.101)
Stopped
0
0.0
535.3 (0.101)
Accelerating
5
3.7
539.0 (0.102)
Accelerating
10
11.0
550.0 (0.104)
Accelerating
15
18.3
568.3 (0.108)
Accelerating
20
25.7
594.0 (0.112)
Accelerating
25
33.0
627.0 (0.119)
Accelerating
30
40.3
667.3 (0.126)
Accelerating
35
47.7
715.0 (0.135)
Cruising
35
51.3
766.3 (0.145)
Cruising
35
51.3
817.7 (0.155)
Cruising
35
51.3
869.0 (0.165)
Cruising
35
51.3
920.3 (0.174)
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 7 of 10
CIT237 C++ Programming
Command: c
Cruise
Cruising
Command: c
Cruise
Cruising
Command: c
Cruise
Cruising
Command: c
Cruise
Cruising
Command: c
Cruise
Cruising
Command: b
Brake
Braking
Command: b
Brake
Braking
Command: b
Brake
Braking
Command: b
Brake
Braking
Command: b
Brake
Braking
Command: b
Brake
Braking
Command: b
Brake
Stopped
Command: b
Brake
Stopped
Command: b
Brake
Stopped
Command: b
Brake
Stopped
Command: b
Brake
Stopped
Command: h
Supported commands:
a
b
c
d
h
q
Programming Project 1
Due date: February 19, 2020
35
51.3
971.7 (0.184)
35
51.3
1023.0 (0.194)
35
51.3
1074.3 (0.203)
35
51.3
1125.7 (0.213)
35
51.3
1177.0 (0.223)
30
47.7
1224.7 (0.232)
25
40.3
1265.0 (0.240)
20
33.0
1298.0 (0.246)
15
25.7
1323.7 (0.251)
10
18.3
1342.0 (0.254)
5
11.0
1353.0 (0.256)
0
3.7
1356.7 (0.257)
0
0.0
1356.7 (0.257)
0
0.0
1356.7 (0.257)
0
0.0
1356.7 (0.257)
0
0.0
1356.7 (0.257)
accelerate.
brake.
cruise.
demo.
print this help text.
quit (end the program).
Command: q
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 8 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
Optional Enhancements (Extra Credit)
As always, I recommend implementing the minimal requirements first, and then setting a copy of that code aside
before starting on any “extra credit” enhancements. That way, if you run out of time or have trouble getting the
enhancements to work, you still have something acceptable to hand-in.
1. Optional “r” command: If you have time and want to try an enhancement, make the amount of acceleration
or braking a variable, rather than the fixed (5 MPH) value described in the basic version. That is, by default,
the value of delta is assumed to be 5 MPH, but you could make it changeable if you wish. Implement a user
command called “r” that changes the delta value. If the user types the “r” command, then the program
must ask the user to enter a new value for the delta variable. If the user attempts to enter a value that is zero
or less than zero, the program must output an error message and leave the delta variable unchanged. Be sure
that your “help” text includes instructions on how to use the new command.
2. Optional “t” command: Another enhancement you could consider is to make the timeInterval itself a
variable. That is, by default, the timeInterval variable is assumed to have a value of 1 second. If the user
types the “t” command, then the program must ask the user to enter a new value for the timeInterval
variable. If the user attempts to enter a value that is zero or less than zero, the program must output an error
message and leave the timeInterval variable unchanged. Be sure that your “help” text includes instructions
on how to use the new command.
Project Deliverables:
The project source file(s) must be submitted by Moodle, using the Moodle Activity:
CIT237_Project1
Submit your source code only: .cpp file(s) and any .h file(s) that you create. I will need to compile
your code on my home computer in order to grade it.
If you are submitting more than one file, do not enclose the files in a ZIP file.
Do not submit the entire Visual Studio project.
Do not include the Visual Studio project folders, or any binary files.
If you have developed your program using some tool set other than Visual Studio, be sure to compile
and test your final version on one of the Windows 10 computers in our classroom before you submit it.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
page 9 of 10
CIT237 C++ Programming
Programming Project 1
Due date: February 19, 2020
Grading Criteria
The project will be graded according to the following grading criteria:
Feature
1. The program functions correctly.
2. In the main function of the program, there is a loop which contains code to
support the following input commands:
a
b
c
d
h
q
Portion of grade
65%
25%
accelerate.
brake.
cruise.
demo.
print this help text.
quit (end the program).
3. The main function calls other functions to implement the various
commands.
4. The “command loop” in the main function must continue until the user
enters a ‘q’ command.
5. The program is clearly organized and commented so as to make it easy to
read and understand.
CIT237_Project1_20200127.docx
1/27/2020 11:15 AM
10%
page 10 of 10