CS16: Programming Assignment 02

Introduction

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

This assignment is due on July 1st at 23:59.

Top

Step 1: Create an Engineering Account

If you haven't created a CoE account, it is extremely important that you do so as soon as possible.

The form to create your account can be found at https://accounts.engr.ucsb.edu/create.

Top

Step 2: 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 3: Programming Assignment 02 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 02 directory:

$ mkdir pa02
$ cd pa02

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

Top

Step 4: Create Your C++ Files

This week, you will need to create a three files called coins.cpp, meeting.cpp, and sum.cpp:

$ touch coins.cpp meeting.cpp sum.cpp
Top

Step 5: 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 6: Write the Code

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

Chapter 1, Programming Project 3

This should be solved in the coins.cpp file.

Write a program that allows the user to enter a number of quarters, dimes, and nickels and then outputs the monetary value of the coins in cents. For example, if the user enters 2 for the number of quarters, 3 for the number of dimes, and 1 for the number of nickels, then the program should output that the coins are worth 85 cents.

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

Enter number of quarters:
2
Enter number of dimes:
3
Enter number of nickels:
1
The coins are worth 85 cents.

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 2, Programming Project 5

This should be solved in the meeting.cpp file.

Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people attending the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as planned due to fire regulations and tells how many people must be excluded in order to meet the fire regulations.

Note: You should do the problem specified above, rather than the harder version shown in the textbook.

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

Enter maximum room capacity:
50
Enter number of people present:
40
Meeting is legal.

Enter maximum room capacity:
50
Enter number of people present:
55
Meeting cannot be held unless 5 leave.

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 2, Programming Project 9

This should be solved in the sum.cpp file.

Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.

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

Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
Enter whole number:
1
The sum of numbers greater than zero is 10.
The sum of numbers less than zero is 0.
The sum of all numbers is 10.

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 7: 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 coins coins.cpp
$ g++ -std=c++11 -o meeting meeting.cpp
$ g++ -std=c++11 -o sum sum.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:

$ ./coins
$ ./meeting
$ ./sum

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 8: 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_m15” and click on “Programming Assignment 2”. 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 500 coins.cpp meeting.cpp sum.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 9: 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 10: 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