CSCI 159 Lab 2 exercises

Lab2 will consist of two parts:

Hopefully most people complete all of the basic exercise and get a good start on the main exercise within their scheduled lab section on Sept. 16/18th.

Here is the collection of new C++ syntax elements we'll be using for lab2.

Today we'll also experiment with a different editor, pluma


Refresher: logging in and setting up

  1. logging in

  2. opening the browser and today's lab page

  3. opening a terminal window

  4. using make159 to get lab2

  5. getting into lab2 and the edit/compile/test/submit cycle


New: getting feedback/marks from the previous lab(s)

  1. getting the feedback directory the first time

  2. updating the feedback directory (for later labs)

  3. seeing the feedback files


Part 1: the basics of this lab's new C++ features (basic.cpp)

For the first part of today's lab exercise we're adding code to a program to experiment with some of of the more unusual behaviour and limitations of simple data types, while also learning to 'code to standards' as we go. Of course we'll get more practice with the edit/compile/test/submit cycle as we go.

We'll also try to create and call a function of our own: passing data to it, having it perform computation and return a result which our main routine can then use.

Go through the various steps, adding each to your basic.cpp main routine as you go.


Part 2: design and implementation exercise (lab2.cpp)

Now that we've practiced using each of our new C++ features, our objective is to design, write, test, and debug a more interesting program in our lab2.cpp file.

The program we'll be creating is supposed to estimate how much stair climbing the user has effectively performed while going from class to class for Math/CS courses, going between buildings 210, 315, and 460 and counting sections of uphill/ramps as an equivalent to stairs.

The behaviour we want from our program is as follows:

For the calculations to be correct, of course, you'll have to count how many stairs there are between the three buildings and between 315's floors along your preferred routes plus account for any uphill/ramp travel. This is discussed in greater detail in the 'Field Research' section below.

No error checking is required at this point, you can trust that the user will provide valid data. (When testing your submission for this lab I'll only use valid data.)

Sample output/behaviour

Below is a sample run of the program, but using totally fictional counts for the actual number of stairs between the various buildings and floors. I'll trust you to do the research to count the actual number of stairs :)

Just for the sake of readability the run below shows the user input in bold italics - that of course is something your program couldn't actually do.

Your output doesn't have to look exactly like the output shown below, and the numbers certainly won't, but should provide a similar level of detail and functionality.

 
Welcome to the StairCounter! 
 
We calculate your total stair climbing for your Math/CS courses based on how often you go from building to building and from floor to floor. 
 
At the moment it just handles travel between buildings 210 (2nd floor), 315 (floors 1 and 2), and 460 (3rd floor).
 
How many times did you travel from building 210 to the ground floor of building 315? 
4 
Within building 315, how many times did you travel from floor 1 up to floor 2? 
2 
How many times did you travel from the upper floor of building 315 to the third floor of building 460? 
3 
Based on the information you provided you climbed 578 total stairs, 
or roughly the equivalent of 41 flights of stairs 

Thanks for using StairCounter! 
 

Field Research

You'll need to do a little investigation to determine for yourself how many stairs each travel section equates to. This will involve picking a route between buildings or floors (what path to follow, what set of stairs to use), then counting the stairs along the route and adding an adjustment for any sections of uphill walking. (E.g. perhaps assuming that when following a ramp or uphill section of sidewalk count every three paces as equivalent to one stair.)

Of course this will result in different students' programs coming up with different numbers, but it's the planning process, design, and program implementation that are the important parts of this exercise.

Feel free to share/compare the route counts with each other: the design and implementation of your program must be done individually, but basic background facts like 'how many stairs are there between floor X and Y in building Z' can be shared. Future modifications

This initial version of our program is pretty limited: not very robust, not very user friendly. In future labs we'll be improving and expanding upon our design (adding error checking, adding a wider range of buildings/floors, making it easier for the user to pick/choose their start/end building/floor, tracking and reporting on information over longer periods of time, etc.)

Designing your solution

Take some time to come up with a design - figuring out what functions you want/need to write/call, what you want each of them to do, and how you want to structure your logic in the main routine.

Taking the time to come up with a good design will, in the long run, result in much cleaner and better organized code: code that is easier to write, understand, debug, and maintain.

Good design takes practice though. Don't worry if you have to significantly revise your original design a couple of times as you work through the coding practice: it's normal to recognize the need for some changes as you start getting deeper into the actual code.

If you're having trouble getting started, go to the lecture resources and check out the sections on design and modularity listed under Sept. 23-27. There are also many good resources online to help new programmers learning to design.

In next week's lab time (Sept 23/25) we'll spend about 45 minutes on the first quiz, but the instructor will be available for the rest of the lab to talk about your design ideas/issues. (And, since there are no labs the week of Sept 30-Oct 4th, you'll have just under two weeks to finish up.)

Standards and submission

Remember to put all your code for this part of the exercise in your lab2.cpp file in your lab2 directory, and submit it prior to the deadline using the "make submit" command.

As with the other lab exercises, you can 'make submit' as often as you like, it basically just copies your latest version to a place where I can access it.

Do remember that your code needs to follow the course code standards, and that it is most effective to code-to-standards as you go.


Prepping for next week's in-lab quiz

You can find discussion of quiz 1 topics, prep ideas, cheat sheet, etc here: csci.viu.ca/~wesselsd/courses/csci159/quizzes/q1/


Thinking about the design (added for post-quiz discussion)

Hopefully over the past week folks have had some time to think about potential designs for the lab2.cpp program, and there's a week left to carry out the implementation and submit it.

VERSION A If you're struggling with an approach to the design, here's a sample process for writing it as 'one big main routine':

Outline the key steps we'll want the program to follow

Identify the libraries we'll probably need
Probably just the iostream library

Identify the constants we'll need for fixed values The integer constants we'll likely need will be for the stair counts for each of the routes and how many stairs we regard as one 'flight': These can be declared after the #includes before the main routine.

Identify the variables we'll need for data storage The integer variables we'll likely need will be for the grand total stairs and flights and for how often the user takes each route These can be declared inside the main routine (likely before anything else in the main).

Write a skeleton for the program, but with comments where the code will go
// #include the libraries we need

// declare constants for the stair counts for each route

int main()
{
   // declare variables for the number of times each trip is made
   // a variable for the total calculated stair count
   // a variable for the total calculated number of flights

   // prompt (cout) the user for the number of times they traveled 210-315
   // read (cin) their response

   // prompt the user for the number of times they traveled 315  floor one to floor two
   // read (cin) their response

   // prompt the user for the number of times they traveled 315-460
   // read (cin) their response

   // calculate the sum of the total distances and store in the final result variable
   //   (# times each trip was made * the stair count for that route)

   // calculate how many flights of stairs this represents
   //   (the total number of stairs / our constant for stairs per flight)

   // display (cout) the final results
}

Take an iterative approach to adding code for each comment
Working from the top down, go through the following cycle repeatedly until done:


VERSION B: ideally, we'll create and use our own functions to carry out portions of the code. Normally we would design our program using functions as a core part of the design right from the start, but this time let's start from our working program (VERSION A above) and replace some of the code with functions, one step at a time.

The display of the final results might be the easiest place to start:

Perhaps next we we'll replace the calculation of the number of flights of stairs with a call to a function:

Then we could do similarly with the calculation of the total number of stairs:

Finally, we could write a function that took the names of the start/destination building/floor as string parameters (e.g. "B210", "B315-Flr1"), did the cout/cin to get the number of trips from the user, and then returned that result to main (to be stored in the corresponding variable).