1 |
h13 |
CS16 F16 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 9am or 10:30am |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h13: Homework 13
ready? | assigned | due | points |
---|---|---|---|
true | Tue 11/15 02:00PM | Thu 11/17 02:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)
Please:
- No Staples.
- No Paperclips.
- No folded down corners.
Reading: Read Chapter 8 and the lecture notes. If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”.
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL! FOR BEST RESULTS, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.
1.(4 pts) Name 4 reasons why breaking up a program into multiple files can be advantageous.
2.(2 pts) How are ordinary arrays of characters and c-strings similar and how are they dissimilar?
3.(4 pts) What are two (2) things that are wrong with this use of a c-string (ignore why someone would write this code, focus on logic/syntax errors)?
char s1[5] = "abcd", s2[5] = "efgh";
for (int i = 0; i <= 5; i++)
s1[i] = 'v';
if (s1 != s2) s1 = "xyz";
4.(4 pts) What is the output of the following code?
char s1[4] = "abc", s2[4] = "ABC";
if (strcmp(s1, s2)) cout << "Yo!";
else cout << "Hey!";
5.(8 pts) Given the code shown below, what happens in each of these cases?
string line;
cin.ignore(10,'x') >> line;
cout << line << endl;
a) If the user enters: abc def ghijk (note the 2 space characters)
b) If the user enters: abcx123
c) If the user enters: abcxxyz
d) If the user enters: abcx123 456 (note the space character)
6.(8 pts) The following code takes in a string input from the user and performs an integer multiplication, as seen in the example run here. Note that the input string will contain the asterix character (i.e. *):
Enter 2 integer numbers to be multiplied, like this: num1*num2: 15*3
The answer is: 45
Complete the missing code below that performs this task (it can be done in 2 lines, but you can use more if you like).
string s; int k(0);
cout << "Enter 2 integer numbers to be multiplied, like this: num1*num2: ";
cin >> s;
cout << "The answer is: " << k << endl;
7.(10 pts) Write a function called swapFrontBack
that takes as input a vector of integers. The function should swap the first element in the vector with the last element in the vector. The function should check if the vector is empty to prevent errors.
Attach your answer to this homework. Please do not use more than a single side of a page.