Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Homework 8 (C++)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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 following 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):
Currenttrain:Engine
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
a
Whichtrainisthis?
4
Currenttrain:Engine
Previoustrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
a
Whichtrainisthis?
1
Currenttrain:Engine
Previoustrain:1
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda

train,or(q)uit?
p
Nexttrain:Engine
Currenttrain:1
Previoustrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
a
Whichtrainisthis?
3
Nexttrain:Engine
Currenttrain:1
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
a
Whichtrainisthis?
2
Nexttrain:Engine
Currenttrain:1
Previoustrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
p
Nexttrain:1
Currenttrain:2
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
p
Nexttrain:2
Currenttrain:3
Previoustrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
p
Nexttrain:3
Currenttrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
p
Nexttrain:3
Currenttrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
a
Whichtrainisthis?

5
Nexttrain:3
Currenttrain:4
Previoustrain:5
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
p
Nexttrain:4
Currenttrain:5
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
n
Nexttrain:3
Currenttrain:4
Previoustrain:5
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
n
Nexttrain:2
Currenttrain:3
Previoustrain:4
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
n
Nexttrain:1
Currenttrain:2
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
n
Nexttrain:Engine
Currenttrain:1
Previoustrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,or(q)uit?
n
Currenttrain:Engine
Previoustrain:1
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
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.

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 news 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):
Currenttrain:Engine
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
a
Whichtrainisthis?
3
Currenttrain:Engine
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
a
Whichtrainisthis?
1
Currenttrain:Engine
Previoustrain:1
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
p
Nexttrain:Engine
Currenttrain:1
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
a
Whichtrainisthis?
TERIBAD
Nexttrain:Engine
Currenttrain:1
Previoustrain:TERIBAD
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
a
Whichtrainisthis?
2
Nexttrain:Engine
Currenttrain:1
Previoustrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?

p
Nexttrain:1
Currenttrain:2
Previoustrain:TERIBAD
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
d
Nexttrain:1
Currenttrain:2
Previoustrain:3
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
d
Nexttrain:1
Currenttrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
d
Nexttrain:1
Currenttrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,or(q)uit?
n
Nexttrain:Engine
Currenttrain:1
Previoustrain:2
Doyouwishtogotothe(n)exttrain,(p)revioustrain,(a)dda
train,(d)etachatrain,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.

You might also like