Practice Quiz 6 with Answer Key - Program Design and Development | C SC 227, Quizzes of Computer Science

Material Type: Quiz; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-erq
koofers-user-erq 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Practice 6 Quiz Name ________________________________ 50pts
ANSWERS
1. Complete method numberOf100s that uses recursion to return the number of elements in an
integer array that are equal to 100. No loops needed. These assertions must pass: (12 pts)
public int num berOf100s(int[ ] x, int n) {
if (n == 0)
return 0;
else if (x[n - 1] == 100)
return 1 + numberOf100s(x, n - 1);
else
return 0 + numberOf100s(x, n - 1); // 0 not necessary
}
-or-
public int num berOf100s(int[ ] x, int n) {
if (n == 0)
return 0;
else {
int hasOne = 0;
if (x[n - 1] == 100)
hasOne++;
return hasOne + numberOf100s(x, n - 1);
}
}
2. Complete method range that uses recursion to return the range of integers in an array. No
loops needed. You may use a helper method but you may not use instance variables. All
variables must be local. These assertions must pass: (13pts)
public int ran ge(int[] x, in t n) {
int maxVal = x[0];
int minVal = x[0];
return range(x, maxVal, minVal, n - 1);
}
private int range(int[] x, int maxVal, int minVal, int rightIndex) {
if (rightIndex < 0)
return maxVal - minVal;
else {
if (x[rightIndex] < minVal)
minVal = x[rightIndex];
if (x[rightIndex] > maxVal)
maxVal = x[rightIndex];
return range(x, maxVal, minVal, rightIndex - 1);
}
}
pf2

Partial preview of the text

Download Practice Quiz 6 with Answer Key - Program Design and Development | C SC 227 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Practice 6 Quiz Name ________________________________ 50pts

ANSWERS

1. Complete method numberOf100s that uses recursion to return the number of elements in an

integer array that are equal to 100. No loops needed. These assertions must pass: (12 pts)

public int numberOf100s(int[] x, int n) { if (n == 0) return 0; else if (x[n - 1] == 100) return 1 + numberOf100s(x, n - 1); else return 0 + numberOf100s(x, n - 1); // 0 not necessary }

  • or- public int numberOf100s(int[] x, int n) { if (n == 0) return 0; else { int hasOne = 0; if (x[n - 1] == 100) hasOne++; return hasOne + numberOf100s(x, n - 1); } }

2. Complete method range that uses recursion to return the range of integers in an array. No

loops needed. You may use a helper method but you may not use instance variables. All

variables must be local. These assertions must pass: (13pts)

public int range(int[] x, int n) {

int maxVal = x[0];

int minVal = x[0];

return range(x, maxVal, minVal, n - 1);

private int range(int[] x, int maxVal, int minVal, int rightIndex) {

if (rightIndex < 0)

return maxVal - minVal;

else {

if (x[rightIndex] < minVal)

minVal = x[rightIndex];

if (x[rightIndex] > maxVal)

maxVal = x[rightIndex];

return range(x, maxVal, minVal, rightIndex - 1);

3. To RecursiveList, add method get that uses recursion to get an element at the given index. No

loops needed. If the index is out of range, return null. These assertions must pass. (12pts)

// Complete get here (there is no size method, use the instance variable n) public E get(int index) {

if (index < 0 || index >= n)

return null;

else

return get(index, 0, first);

private E get(int index, int start, Node ref) {

if (index == start)

return ref.data;

else

return get(index, start + 1, ref.next);

4. To RecursiveList, add recursive method insertElement to returns the toString version of all elements

in a RecursiveList. No loop required. If the indexes are out of range, do not insert the element. These

assertions must pass: (13pts)

public void insertElementAt(int index, E element) {

if (index < 0 || index > n)

return; // Index out of bounds, exit method so n is never increased

else if (index == 0)

first = new Node(element, first);

else

insertElementAt(index, 1, element, first);

// Must have successfully inserted, so increase the size

n++;

private void insertElementAt(int index, int current, E element, Node ref) {

if (index == current)

ref.next = new Node(element, ref.next);

else

insertElementAt(index, current + 1, element, ref.next);