CS 135 - Final Review
1. What is wrong with the following code? Finish the function to fix the issue.
//Finds the sign of the given num.
int findSign(int num) {
if (num < 0)
return -1;
else if (num == 0)
return 0;
}
2. What are void functions? How do they differ from a value returning function and what kinds of uses do
they have?
3. Given the following function prototypes:
int findDistance(int, int);
string getName();
void printNameAndAge(string, int);
Explain the errors (if any) in each of the following lines of code used later:
int distance = findDistance(3, "distance");
bool name = getName();
string name2 = "Vincent";
name = printNameAndAge(name2, 20);
4. Write a short code fragment to create an int array of size 10, and then fill it with the squares of numbers
from 0-9 (e.g. 0, 1, 4, 9, 16, ..., 81).
5. What action may cause a segmentation fault when using arrays?
6. Given the following code, finish it to find and print what index the string ”stopword” is at. If it’s not
found in the array, print ”not found”.
const int SIZE = 100;
string words[SIZE];
//Code that would set the words array
//YOUR CODE HERE
7.Bubble Sort is generally an inefficient algorithm. In what situation would it be good, however?
8. Rewrite the following code so that it is more organized and stores everything in a single array.
const int ARRAY_SIZE = 300;
int userIds[ARRAY_SIZE];
bool checkedIn[ARRAY_SIZE];
string emails[ARRAY_SIZE];
int scores[ARRAY_SIZE];
1
9. Provide some advantages of using an abstract data type like a struct.
10. Write a Person struct with an age, a name, and a function that prints the name and age of the person
to cout. You can write the print function either inside or outside of the struct.