1 Exam #131 Page: 1 Name: _____________________________________ 131 CS32—Midterm Exam E01, W15, Phill Conrad, UC Santa Barbara Wednesday, 04/30/2015, 9:30am10:45am Name: ___________________________________________________ Umail Address: ______________________________@ umail.ucsb.edu Please write your name above AND AT THE TOP OF EVERY PAGE Be 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 off You are permitted one sheet of paper (max size 8.5x11") on which to write notes These sheets will be collected with the exam, and might not be returned Please write your name on your notes sheet
240
Embed
CS32—Midterm Exam - cs.ucsb.edupconrad/cs32/15S/exams/E01/pdf/131-160... · CS32—Midterm Exam E01, W15, Phill Conrad, UC Santa Barbara Wednesday, 04/30/2015, 9:30am10:45am Name
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.
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
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?
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.
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.):
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.):
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:
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.
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.