CS16: Programming Assignment 09

Introduction

The assignment for this week will utilize pointers and dynamic arrays.

This assignment is due on March 5th 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 09 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 09 directory:

$ mkdir pa09
$ cd pa09

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 three files called sort.cpp, split.cpp, and anagram.cpp:

$ touch sort.cpp split.cpp anagram.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 problem is worth 40 points. Each should be solved in its own file and both must be submitted for full assignment credit.

Chapter 8: Programming Project 8

This should be solved in the sort.cpp file.

Write a sorting function that will sort a vector of ints. Use the selection sort algorithm (see Display 7.11 in the text for an example). The sort function should have the following declaration:

void sort(vector<int>& v);

Note: the function should have one parameter, it does not need a size parameter since vectors know their own size.

Write a program that inputs a sequence of integers (ending with an empty line) and prints the resulting sorted vector all on one line with elements separated by commas.

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

Enter integers (one on each line, entering an empty line quits):
1
2
3

Sorted: 1, 2, 3

Enter integers (one on each line, entering an empty line quits):
3
2
1

Sorted: 1, 2, 3

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

Chapter 8: Programming Project 14

This should be solved in the split.cpp file.

Given the following header:

vector<string> split(string target, string delimiter);

implement the function split so that it returns a vector of the strings in target that are separated by the string delimiter.

For example: split("10,20,30", ",") should return a vector with the strings "10", "20", and "30". Similarly, split("do re mi fa so la ti do", " ") should return a vector with the strings "do", "re", "mi", "fa", "so", "la", "ti", and "do".

Write a program that inputs two strings and calls your function to split the first target string by the second delimiter string and prints the resulting vector all on line line with elements separated by commas.

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 possibly different numbers in the output:

Enter string to split:
10,20,30
Enter delimiter string:
,
The substrings are: "10", "20", "30"

Enter string to split:
do re mi fa so la ti do
Enter delimiter string:
 
The substrings are: "do", "re", "mi", "fa", "so", "la", "ti", "do"

Note: the fourth line in the second example has a single space character (it is not an empty line).

The strings printed by the program should include a newline at the end.

Chapter 8: Programming Project 15

This should be solved in the anagram.cpp file.

Write a function that determines if two strings are anagrams. The function should not be case sensitive and should disregard any punctuation or spaces. Two strings are anagrams if the letters can be rearranged to form each other. For example, “Eleven plus two” is an anagram of “Twelve plus one”. Each string contains one “v”, three “e’s”, two “l’s”, etc. You may use either the string class or a C-style string.

Write a program that inputs two strings and calls your function to determine whether or not the strings are anagrams and prints the result.

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 possibly different numbers and numbers of asterisks in the output:

Enter first string:
Eleven plus two
Enter second string:
Twelve plus three
The strings are not anagrams.

Enter first string:
Rats and Mice
Enter second string:
in cat's dream
The strings are anagrams.

The strings 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 sort sort.cpp
$ g++ -std=c++11 -o split split.cpp
$ g++ -std=c++11 -o anagram anagram.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:

$ ./sort
$ ./split
$ ./anagram

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

If you encounter memory errors (like segmentation faults or bus errors), you will need to make use of gdb to diagnose and solve the errors. Refer to the first problem of PA07 for a reminder on how to use gdb.

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 9”. 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 437 sort.cpp split.cpp anagram.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. 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