Exam #1 - Computer Science

advertisement
Honors Computer Science I – Exam #1
Date: 2/9/09
SOLUTIONS
1) (6 pts) Write out all the permutations of “CAKE” in the order in which these would be printed
by the recursive permutation algorithm shown in class:
1. CAKE
5. CEKA
9. AKCE
13. KACE
17. KECA
21. EKAC
2. CAEK
6. CEAK 10. AKEC
14. KAEC
18. KEAC
22. EKCA
3. CKAE
7. ACKE 11. AEKC
15. KCAE
19. EAKC
23. ECKA
4. CKEA
8. ACEK 12. AECK
16. KCEA
20. EACK
24. ECAK
¼ pt each, take the floor to get the score
2) (10 pts) In class we wrote a recursive function that printed out the corresponding binary
representation of an input value given in decimal. For this question you will do the opposite task.
You will write a recursive function that takes in a binary number encoded as a string of ‘0’ and
‘1’ characters only with a leading character of ‘1’, along with the string’s length and return the
value of the number in base ten. Hint: Notice that if we look at the binary number 1011, it can be
broken down into two components: 101 (rest of the digits) and 1 (last digit). To build the decimal
equivalent of 1011 from these two components, simply calculate 2x5 (decimal equivalent of 101)
+ 1 (last digit of 1011). The prototype is provided below:
int binToDec(char binString[], int length) {
if (length == 0) // 1 pts
return 0; // 1 pt
else {
// 1 pts array access, 1 pt -, 1 pt ‘0’
int last = binString[length-1]-'0';
// 1 pts 2*, 1 pts binToDec, 1 pt binString,
// 1 pt length-1, 1 pt +last
return 2*binToDec(binString, length-1) + last;
}
}
3) (10 pts) Write a recursive function that finds the largest number in an unsorted integer array.
Return this value. The two input values to the function are the array and the length of the array.
Fill in the prototype given below:
int max(int array[], int length) {
if (length == 1) // 1 pt
return array[0]; // 1 pt
int tempMax = max(array,length-1); // 3 pts
if (array[length-1] > tempMax) // 3 pts
return array[length-1]; // 1 pt
else
return tempMax; // 1 pt
}
4) (4 pts) Convert B62D16 to binary. Put a box around your final answer.
1011011000101101 (1 pt per block of 4)
5) (8 pts) Convert 34568 to base 6. Put a box around your final answer.
34568 = 3x83 + 4x82 + 5x8 + 6 = 3x512 + 4x64 + 40 + 6 = 1536 + 256 + 46 = 183810
1838%6 = 2
306%6 = 0
51%6 = 3
8%6
=2
1%6
=1
1838/6 = 306
306/6 = 51
51/6 = 8
8/6
=1
1/6
=0
Answer is 123026. (4 pts for each part – 1 pt off for each mistake)
6) (10 pts) The following struct can be used to store information about a student:
struct
char
char
int*
};
student {
first[20];
last[20];
grades;
The last item of the struct is meant to be a pointer to a dynamically allocated array of ints that
stores test scores.
Write a segment of code that asks the user to enter two positive integers:
1) n, representing the number of students
2) m, representing the number of tests each student takes.
and allocates space for a dynamically allocated array of size n of struct student, where each
student has space to store exactly m test scores.
Note: You don’t need to initialize any of the allocated space, you just need to allocate it.
The beginning part of the code segment is given to you. Declare any extra variables you need.
int n,m;
struct student* studList;
printf(“How many students do you have?\n”);
scanf(“%d”, &n);
printf(“How many tests have they taken?\n”);
scanf(“%d”, &m);
// 1 pt cast, 1 pts malloc, 1 pt sizeof, 1 pts *n
studList = (struct student*)malloc(sizeof(struct student)*n);
int i;
// 1 pts loop
for (i=0; i<n; i++)
// 1 pts LHS, 1 pt cast, 1 pt malloc, 1 pt size, 1 pt *m
studList[i].grades = (int*)malloc(sizeof(int)*m);
7) (8 pts) Free the memory allocated to studList in the previous question.
for (i=0; i<n; i++) // 2 pts
free(studList[i].grades); // 3 pts
free(studList); // 3 pts
8) (10 pts) Write a recursive function that prints out the items in a linked list in reverse order.
The struct and function prototype are given below:
struct ll {
int data;
struct ll* next;
};
void print(struct ll* list) {
if (list != NULL) { // 3 pts
print(list->next); // 4 pts
printf("%d ", list->data); // 3 pts
}
}
9) (10 pts) Write a function that takes in a pointer to a linked list and an integer that represents a
threshold value, and returns the number of values in the linked list less than the given threshold
value. Fill in the prototype given below:
int numBelowVal(struct ll* list, int threshold) {
int cnt = 0; // 1 pt
while (list != NULL) { // 2 pts
if (list->data < threshold) // 2 pts
cnt++; // 2 pt
list = list->next; // 2 pts
}
return cnt; // 1 pt
}
10) (10 pts) Write a function search that returns 1 if val is in the linked list pointed to by list, and
false otherwise. (Hint: This is easier with recursion!) The prototype is below:
int search(struct ll *front, int val) {
if (list == NULL) // 2 pt
return 0; // 1 pt
else if (list->data == val) // 2 pts
return 1; // 1 pt
else
return search(front->next, val); // 4 pts
}
11) (12 pts) Write a recursive function that prints out all strings composed of digits of length k,
by modifying the permutation algorithm shown in class. The framework is provided for you
below. (For example, the code below, when properly filled out, should print 10000 strings,
starting from 0000 and ending with 9999.)
#include <stdio.h>
void ListAll(char str[]);
void RecursiveListAll(char str[], int k);
int main() {
char word[20] = "0000";
printf("\nHere are the strings:\n\n");
ListAll(word);
return 0;
}
void ListAll(char str[]) {
RecursiveListAll(str, 0);
}
void RecursiveListAll(char str[], int k) {
int j;
if (k == strlen(str))
printf("%s\n", str);
else {
// Fill in what goes here.
// Fill in what goes here.
for (j=0; j<10; j++) {
// 4 pts
str[k] = (char)(‘0’+j);
// 4 pts
RecursiveListAll(str, k+1); // 4 pts
}
}
}
12) (2 pts) From what party city do the Super Bowl Champion New Orleans Saints hail?
New Orleans
Download