CS32—Midterm Exam - cs.ucsb.edupconrad/cs32/15S/exams/E01/pdf/131-160... · CS32—Midterm Exam...
-
Upload
vuongtuong -
Category
Documents
-
view
224 -
download
0
Transcript of CS32—Midterm Exam - cs.ucsb.edupconrad/cs32/15S/exams/E01/pdf/131-160... · CS32—Midterm Exam...
1Exam #131 Page: 1 Name: _____________________________________
131
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #131 Page: 2 Name: _____________________________________
131
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -17,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -48,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11011110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11110111 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10111111 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #131 Page: 3 Name: _____________________________________
131
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #131 Page: 4 Name: _____________________________________
131
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #131 Page: 5 Name:_____________________________________
131
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #131 Page: 6 Name: _____________________________________
131
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #131 Page: 7 Name: _____________________________________
131
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #131 Page: 8 Name:_____________________________________
131
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #132 Page: 1 Name: _____________________________________
132
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #132 Page: 2 Name: _____________________________________
132
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -61,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -28,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11011101 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10001010 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11101110 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #132 Page: 3 Name: _____________________________________
132
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #132 Page: 4 Name: _____________________________________
132
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #132 Page: 5 Name:_____________________________________
132
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #132 Page: 6 Name: _____________________________________
132
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #132 Page: 7 Name: _____________________________________
132
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #132 Page: 8 Name:_____________________________________
132
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #133 Page: 1 Name: _____________________________________
133
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #133 Page: 2 Name: _____________________________________
133
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -50,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -10,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000001 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10010001 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11010101 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #133 Page: 3 Name: _____________________________________
133
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #133 Page: 4 Name: _____________________________________
133
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #133 Page: 5 Name:_____________________________________
133
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #133 Page: 6 Name: _____________________________________
133
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #133 Page: 7 Name: _____________________________________
133
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #133 Page: 8 Name:_____________________________________
133
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #134 Page: 1 Name: _____________________________________
134
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #134 Page: 2 Name: _____________________________________
134
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -120,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -113,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101010 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10011100 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10101000 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #134 Page: 3 Name: _____________________________________
134
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #134 Page: 4 Name: _____________________________________
134
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #134 Page: 5 Name:_____________________________________
134
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #134 Page: 6 Name: _____________________________________
134
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #134 Page: 7 Name: _____________________________________
134
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #134 Page: 8 Name:_____________________________________
134
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #135 Page: 1 Name: _____________________________________
135
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #135 Page: 2 Name: _____________________________________
135
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -26,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -7,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000011 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11010011 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11001011 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #135 Page: 3 Name: _____________________________________
135
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #135 Page: 4 Name: _____________________________________
135
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #135 Page: 5 Name:_____________________________________
135
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #135 Page: 6 Name: _____________________________________
135
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #135 Page: 7 Name: _____________________________________
135
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #135 Page: 8 Name:_____________________________________
135
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #136 Page: 1 Name: _____________________________________
136
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #136 Page: 2 Name: _____________________________________
136
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -71,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -115,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000011 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11100110 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11111010 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #136 Page: 3 Name: _____________________________________
136
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #136 Page: 4 Name: _____________________________________
136
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #136 Page: 5 Name:_____________________________________
136
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #136 Page: 6 Name: _____________________________________
136
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #136 Page: 7 Name: _____________________________________
136
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #136 Page: 8 Name:_____________________________________
136
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #137 Page: 1 Name: _____________________________________
137
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #137 Page: 2 Name: _____________________________________
137
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -105,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -9,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11011100 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10011101 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10011100 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #137 Page: 3 Name: _____________________________________
137
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #137 Page: 4 Name: _____________________________________
137
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #137 Page: 5 Name:_____________________________________
137
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #137 Page: 6 Name: _____________________________________
137
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #137 Page: 7 Name: _____________________________________
137
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #137 Page: 8 Name:_____________________________________
137
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #138 Page: 1 Name: _____________________________________
138
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #138 Page: 2 Name: _____________________________________
138
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -2,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -72,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10010000 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11111000 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10110100 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #138 Page: 3 Name: _____________________________________
138
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #138 Page: 4 Name: _____________________________________
138
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #138 Page: 5 Name:_____________________________________
138
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #138 Page: 6 Name: _____________________________________
138
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #138 Page: 7 Name: _____________________________________
138
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #138 Page: 8 Name:_____________________________________
138
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #139 Page: 1 Name: _____________________________________
139
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #139 Page: 2 Name: _____________________________________
139
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -36,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -93,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101001 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10101111 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11010111 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #139 Page: 3 Name: _____________________________________
139
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #139 Page: 4 Name: _____________________________________
139
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #139 Page: 5 Name:_____________________________________
139
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #139 Page: 6 Name: _____________________________________
139
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #139 Page: 7 Name: _____________________________________
139
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #139 Page: 8 Name:_____________________________________
139
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #140 Page: 1 Name: _____________________________________
140
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #140 Page: 2 Name: _____________________________________
140
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -80,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -74,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101001 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11000010 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10000101 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #140 Page: 3 Name: _____________________________________
140
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #140 Page: 4 Name: _____________________________________
140
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #140 Page: 5 Name:_____________________________________
140
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #140 Page: 6 Name: _____________________________________
140
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #140 Page: 7 Name: _____________________________________
140
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #140 Page: 8 Name:_____________________________________
140
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #141 Page: 1 Name: _____________________________________
141
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #141 Page: 2 Name: _____________________________________
141
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -115,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000010 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11111001 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10101000 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #141 Page: 3 Name: _____________________________________
141
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #141 Page: 4 Name: _____________________________________
141
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #141 Page: 5 Name:_____________________________________
141
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #141 Page: 6 Name: _____________________________________
141
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #141 Page: 7 Name: _____________________________________
141
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #141 Page: 8 Name:_____________________________________
141
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #142 Page: 1 Name: _____________________________________
142
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #142 Page: 2 Name: _____________________________________
142
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -11,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -30,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11110110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11010100 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10111111 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #142 Page: 3 Name: _____________________________________
142
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #142 Page: 4 Name: _____________________________________
142
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #142 Page: 5 Name:_____________________________________
142
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #142 Page: 6 Name: _____________________________________
142
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #142 Page: 7 Name: _____________________________________
142
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #142 Page: 8 Name:_____________________________________
142
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #143 Page: 1 Name: _____________________________________
143
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #143 Page: 2 Name: _____________________________________
143
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -46,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -52,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10001111 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10001011 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11100010 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #143 Page: 3 Name: _____________________________________
143
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #143 Page: 4 Name: _____________________________________
143
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #143 Page: 5 Name:_____________________________________
143
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #143 Page: 6 Name: _____________________________________
143
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #143 Page: 7 Name: _____________________________________
143
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #143 Page: 8 Name:_____________________________________
143
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #144 Page: 1 Name: _____________________________________
144
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #144 Page: 2 Name: _____________________________________
144
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -12,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -106,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11100000 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10111110 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10110011 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #144 Page: 3 Name: _____________________________________
144
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #144 Page: 4 Name: _____________________________________
144
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #144 Page: 5 Name:_____________________________________
144
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #144 Page: 6 Name: _____________________________________
144
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #144 Page: 7 Name: _____________________________________
144
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #144 Page: 8 Name:_____________________________________
144
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #145 Page: 1 Name: _____________________________________
145
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #145 Page: 2 Name: _____________________________________
145
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -47,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -128,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11111001 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11110101 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11010110 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #145 Page: 3 Name: _____________________________________
145
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #145 Page: 4 Name: _____________________________________
145
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #145 Page: 5 Name:_____________________________________
145
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #145 Page: 6 Name: _____________________________________
145
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #145 Page: 7 Name: _____________________________________
145
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #145 Page: 8 Name:_____________________________________
145
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #146 Page: 1 Name: _____________________________________
146
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #146 Page: 2 Name: _____________________________________
146
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -71,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -63,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101101 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11010000 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11101110 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #146 Page: 3 Name: _____________________________________
146
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #146 Page: 4 Name: _____________________________________
146
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #146 Page: 5 Name:_____________________________________
146
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #146 Page: 6 Name: _____________________________________
146
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #146 Page: 7 Name: _____________________________________
146
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #146 Page: 8 Name:_____________________________________
146
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #147 Page: 1 Name: _____________________________________
147
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #147 Page: 2 Name: _____________________________________
147
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -106,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -85,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10000111 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10010000 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #147 Page: 3 Name: _____________________________________
147
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #147 Page: 4 Name: _____________________________________
147
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #147 Page: 5 Name:_____________________________________
147
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #147 Page: 6 Name: _____________________________________
147
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #147 Page: 7 Name: _____________________________________
147
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #147 Page: 8 Name:_____________________________________
147
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #148 Page: 1 Name: _____________________________________
148
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #148 Page: 2 Name: _____________________________________
148
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -22,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -65,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10011010 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11111100 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #148 Page: 3 Name: _____________________________________
148
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #148 Page: 4 Name: _____________________________________
148
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #148 Page: 5 Name:_____________________________________
148
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #148 Page: 6 Name: _____________________________________
148
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #148 Page: 7 Name: _____________________________________
148
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #148 Page: 8 Name:_____________________________________
148
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #149 Page: 1 Name: _____________________________________
149
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #149 Page: 2 Name: _____________________________________
149
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -56,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -87,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11011111 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11010001 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11100010 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #149 Page: 3 Name: _____________________________________
149
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #149 Page: 4 Name: _____________________________________
149
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #149 Page: 5 Name:_____________________________________
149
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #149 Page: 6 Name: _____________________________________
149
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #149 Page: 7 Name: _____________________________________
149
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #149 Page: 8 Name:_____________________________________
149
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #150 Page: 1 Name: _____________________________________
150
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #150 Page: 2 Name: _____________________________________
150
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -81,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -22,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10010011 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10101100 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11111001 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #150 Page: 3 Name: _____________________________________
150
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #150 Page: 4 Name: _____________________________________
150
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #150 Page: 5 Name:_____________________________________
150
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #150 Page: 6 Name: _____________________________________
150
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #150 Page: 7 Name: _____________________________________
150
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #150 Page: 8 Name:_____________________________________
150
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #151 Page: 1 Name: _____________________________________
151
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #151 Page: 2 Name: _____________________________________
151
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -115,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -43,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101100 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11100011 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10011100 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #151 Page: 3 Name: _____________________________________
151
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #151 Page: 4 Name: _____________________________________
151
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #151 Page: 5 Name:_____________________________________
151
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #151 Page: 6 Name: _____________________________________
151
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #151 Page: 7 Name: _____________________________________
151
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #151 Page: 8 Name:_____________________________________
151
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #152 Page: 1 Name: _____________________________________
152
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #152 Page: 2 Name: _____________________________________
152
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -32,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -24,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101011 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11110110 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11001011 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #152 Page: 3 Name: _____________________________________
152
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #152 Page: 4 Name: _____________________________________
152
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #152 Page: 5 Name:_____________________________________
152
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #152 Page: 6 Name: _____________________________________
152
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #152 Page: 7 Name: _____________________________________
152
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #152 Page: 8 Name:_____________________________________
152
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #153 Page: 1 Name: _____________________________________
153
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #153 Page: 2 Name: _____________________________________
153
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -66,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -46,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11000101 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10101101 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11101110 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #153 Page: 3 Name: _____________________________________
153
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #153 Page: 4 Name: _____________________________________
153
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #153 Page: 5 Name:_____________________________________
153
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #153 Page: 6 Name: _____________________________________
153
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #153 Page: 7 Name: _____________________________________
153
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #153 Page: 8 Name:_____________________________________
153
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #154 Page: 1 Name: _____________________________________
154
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #154 Page: 2 Name: _____________________________________
154
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -91,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -108,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11111000 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10001000 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10000101 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #154 Page: 3 Name: _____________________________________
154
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #154 Page: 4 Name: _____________________________________
154
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #154 Page: 5 Name:_____________________________________
154
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #154 Page: 6 Name: _____________________________________
154
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #154 Page: 7 Name: _____________________________________
154
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #154 Page: 8 Name:_____________________________________
154
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #155 Page: 1 Name: _____________________________________
155
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #155 Page: 2 Name: _____________________________________
155
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -125,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -2,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10010010 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10111111 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10101000 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #155 Page: 3 Name: _____________________________________
155
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #155 Page: 4 Name: _____________________________________
155
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #155 Page: 5 Name:_____________________________________
155
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #155 Page: 6 Name: _____________________________________
155
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #155 Page: 7 Name: _____________________________________
155
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #155 Page: 8 Name:_____________________________________
155
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #156 Page: 1 Name: _____________________________________
156
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #156 Page: 2 Name: _____________________________________
156
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -41,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -111,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11010010 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11010110 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11011010 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #156 Page: 3 Name: _____________________________________
156
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #156 Page: 4 Name: _____________________________________
156
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #156 Page: 5 Name:_____________________________________
156
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #156 Page: 6 Name: _____________________________________
156
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #156 Page: 7 Name: _____________________________________
156
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #156 Page: 8 Name:_____________________________________
156
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #157 Page: 1 Name: _____________________________________
157
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #157 Page: 2 Name: _____________________________________
157
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -76,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -4,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10101011 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10001001 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11111001 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #157 Page: 3 Name: _____________________________________
157
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #157 Page: 4 Name: _____________________________________
157
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #157 Page: 5 Name:_____________________________________
157
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #157 Page: 6 Name: _____________________________________
157
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #157 Page: 7 Name: _____________________________________
157
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #157 Page: 8 Name:_____________________________________
157
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #158 Page: 1 Name: _____________________________________
158
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #158 Page: 2 Name: _____________________________________
158
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -100,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -67,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11011110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 11100100 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10010001 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #158 Page: 3 Name: _____________________________________
158
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #158 Page: 4 Name: _____________________________________
158
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #158 Page: 5 Name:_____________________________________
158
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #158 Page: 6 Name: _____________________________________
158
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #158 Page: 7 Name: _____________________________________
158
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #158 Page: 8 Name:_____________________________________
158
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #159 Page: 1 Name: _____________________________________
159
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #159 Page: 2 Name: _____________________________________
159
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -96,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -7,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -89,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 11111000 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10011011 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 10110100 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #159 Page: 3 Name: _____________________________________
159
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #159 Page: 4 Name: _____________________________________
159
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #159 Page: 5 Name:_____________________________________
159
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #159 Page: 6 Name: _____________________________________
159
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #159 Page: 7 Name: _____________________________________
159
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #159 Page: 8 Name:_____________________________________
159
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100
1Exam #160 Page: 1 Name: _____________________________________
160
CS32—Midterm ExamE01, W15, Phill Conrad, UC Santa BarbaraWednesday, 04/30/2015, 9:30am10:45am
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Please write your name above AND AT THE TOP OF EVERY PAGEBe sure you turn in every page of this exam.
Each exam is numbered (e.g. Exam #137).Each pages is numbered (e.g. Page 1, Page 2, etc.)The last page clearly says "End of Exam".
This exam is closed book, closed notes, closed mouth, cell phone offYou are permitted one sheet of paper (max size 8.5x11") on which to write notesThese sheets will be collected with the exam, and might not be returnedPlease write your name on your notes sheet
2Exam #160 Page: 2 Name: _____________________________________
160
1. For these questions, assume 8-bit two's complement representation of negative integers. The left-most bit is the signbit, with 1 representing negative numbers, and 0 representing positive numbers.
a. (2 pts) Given the decimal number -95,what is this number's binary representation in 8-bit two's complement?
b. (2 pts) Given the decimal number -1,what is this number's binary representation in 8-bit two's complement?
c. (2 pts) Given the decimal number -124,what is this number's binary representation in 8-bit two's complement?
d. (2 pts) Given that 10100110 is the 8-bit two's complement representation of a number,what is that number in base ten?
e. (2 pts) Given that 10001110 is the 8-bit two's complement representation of a number,what is that number in base ten?
f. (2 pts) Given that 11000000 is the 8-bit two's complement representation of a number,what is that number in base ten?
3Exam #160 Page: 3 Name: _____________________________________
160
2. Suppose you have an array called a full of n integer values, indexed 0 through n-1. For simplicity, suppose there areno duplicates. You need to implement a function to return the index of a certain key value k if it occurs in the array,or -1 if it does not.
As we've discussed, two possible techniques are linear search (which our textbook calls serial search, and binarysearch.
a. (6 pts) Binary search places one additional requirement on the elements of the array. What is this additionalrequirement? It can be stated in a single sentence.
b. (6 pts) Assuming that the array a meets this additional requirement, describe how to apply the binary searchalgorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE OR PSEUDOCODE.Instead, describe the main idea of the algorithm in plain english. Though it is not required, if you wish, youmay make reference to a, n and k in your description.
c. (6 pts) Linear search has the advantage of not having this extra requirement, and is much easier to code andimplement correctly, but has a serious drawback as compared to binary search. Explain what that is. Again, itcan be stated in a single sentence—but for full credit, you must be as precise as possible in your answer.
d. (6 pts) Assuming that the array a does not meet the requirements for binary search, describe how to apply thelinear search algorithm to this problem. Be clear, precise, and concise. DO NOT WRITE CODE ORPSEUDOCODE. Instead, describe the main idea of the algorithm in plain english. Though it is not required,if you wish, you may make reference to a, n and k in your description.
4Exam #160 Page: 4 Name: _____________________________________
160
3. So far, our discussion of sorting has focused on two main sorting methods: insertion sort, and selection sort. Both ofthese can be implemented as an "in place" sort—one where the values are not copied anywhere outside the array,except to a single temporary variable.
As we've discussed, both of these sorting techniques can be implemented "in place" as successive passes, where thesorted array is built up at one end of the array or the other, in a series of n-1 steps. Our practice examples havefocussed on doing insertion sort where the sorted array is built left to right, and on selection sort, where the sortedarray is built right to left—but this is not the only possible implementation.
For each of the tables below, fill in the successive steps of the algorithm shown. The first two match what we'vedone before. The second two check whether you can adapt your understanding of how selection sort and insertionwork to a novel situation.
Grading for parts (a,b): 1 for each incorrect entry, up to max of 8Grading for parts (c,d): 1 for each incorrect entry, up to max of 4
a. (8 pts) Sort using insertion sort, building sorted array at start of array (at left, the way we've done it before):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
b. (8 pts) Sort using selection sort, building sorted array at end of array (at right, the way we've done it before.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
5 Exam #160 Page: 5 Name:_____________________________________
160
c. (4 pts) Sort using insertion sort, building sorted array at END of array (at right, the OPPOSITE of the waywe've done it before, but still using the approach used in insertion sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
d. (4 pts) Sort using selection sort, building sorted array at START of array (at left, the OPPOSITE of the waywe've done it before, but still using the approach used in selection sort.):
[0] [1] [2] [3] [4]33 29 48 17 6
6 17 29 33 48
6Exam #160 Page: 6 Name: _____________________________________
160
4. Most of the code we write in this course goes in files that end in .cpp. >But sometimes we worked with each of thefollowing types of files. For each type, explain what its purpose is, and its relationship to .cpp files.
DON'T USE TOO MANY WORDS. Write just enough to get the main point across. Excessively verbose answersthat stray off point may be penalized.
a. (4 pts) Purpose of .h files:
b. (4 pts) .h files relationship to .cpp files:
c. (4 pts) Purpose of .o files:
d. (4 pts) .o files relationship to .cpp files.
e. (4 pts) Purpose of Makefile:
f. (4 pts) Relationship of Makefile to .cpp files:
struct Node { int data; Node *next;} void printList(Node *head) { for (Node *p=head; p!=NULL; p=p->next) { cout << p->data << endl; }}
7Exam #160 Page: 7 Name: _____________________________________
160
5. At right is a fairly common definition fora C++ struct Node that can be used tomake a linked list of int values, along witha conventional implementation of afunction that prints the value of the linkedlist, one per line. As a homeworkassignment, you were asked to rewrite thefunction as a recursive function, i.e. to dothe same thing as this iterative version,but using recursion. BUT THAT'S NOTWHAT I'M ASKING YOU TO DO ONTHIS EXAM. So read carefully.
a. (10 pts) Write the definition of aC++ function calledprintListBackwards that also takes a Node *head as its parameter, and prints the list backwards.Given that the struct Node only has next pointers and no prev pointers, you might think this isimpossible, but with recursion is not only possible, it is trivially easy. (Well, if you understand recursion itis.)
HINT: If you are stuck, start by writing out the recursive version of printList which prints the listforwards using recursion. Then think about how you could tweak the function to print the list backwardsinstead.
THERE IS ONE MORE PROBLEM ON THE FINAL PAGE!
8 Exam #160 Page: 8 Name:_____________________________________
160
b. (6 pts) Given that there are no prev pointers, there is no straightforward way to print the list backwardswithout iteration that doesn't involve storing some amount of temporary data that is the same size as the listitself. For example, go might go through the list in the forward direction, and either store the data from thenodes in an array which you then iterate through backwards, or in another linked list that is built in theopposite diretion.
So how is it that this is possible with recursion? What is happening "under the hood" that makes thispossible? Briefly explain.
Hint: "activation record".
End of Examtotal points=100