CS16: Programming Assignment 03

Introduction

The assignment for this week will utilize input and output and control flow.

This assignment is due on January 23th at 23:59.

Top

Step 1: Open a Terminal

The first step in every assignment will be to open a terminal window, which will be the environment you use to write, compile, and run your programs.

For a reminder on how to do this step, take a look at Step 2 of PA 01.

Top

Step 2: Programming Assignment 03 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 03 directory:

$ mkdir pa03
$ cd pa03

Remember that at any time, you can check what directory you are current in with the command pwd.

Top

Step 3: Create Your C++ Files

This week, you will need to create a three files called pi.cpp, keypad.cpp, and 23.cpp:

$ touch pi.cpp keypad.cpp 23.cpp
Top

Step 4: Edit Your C++ Files

You will now need to complete each source file. Each corresponds to one of the problems listed below, which make up this lab.

For a reminder on how to open the text editor and how edit files, take a look at Step 5 of PA 01.

Top

Step 5: Write the Code

This assignment consists of three problems, each of which is described below. The first and last problems are worth 30 points each, and the middle one is worth 40 points. Each should be solved in its own file and all three must be submitted for full assignment credit.

Chapter 3, Programming Project 8

This should be solved in the pi.cpp file.

An approximate value of pi can be calculated using the series given below:

pi = 4 · [ 1 – 1/3 + 1/5 – 1/7 + 1/9 ... + (–1 ^ n)/(2n + 1) ]

Write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.

The program print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example (including whitespace and formatting), with possibly different inputs and numbers in the output:

Enter the number of terms to approximate (or zero to quit):
1
The approximation is 4.00 using 1 term.
Enter the number of terms to approximate (or zero to quit):
2
The approximation is 2.67 using 2 terms.
Enter the number of terms to approximate (or zero to quit):
0
	

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

All approximated floating pointer numbers must be displayed to exactly two digits after the decimal point.

Chapter 3, Programming Project 11

This should be solved in the keypad.cpp file.

The keypad on your oven is used to enter the desired baking temperature and is arranged like the digits on a phone:

123
456
789
0

Unfortunately the circuitry is damaged and the digits in the leftmost column no longer function. in other words, the digits 1, 4, and 7 do not work. if a recipe calls for a temperature that can't be entered, then you would like to substitute a temperature that can be entered. Write a program that inputs a desired temperature. The temperature must be between 0 and 999 degrees. if the desired temperature does not contain 1, 4, or 7, then output the desired temperature. otherwise, compute the next largest and the next smallest temperature that does not contain 1, 4, or 7 and output both.

For example, if the desired temperature is 450, then the program should output 399 and 500. Similarly, if the desired temperature is 375, then the program should output 380 and 369.

The program print a string of text to the terminal before getting each piece of input from the user. A session should look like one of the the following examples (including whitespace and formatting), with possibly different inputs and numbers in the output:

Enter desired temperature:
450
Closest possible temperatures are 399 and 500.

Enter desired temperature:
223
Closest possible temperature is 223.

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

Chapter 3, Programming Project 12

This should be solved in the 23.cpp file.

The game of “23” is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according to the following rules:

When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3.

The program print a string of text to the terminal before getting each piece of input from the user. A session should look like one of the the following two examples (including whitespace and formatting), with possibly different inputs and numbers in the output:

There are 23 toothpicks left. How many would you like to take?
1
The computer takes 3.
There are 19 toothpicks left. How many would you like to take?
4
You must take between 1 and 3 toothpicks. How many would you like to take?
3
The computer takes 1.
There are 15 toothpicks left. How many would you like to take?
2
The computer takes 2.
There are 11 toothpicks left. How many would you like to take?
3
The computer takes 1.
There are 7 toothpicks left. How many would you like to take?
2
The computer takes 2.
There are 3 toothpicks left. How many would you like to take?
2
The computer takes 1.
The computer loses.

There are 23 toothpicks left. How many would you like to take?
3
The computer takes 1.
There are 19 toothpicks left. How many would you like to take?
1
The computer takes 3.
There are 15 toothpicks left. How many would you like to take?
3
The computer takes 1.
There are 11 toothpicks left. How many would you like to take?
1
The computer takes 3.
There are 7 toothpicks left. How many would you like to take?
3
The computer takes 3.
There is 1 toothpick left. How many would you like to take?
1
You lose.

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

Top

Step 6: Compile the Code

To compile our code, we will use the same g++ command as last week. The following three commands will compile the three source files (in the same order as listed above):

$ g++ -std=c++11 -o pi pi.cpp
$ g++ -std=c++11 -o keypad keypad.cpp
$ g++ -std=c++11 -o 23 23.cpp

If the compilation is successful, you won't see any output from the compiler. You can then use the following commands to run your programs:

$ ./pi
$ ./keypad
$ ./23

If you encounter an error, use the compiler hints and examine the line in question. If the compiler messsage is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile the relevant files after you make any changes to the C++ code.

Top

Step 7: Submit

Once you are satisfied that your program is correct, it is time to submit it. Login at https://submit.cs.ucsb.edu/session, then navigate to “CS16_s15” and click on “Programming Assignment 3”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit all three .cpp files.

Please remember that you must submit the program to obtain any credit for the assignment; just completing the program is not enough.

Once you submit, you should see a page detailing your submission. The system will automatically grade your program and will show you the results on this page after a 1 minute delay.

You can alternatively submit your code from the command line (terminal) on any CS machine, including the Phelps lab machines or the CSIL server. You can use this method when logged in remotely. To submit the the three source files to this assignment by running the command:

$ ~submit/submit -p 418 pi.cpp keypad.cpp 23.cpp

You can copy the URL shown in the output of the above and paste into a web browser to reach the submission result page.

Top

Step 8: Check Submission Results

After the 1 minute delay, the submit system will show your score and give you feedback on your submission. Refresh the webpage after a minute to see this information.

You may submit multiple times. You should submit only after local compilation does not produce any errors and runs as expected. The score of the last submission uploaded before the deadline will be used as your assignment grade.

Top

Step 9: Done!

Once your submission receives a score of 100/100, you are done with this assignment.

If you are in the Phelps lab or in CSIL, make sure to log out of the machine before you leave. Also, make sure to close all open programs before you log out. Some programs will not work next time if they are not closed. Remember to save all your open files before you close your text editor.

If you are logged in remotely, you can log out using the exit command:

$ exit