AB Exam
[Question 1 |
Question 2 |
Question 3 |
Question 4 |
Question 4 (class version)]
For example,
bool values | Individual Translations | Final Translations |
---|---|---|
true true true | 4 2 1 | 7 |
true false false | 4 0 0 | 4 |
false true true | 0 2 1 | 3 |
Part A
Write function Translate, whose header is given below. The function should translate three bool values from its array (apvector) parameter (starting at the specified position in the array) into the corresponding integer value by using the algorithm described above. For example, if array info is as pictured below
then the following illustrates several calls to Translate.
Call to Translate | info (relevant values shown in bold) | Return Value |
---|---|---|
Translate(info,3) | true false false true true false true | 6 |
Translate(info,1) | true false false true true false true | 1 |
Translate(info,0) | true false false true true false true | 4 |
Complete function Translate below the following header. Assume that Translate is called only with parameters that satisfy its precondition.
Part B
Write function TranslateAll whose header is given below. TranslateAll should translate each successive sequence of three bool values in its parameter info, storing the integer values in its parameter result. Assume that the number of elements in info is a multiple of 3.
The following shows the effect of the call TranslateAll(info,result) for two different versions of info.
info | result |
---|---|
true false true false false false true true false | 5 0 6 |
false false true true false false true true true | 1 4 7 |
In writing TranslateAll, you may call the function Translate of part (a). Assume that Translate works as specified, regardless of what you wrote in part (a).
Complete function TranslateAll below the following header
Consider the problem of storing and displaying information about students' letter grades (A-D).
Part A
Write function GradeDistribution, whose header is given below. GradeDistribution reads students' letter grades (characters in the range 'A'..'D') from a file gradeFile, and determines how many times each letter grade occurs. GradeDistribution stores the information about grade occurrences, as well as the total number of grades read, in a Grade struct (declared below).
In the Grade struct, freq[0] represents the number of times a grade of 'A' occurs, freq[1] represents the 'B' occurrences, freq[2] the 'C' occurrences, and freq[3] the 'D' occurrences.
(Recall that the value of 'A' - 'A' is 0, the value of 'B' - 'A' is 1, the value of 'C' - 'A' is 2, and the value of 'D' - 'A' is 3).
Assume that grades are stored in gradeFile one per line with no blank lines. For example, consider the following file gradeFile.
gradeFile A A B B C B D A D D C B B B C B A B B C
Consider the sequence of statements:
Assume that input is bound to a correctly formatted file of grades.
Complete function GradeDistribution below the following header.
Part B
Write function PrintAllGrades, whose header is given below. PrintAllGrades prints a bar graph that represents the grades stored in the freq field of its Grade parameter along with the total number of grades. For example, the call PrintAllGrades(grades) where grades contains the data from the example of part (a) should produce the following output.
AAAA BBBBBBBBB CCCC DDD Total Grades = 20Complete function PrintAllGrades below the following header.
The class GradeInfo, whose declaration is shown below, is used to keep track of student grades.
The value of myGrades[0] represents the number of times a grade of 'A' occurs, myGrades[1] represents the 'B' occurrences, myGrades[2] the 'C' occurrences, and myGrades[3] the 'D' occurrences.
The private member function CharToIndex, shown above, should be used to convert a character representing a grade to an index into myGrades.
Assume that grades are stored in a file one per line with no blank lines. For example, consider the following text filegradeFile.
gradeFile A A B B C B D A D D C B B B C B A B B C
If the three statements below are executed
Assume that input is bound to a correctly formatted file of grades.
Complete member function Read below the following header
Part B Write member function PrintBarGraph, whose header is given below. PrintBarGraph prints a bar graph that represents the grades as read by a call of member function Read, i.e., as stored in the private section of a GradeInfo variable. For example, executing the three statements below
AAAA BBBBBBBBB CCCC DDD Total Grades = 20Complete member function PrintBarGraph below the following header.
For example, a character matrix puzzle with 8 rows and 12 columns (where puzzle.numrows() == 8 and puzzle.numcols() == 12) is shown below.
Write function NumSpaces, whose header is given below. NumSpaces returns the number of positions in the puzzle (including the border) that contain the character ' ' (a space). For example, in the puzzle above, there are 12 spaces in the row with index 0, there are 4 spaces in the row with index 3, and the entire puzzle contains 66 spaces.
Complete function NumSpaces below the following header.
Part B
Write function PrintWordsInRow, whose header is given below. PrintWordsInRow prints the words in the row with index given by parameter r, one word per line. In the example puzzle above, PrintWordsInRow(puzzle,1) produces the following output:
CLASSNote that C is not printed because, by definition, words are at least 2 characters long.
PrintWordsInRow(puzzle,3) produces the following output:
FALSE FOR
Complete function PrintWordsInRow below the following header. Assume that PrintWordsInRow is called only with parameters that satisfy its precondition.
Part C
Write function PrintAllWords, whose header is given below. PrintAllWords prints all the words (both horizontal and vertical) in the puzzle, one word per line. The words in the puzzle can be printed in any order.
In writing PrintAllWords, you may call function PrintWordsInRow of part (b). Assume that PrintWordsInRow works as specified, regardless of what you wrote in part (b).
You may also call function PrintWordsInColumn, declared below. (You do not need to write the body of PrintWordsInColumn.)
Complete function PrintAllWords below the following header.
Assume that binary trees are implemented using the following declarations
Part A
Write function CountNum whose header is given below. CountNum returns the number of nodes in tree T that contain the value key.
For example, if T is as pictured below, CountNum(T,0) returns 2, CountNum(T,1) returns 3, and CountNum(T,2) returns 0.
Complete function CountNum below the following header
Part B
Write function Insert whose header is given below. Insert creates a new node containing the value key and inserts this new node between the node pointed to by P and either P's right child (if the direction is 'R') or P's left child (if the direction is 'L'). If direction is 'R', the right child of P becomes the right child of the new node and the new node becomes the right child of P. If direction is 'L', the left child of P becomes the left child of the new node and the new node becomes the left child of P.
For example
Complete function Insert below the following header. Assume that Insert is called only with parameters that satisfy its precondition.
Part C
Write function Separate, whose header is given below. Separate modifies tree T in the following way: for each occurrence of a parent and child that contain the same value n, a new node is inserted between them containing the value n-1.
For example:
In writing Separate you may call the function Insert of part (b). Assume that Insert works as specified, regardless of what you wrote in part (b).
const int SIZE = some positive integer;
Part A
Write function Hash, whose header is given below. Hash returns the hash value of its parameter nn. The hash value is an integer in the range 0..SIZE-1, computed as follows:
For example, if SIZE = 5:
Values of first | Value returned by | ||
---|---|---|---|
value of nn | and last chars | sum | Hash(nn) |
Ken | 75, 110 | 185 | 0 |
Susan | 83, 110 | 193 | 3 |
Mark | 77, 107 | 184 | 4 |
Cary | 67, 121 | 188 | 3 |
Don | 68, 110 | 178 | 3 |
X | 90, 90 | 180 | 0 |
Complete the function Hash below the following header. In writing Hash you may call member functions of the class Name (Name::length, Name::operator[], Name::Equal) and the overloaded operator == . You will not receive full credit if your code relies on a particular implementation of Name. Assume that function Hash is called only with parameters that satisfy its precondition.
Part B
Assume that the hash table will be implemented using an apvector of linked lists, indexed from 0 to SIZE-1. The list in the kth element of the vector will hold all names with hash value k that have been inserted into the hash table. If no inserted name has hash value k, then the kth element of the vector will be NULL/0. For example, if SIZE is 5 and the names Ken, Susan, Mark, Cary, Don, and X have been inserted into the hash table, then the vector will be as shown below.
Assume that nodes for the linked list have been declared as follows:
Write function Lookup, whose header is given below. Lookup should return true if the parameter nn is in the hash table table; otherwise it should return false. Lookup should use the hash value of nn to identify the list that might contain nn; then it should search for nn in the list. For example, if variable Table represents the hash table shown on the previous page:
Value returned by | Value returned by | |
---|---|---|
value of nn | Hash(nn) | Lookup(Table,nn) |
Susan | 4 | true |
Gail | 5 | false |
Chris | 3 | false |
In writing Lookup you may call member functions of the class Name and the overloaded operator == for Name values. You will not receive full credit if your code relies on a particular implementation of Name. You may also call the function Hash of part (a). Assume that Hash works as specified, regardless of what you wrote in part (a). Assume that function Lookup is called only with parameters that satisfy its precondition.
Complete function Lookup below the following header.
Part A
Write member function HashValue, whose header is given below. HashValue returns the hash value of a Name object. The hash value is computed as follows:
For example:
Number for each | Value returned by | |
---|---|---|
value of Name | character | HashValue() |
Ken | 75*1 + 101*2 + 110*3 | 607 |
Susan | 83*1 + 117*2 + 115*3 + 97*4 + 110*5 | 1600 |
Mark | 77*1 + 97*2 + 114*3 + 107*4 | 1041 |
Cary | 67*1 + 97*2 + 114*3 + 121*4 | 1087 |
Don | 68*1 + 111*2 + 110*3 | 620 |
Y | 89*1 | 89 |
Complete the member function HashValue below the following header. In writing HashValue you may call member functions of the class Name (Name::length, Name::kthChar, Name::operator[], Name::Equal) and the overloaded operator == . You will not receive full credit if your code relies on a particular implementation of Name. Assume that the precondition of member function HashValue is satisfied whenever it is called.
Part B
Assume that the hash table will be implemented using an apvector of linked lists. The list in the kth element of the vector will hold all names that have been inserted into the hash table and that have a hash value that when divided by the size of the vector is equal to k. The kth element of the vector will be NULL/0 if no values have been inserted into the k-th linked list. For example, if the size of the vector is 5, and the names Ken, Susan, Mark, Cary, Don, and Y have been inserted into the hash table, then the vector will be as shown below.
The declaration for templated class hashTable is shown below:
Write member function Contains, whose header is given below. Contains should return true if the parameter key is in the hash table; otherwise it should return false. Contains should use the hash value of key to identify the list that might contain key; the index of the linked list in myTable is the remainder when key.HashValue() is divided by mySize. For example, if variable Table represents the hash table shown on the previous page (so that mySize has the value 5):
index of linked list | Value returned by | ||
---|---|---|---|
value of key | key.HashValue() | in myList | Table.Contains(key) |
Susan | 1600 | 0 | true |
Gail | 1044 | 4 | false |
Chris | 1612 | 2 | false |
In writing Contains you may call member functions of the class Name and the overloaded operator == for Name values. You will not receive full credit if your code relies on a particular implementation of Name. Assume that function Name::HashValue from part (a) works as specified, regardless of what you wrote in part (a).
Complete function Contains below the following header.