HW 08

pdf

School

University of Minnesota-Twin Cities *

*We aren’t endorsed by this school

Course

1113

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

5

Uploaded by Hughng

Report
CSci 1113: Introduction to C/C++ Programming for Scientists and Engineers Homework 8 Fall 2016 Due Date: Wednesday, Nov. 30, 2016 before 4:00pm. Instructions : This is an individual homework assignment. There are two problems worth 20 points each. Solve the problems below by yourself (unlike the labs, where you work collaboratively), and submit the solution as a C++ source code file. Because all homework assignments are submitted and tested electronically, the fol- lowing are important: • You follow any naming conventions mentioned in the homework instructions. • You submit the correct file(s) through Moodle by the due deadline. • You follow the example input and output formats given in each problem description. • Regardless of how or where you develop your solutions, your programs compile and execute on cselabs computers running the Linux operating system. Problem A: Adding trains (20 points) Start with the tootToot.cpp file on the website. Fill in the missing functions/classes to enable the program to compile and work correctly. This program simulates a train, where you start with just an engine. You have three options: go to the next train (forward towards the engine), go to the previous train (back towards the end of the train) or add a train behind the current train car. You may not change main(), we will check to ensure it is exactly the same. You should be able to train cars anywhere, and this will insert it into that part of the train. For this part, you do not need to worry about deleting dynamic memory . Hint: when making the add() function, it would probably help to draw it out and figure out how many arrows/pointers you need to change. Example 1 (user input is underlined): Current train: Engine Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? a Which train is this? 4 Current train: Engine Previous train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? a Which train is this? 1 Current train: Engine Previous train: 1 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a 
train, or (q)uit? p Next train: Engine Current train: 1 Previous train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? a Which train is this? 3 Next train: Engine Current train: 1 Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? a Which train is this? 2 Next train: Engine Current train: 1 Previous train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? p Next train: 1 Current train: 2 Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? p Next train: 2 Current train: 3 Previous train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? p Next train: 3 Current train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? p Next train: 3 Current train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? a Which train is this?
5 Next train: 3 Current train: 4 Previous train: 5 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? p Next train: 4 Current train: 5 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? n Next train: 3 Current train: 4 Previous train: 5 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? n Next train: 2 Current train: 3 Previous train: 4 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? n Next train: 1 Current train: 2 Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? n Next train: Engine Current train: 1 Previous train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? n Current train: Engine Previous train: 1 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, or (q)uit? q When you are done, name the source code file <username>_8A.cpp. Here you replace <username> with your U of M email address; for example, if your email address is smithx1234@umn.edu, your file should be named smithx1234_8A.cpp. Then submit your program using the HW 8 Problem A submission link in Moodle.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Problem B: Removing trains (20 points) Build off your answer for problem A (and include it in the submission). Add functionality to detach the previous train (the one behind the current train), if it exists. This time you need to ensure there are no memory leaks and all “new”s are deleted. Any cars on the train that are left once the user quits the loop should be deleted at the end of main(). Example 1 (user input is underlined): Current train: Engine Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? a Which train is this? 3 Current train: Engine Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? a Which train is this? 1 Current train: Engine Previous train: 1 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? p Next train: Engine Current train: 1 Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? a Which train is this? TERIBAD Next train: Engine Current train: 1 Previous train: TERIBAD Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? a Which train is this? 2 Next train: Engine Current train: 1 Previous train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit?
p Next train: 1 Current train: 2 Previous train: TERIBAD Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? d Next train: 1 Current train: 2 Previous train: 3 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? d Next train: 1 Current train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? d Next train: 1 Current train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? n Next train: Engine Current train: 1 Previous train: 2 Do you wish to go to the (n)ext train, (p)revious train, (a)dd a  train, (d)etach a train, or (q)uit? q When you are done, name the source code file <username>_8B.cpp. Here you replace <username> with your U of M email address; for example, if your email address is smithx1234@umn.edu, your file should be named smithx1234_8B.cpp. Then submit your program using the HW 8 Problem B submission link in Moodle.