Lab02: Modular Programming

Introduction

This lab will have you practicing modular programming. Hopefully you'll see how breaking things up into well-defined modules can make large programs manageable, can make working together on programs possible, and can make adding and improving programs (relatively) easy.

You will write a program for doing financial simulations. This is a pretty important (and lucrative) application of computers, although we probably won't be able to cash in on today's lab. Moreover, this basic problem is one that we will revisit throughout the semester, since much of what we'll be learning can really help make more and more powerful financial simulators.

You'll work in groups of two! This lab is also the beginning of Project 1.

Setup

Pick a leader for your team that will create the project in GitLab and then follow the GitLab instructions to add your teammates as members to your project.

Teammates will pull a copy of the leader's GitLab project.

The simulator

For this lab, you will simulate 5 years of the financial life of Ensign W. T. Door. ENS Door has to pay rent, pay for his car (at least until it's paid off), and pay for occasional car repairs. ENS Door has a bank account which pays him interest, and which allows him to hold a negative balance to help him through the hard times ... although they charge him a pretty stiff rate for it. Finally, ENS Door has a job ... at least for the moment. As time goes by he may get raises, or he may get fired. However, if fired he'll probably get a new job at the same salary within a few months.
Note: this isn't how life goes for a recently commissioned officer, but please suspend reality for the purpose of the lab.

The simulator consists of three modules:

The interfaces to these modules are defined in the files: bank.h, expenses.h, and income.h. These header files contain everything you need to use the three modules, and also describes what each module is supposed to do.

Additionally, the following is provided:

All that's missing is the implementation of the remaining two modules (expenses.cpp and income.cpp).

You can copy all the provided files from here: driver.cpp. bank.h. bank.o. income.h. expenses.h.

Save the files in your lab02 directory.

Working together ... separately

Each member of the group will implement and thoroughly test one of the remaining modules of the simulator. (I'm not kidding about the testing! You should think about what you need to do to be sure your module works before you try to incorporate it into the whole program!)

After you complete your code and are ready to upload it to the repository, first pull to ensure that you have the most up to date files in the project:

Then follow the standard three git commands to add your changes to the code repository. Then when your partner has pushed their changes, pull those changes with:

Once you've got all changes, compile your program with:

g++ -o WTDsim driver.cpp expenses.cpp income.cpp bank.o

Once you've done that, you can run the simulator by typing./WTDsim. By the way, depending on what you implement you might need therand()function. Check out the cppreference.com page for info onrand()andRAND_MAX. Note thatsrand()is done for you in the driver.cpp file, so there's no need to repeat the seeding.

The power of text-stream Unix tools: reprise

Now change the driver program by printing out the balance at the end of every loop iteration, rather than printing just once at the end of the program. Now when you run the program you get the balance at the end of each month of the 5-year time span. Suppose you wanted to know what the min, max, average, and median values of ENS Door's bank balance during those 5 years. Well, just pipe the output of the modified simulator to themmamprogram from last lab and you've got that information instantly! In other words

./WTDsim | ../lab01/mmam

should do it, assuming the programmmamyou wrote last lab is in lab01.

Take a screenshot of your terminal window and name it execute0.png

Now plot the balance over time on a graph. To do so, start by redirecting the output of your modified simulator into a file named balancedata.

./WTDsim > balancedata

Next, from the terminal window run the program:

gnuplot

Then, from gnuplot's command line give the command:

plot "balancedata" with linespoints

What happened? Did you get your plot?

Take a screenshot of your plot and name it gnuplot0.png

NOTE: Be sure to run the gnuplot commands from the lab machine or your VM, and not a remote shell (i.e. don't run the commands from a command line on midn.cs.usna.edu). If you need to install the gnuplot program, issue the following command in the VM's terminal:sudo apt-get install gnuplot-x11

Now we want to run WTDsim three times producing output files balancedata1, balancedata2, and balancedata3. Now, because the seed is defaulted to use the ever changing system time in driver.cpp, we will get different data each time the program is run. So, in order to get output that we can compare with each other to ensure that our programs are running correctly, we need to manually set the seed for the random number generator. The good news is that driver.cpp has been set up to accept a number as an argument from the command line and use that as the seed.

So, if you want your WTDsim program to run with the seed = 42, then you'd give the command:

./WTDsim 42

Change the parameter for the seed of the random number generator to get the associated output files as indicated below:

filename seed
balancedata1 1
balancedata2 2001
balancedata3 your class (2020 for this section) #

Once you have the three new output files, run gnuplot again, giving it the following command:

plot "balancedata1" with linespoints , "balancedata2" with linespoints , "balancedata3" with linespoints

What happened this time around? Gnuplot is a useful tool for producing plots from data in simple text files.

Take a screenshot of this plot and name it gnuplot1.png

To exit the gnuplot program, give the command:

exit

Lab Deliverables

This Lab is the completion of Project 1, Part 1. Each team member will submit all files that you developed, including the implementation written by your partner for this Lab (i.e. expenses.cpp & income.cpp). Each partner must ensure the File Header is complete per the SI221 Coding Standards, and that both group member's names are indicated in both expenses.cpp and income.cpp. Indicate which group member was the primary author and which was the partner.

You are required to submit the following:

  1. Electronic copy of all authored project files (expenses.cpp & income.cpp).
  2. The image execute0.png as described above.
  3. The images gnuplot0.png and gnuplot1.png as described above.

In a terminal window from your lab02 folder, use the following command to submit your solution:
~/bin/submit -c=SI221 -p=Lab02 expenses.cpp income.cpp execute0.png gnuplot0.png gnuplot1.png

The rest is specific to Project 1.
No more pair work beyond this point!