CS 112 Lab 1: Control Structures

Objectives:

  1. Practice using for loops, while loops, and if statements.
  2. Practice creating functions and passing parameters.

Grading Rubric

This lab is worth 25 pts:

Step 0. Prepare

Open Eclipse and create a new project called lab1 (not lab 1, not lab01, not Lab 1, not Lab1). In the project create a new Source File called main.cpp.

Create a second file, a text file, called answers.txt. (To do this, right-click on your project icon (called lab1), and choose New -> File.) You will submit answers to each small coding step below into this file. Separate your answers with a line like this:

      Step x. -------- <the current time> -----------------
      your code here
      sample output from running your code
   
Please put the current clock time into the header for each step, so that I (your professor) can see how long each step takes you.

Step 1. if statement practice, 1

In main.cpp, create a program that
  1. prints "Enter your grade: "
  2. reads in an unsigned value from the user
  3. computes whether the letter grade (a char) is an A, B, C, D, or F. An A is 90 or above, a B is 80 - 89, etc.
  4. prints out the grade saying "Your grade is grade".
When you have this working copy and paste the entire file, plus a sample run of your code, into your answers.txt file, labeling the submission as "Step 1. --------- <the current time> -------------------------------"

Step 2. if statement practice, 2

Clear out everything you have inside main().
Implement a program to print out the code of this insurance plan, computing the result in as few lines as possible -- but still using readable/hospitable code.
Months
Plan Name 1 2-6 7+
silver $90 $60 $60
gold $90 $70 $35

The program asks the user first for the plan name -- a string. Then, the program asks for the number of months -- an unsigned. Then, the code uses a series of if statements to compute the cost -- an unsigned, printing it out at the end. My implementation uses 2 if statements, 1 else if, and 2 else statements, not in that order. Part of my solution has an if-else pair embedded in another if/else body.
Finally, of course, your code prints out the results in nice English. Perhaps something like this:

The cost for your plan is $60.

When you have your code completed (and perfectly indented!), copy and paste your code, plus sample output, into answers.txt.

Step 3. while loop practice

Improve your code from step 2. by adding a while loop that checks if the user entered a legal plan name ("silver" or "gold"), and if not tells the user to try again. E.g.,
      Please enter a plan name: maroon
      I'm sorry, "maroon" is not a known plan.
      Please enter a plan name: sliver
      I'm sorry, "sliver" is not a known plan.
      Please enter a plan name: gold
      ... code goes on now ...
   
The while loop repeats until the user enteres either silver or gold.
When you have this working correctly, paste the entire code and sample output into your answers.txt file.

Step 4. switch statement

Erase the contents of main(). Now, write code that asks the user to enter a single lowercase letter. Your code then uses a switch statement to test if the letter is a vowel or consonant. Vowels are "a", "e", "i", "o", and "u". We'll call "y" a consonant for this exercise.
Use default in your switch statement for the "consonant" case.

When you have this working, paste the code and sample output into your answers.txt file.

Step 5. for loop, 1

Erase the contents of main(). Now, write code to:
  1. create an array of unsigned, of size 8, called values
  2. a for loop to initialize the values array to 0, 8, 16, 24, ...
  3. a series of assert statements to check that the 0th value is 0, 3rd value is 24, and 7th value is 56.
Submit your code and sample output, including your assert statements, to your answers.txt file.

Step 6. for loop, 2

Erase the contents of main(). Now, write code:
  1. create an array of doubles, of size 100.
  2. write a for loop to fill in the array with the values 0.0, 0.1, 0.2, 0.3, etc.
  3. write a for loop to sum up all the values in the array -- but this loop has to iterate from the last item down to the first. Note: make the loop variable be of type int, not unsigned, as this variable will be decremented down to -1, which cannot be represented by an unsigned.
  4. print out the resulting sum
Submit your code and output to your answers.txt file.

Step 7. defining a function, 1

Erase the contents of main() and all the functions you defined. Now, rewrite step 4. above so that the computation of whether a letter is a vowel -- i.e., the switch statement -- is in a function. Give the function a good name -- something that indicates to the reader what the function does.
Getting the input from the user and printing the output remains in main(). So, you'll need to change the switch statement so that it returns a boolean -- true if the letter is a vowel, and false otherwise. Submit your code, including the function, and sample output to your answers.txt file.

Step 8. defining a function, 2

Erase the contents of main(). Now, write code to:
  1. implement your code from step 3. above in a function called getPlan() that returns the string ("silver" or "gold") that the user enters.
  2. move the code that gets the number of months from the user into a function.
  3. move the code that computes the cost into a function called computeCost(). This function takes the plan name and the months as arguments.
  4. your main() code now just does 4 things: call getPlan(), call getMonths(), call computeCost(), print out the cost. (My implementation of main() is literally just 4 lines long.)
Submit your code, including the functions, and sample output to your answers.txt file.

Submit

For this lab, submit only your answers.txt file to

/home/cs/112/current/yourUserId/lab1

Any code in the file should be indented correctly, as if it were in a .cpp file.

If you used pair programming, only one of you should turn in your code. It does not matter which person turns it in, but
--> make sure you have both students' names and user-names at the top of your answers.txt file. <--

The directory /home/cs/112/current should contain a directory with your user-name. You can check by typing this command into a terminal window:

$ ls /home/cs/112/current
   
Each week's lab and project must be (separately) copied to your directory, so that the grader can grade it. If your lab is not copied to your directory within /home/cs/112/current, the grader will not be able to find it, and you will receive a zero!

To illustrate, suppose that your user-name is abc1, that you are currently in your home folder, that your Eclipse workspace is named cs112, that your cs112 folder is within your home folder, and that your lab1 folder is inside your cs112 folder (i.e., /home/abc1/cs112/lab1). Then you should enter:

$ cd cs112

to change directory to your workspace -- the folder "above" your lab1 folder. Then enter:

$ ls
to view the contents of your workspace. At this point, you should see folders for last week's lab (lab0) and this week's lab (lab1). Then enter:
$ cp -r lab1 /home/cs/112/current/abc1

where you replace abc1 with your user-name. The cp -r command will recursively copy lab1 and all of its subfolders into your folder inside /home/cs/112/current/. You can check that this worked by listing the contents of your folder:

$ ls /home/cs/112/current/abc1
lab1

and then verifying that your folder contains a copy of the today's files:

$ ls /home/cs/112/current/abc1/lab1
... files for lab1 should appear ...

We will be using these same commands to submit our labs and projects each week for the rest of the semester, so you should record them somewhere that you can easily find them (or maybe bookmark this page).

When your folder within current contains a copy of your lab work, you are all done. Congratulations!

Next, you should email all your files to your partner, so that she/he can work independently on the project.

If time permits, you may each begin working on this week's project.


CS > 112 > Labs > 01


This page maintained by Victor Norman.