CS16: Programming Assignment 06

Introduction

The assignment for this week will utilize arrays.

This assignment is due on February 14th 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 06 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 06 directory:

$ mkdir pa06
$ cd pa06

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 two files called hexadecimal.cpp and letters.cpp:

$ touch hexadecimal.cpp letters.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 two problems, each of which is described below. Each problem is worth 50 points. Each should be solved in its own file and both must be submitted for full assignment credit.

Chapter 7, Practice Project 2

This should be solved in the hexadecimal.cpp file.

Hexadecimal numerals are integers written in base 16. The 16 digits used are '0' through '9' plus 'a' for the “digit 10”, 'b' for the “digit 11”, 'c' for the “digit 12”, 'd' for the “digit 13”, 'e' for the “digit 14”, and 'f' for the “digit 15”. For example, the hexadecimal numeral d is the same as base 10 numeral 13 and the hexadecimal numeral 1d is the same as the base 10 numeral 29.

Write a C++ program to perform addition of two hexadecimal numerals each with up to 10 digits. If the result of the addition is more than 10 digits long, then simply give the output message “Addition Overflow” and not the result of the addition. Use arrays to store hexadecimal numerals as arrays of characters.

Your program should include and use a constant that specifies the maximum length of the numbers (10).

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

Enter first number:
1a
Enter second number:
a1
The sum is bb.
	
Enter first number:
1
Enter second number:
ffffffffff
Addition Overflow.
	

The 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 7, Programming Project 8

This should be solved in the letters.cpp file.

Write a program that will read a line of text and output a list of all the letters that occur in the text together with the number of times each letter occurs in the line. End the line with a period that serves as a sentinel value. The letters should be listed in the following order: the most frequently occurring letter, the next most frequently occurring letter, and so forth. Case should be ignored ('a' should be treated the same as 'A'). If two or more letters have same frequency, print them in lexicographical order. The program will prompt the user to enter the lines of text in the terminal. The text line may contain any English letter and zero or more whitespace characters (spaces and tabs only).

For example, the input

do be do bo.

should produce output similar to the following:

Letter# of Occurrences
o3
b2
d2
e1

Your program should include at least one function that takes in an array. Hint: You may want to use this function to sort the letters and their frequencies.

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

Enter text:
do be do bo.
Frequencies:
o 3
b 2
d 2
e 1

Enter text:
aaaaaaaaaabbbbbbbbbbcccccccccca.
Frequencies:
a 11
b 10
c 10

The 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 two commands will compile the two source files (in the same order as listed above):

$ g++ -std=c++11 -o hexadecimal hexadecimal.cpp
$ g++ -std=c++11 -o letters letters.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:

$ ./hexadecimal
$ ./letters

If you encounter a compile-time error, refer to the list of common C++ errors and possible solutions at http://charlottehill.com/cpperrors.html.

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_w16” and click on “Programming Assignment 6”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit both .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 two source files to this assignment by running the command:

$ ~submit/submit -p 429 hexadecimal.cpp letters.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 highest score among submissions 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