CSCI220 Final Exam
CSCI220 Final Exam
int SumOddVal(int n) {
int sum = 0;
for (int i = 1; i < n; i += 2) {
sum += i;
}
return sum;
}
2.
Output from calling the main function of the Maryland class is: Read it, Ship it,
Box it, and Buy it. Because Maryland class is inherited from State class, and State
class is inherited from Region class, Region class is inherited from Place class,
Place class is inherited from Object class. All four classes of printMe() will be
called.
Code output:
Read it.
Ship it.
Buy it.
Read it.
Box it.
Read it.
3.
● Initially: A = {4, 3, 6, 2, 5}
● ReverseArray(A,0,4) → A = {5, 3, 6, 2, 4}; ReverseArray(A,0 + 1,4 - 1)
● ReverseArray(A,1,3) → A = {5, 2, 6, 3, 4}; ReverseArray(A,1+1,3 -1)
● ReverseArray(A,2,2) → Nothing will be changed (2 < 2 is false); Function
stop.
5.
6.
7.
Executes a for loop that iterates n times, which n is the size of the array. In the for
loop, starting from position i = 1 of the array. Let the key equal to the value at this
position of the list, create a new variable j and let it equal to i -1. Next is to have a
while loop that checks if j >= 0 AND arr[j] > key. If so, move elements that are
greater than key one position ahead. After the while loop, let key equals arr[j + 1];
after the for loop is finished, the elements in the array are in ascending order,
therefore, execute another for loop that prints the last 10 elements of the array. It is
an insertion sort. It's time complexity is O(n^2).
8.
9.
10.
11.
12.
13.
If the sequence S is implemented with a doubly linked list, then, in order to let the
element in p’s position to be printed first, we need to let the prev pointer of the
node in p position points to the header, and the next potiner of the header points to
the p(p-> next = header-> next; header->next->pre = p; header->next = p; p->pre =
header). Not only that, we also need to link the nodes that were previously linked
with p’s node together. (p->pre->next = p->next; p->next->pre = p->pre)
14.
15.
16
A. Preorder Traversal: 1 2 4 5 3 6 7
B. Inorder Traversal: 4 2 5 1 6 3 7
C. Inorder Traversal: 4 5 2 6 7 3 1
17