CS 70

Interactive Experiments

In this part of the assignment, you will experiment with your implementation to explore the differences between our two train-resizing strategies, BASIC and SMART.

Your Tasks

Running Interactively

We have provided a function interact(traintype ttype) that will allow you to simulate sending an receiving packages. You can comment out all of the tests for now and uncomment the interact(ttype) function.

When this function runs, you can type

  • s and hit Enter to send a package (i.e., put a package on the train).
  • r and hit Enter to receive a package (i.e., take the package off the train).
  • ; to stop interaction.

You can run your code from the command line using a flag for main which is either a B to interact with a BASIC train or an S to interact with a SMART train. You can also still interact with the program when running with Valgrind, which we recommend.

Here is an example run:

DOCKER > valgrind --leak-check=full ./testtrain B
==1085== Memcheck, a memory error detector
==1085== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1085== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==1085== Command: ./testtrain B
==1085==
s
(4, 1) [x][_][_][_]~
s
(8, 2) [x][x][_][_]~
s
(12, 3) [x][x][x][_]~
s
(16, 4) [x][x][x][x]~
s
(20, 9) [x][x][x][x]~[x][_][_][_]~
r
(20, 13) [x][x][x][x]~
r
(20, 13) [x][x][x][_]~
r
(20, 13) [x][x][_][_]~
r
(20, 13) [x][_][_][_]~
r
(20, 13) [_][_][_][_]~
;
==1085==
==1085== HEAP SUMMARY:
==1085==     in use at exit: 0 bytes in 0 blocks
==1085==   total heap usage: 6 allocs, 6 frees, 74,816 bytes allocated
==1085==
==1085== All heap blocks were freed -- no leaks are possible
==1085==
==1085== For lists of detected and suppressed errors, rerun with: -s
==1085== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Using File Redirection

Rather than typing each send and receive instruction, you can also put your instructions in a file and use the shell's input redirect operator, <, which causes the contents of the file to appear on the program's standard input, ala

DOCKER> somecommand < inputfile

In the tests directory, we have provided several test files for you to use.

Here's an example of running with one of the test files (we're not using Valgrind here, but you'll probably want to run it both normally and with Valgrind):

DOCKER > ./testtrain B < tests/test-02.txt
(4, 1) [x][_][_][_]~
(8, 2) [x][x][_][_]~
(12, 3) [x][x][x][_]~
(16, 4) [x][x][x][x]~
(20, 9) [x][x][x][x]~[x][_][_][_]~
(24, 10) [x][x][x][x]~[x][x][_][_]~
(28, 11) [x][x][x][x]~[x][x][x][_]~
(28, 11) [x][x][x][x]~[x][x][_][_]~

Explore Worst Cases for the BASIC Train

The BASIC train will resize whenever possible. Experiment with the BASIC train and find a sequence of inputs that causes Quinn to lose money—where the operating cost exceeds revenue.

In the tests directory, create two input files to lose money:

  • basic_worst_case_01.txt, which never uses more than two cars.
  • basic_worst_case_02.txt, which has at least three cars at the end of the run.

Explore the behavior of the SMART train.

Can you come up with a sequence of inputs that causes Quinn to lose money when using a SMART train?

In tests, create a file called smart_train_behavior.txt and

  • if you came up with a money-losing sequence, include it here
  • if you cannot come up with such a sequence, write at least three sentences explaining why
  • Hedgehog speaking

    Do we have to have the level of rigor we'd have in a math class?

  • LHS Cow speaking

    Not right now. We haven't learned the tools yet, so you'll just have to explain as best you can.

  • Pig speaking

    Now I'm excited to learn MORE about that stuff! I can't wait!

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)