1. Write code that fills an array values with each set of numbers below using as few statements as possible (i.e., consider using a loop for each).

a. 1 2 3 4 5 6 7 8 9 10
b. 2 4 6 8 10 12 14 16 18 20
c. 1 4 9 16 25 36 49 64 81 100
d. 0 0 0 0 0 0 0 0 0 0
e. 1 4 9 16 9 7 4 9 11
f. 0 1 0 1 0 1 0 1 0 1
g. 0 1 2 3 4 0 1 2 3 4

2.Consider the following array:

int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };

What is the value of total after each of the following loops complete?

a. int total = 0; for (int i = 0; i < 10; i++) { total = total + a[i]; }

b. int total = 0; for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; }

c. int total = 0; for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; }

d. int total = 0; for (int i = 2; i <= 10; i++) { total = total + a[i]; }

e. int total = 0; for (int i = 1; i < 10; i = 2 * i) { total = total + a[i]; }

f. int total = 0; for (int i = 9; i >= 0; i--) { total = total + a[i]; }

g. int total = 0; for (int i = 9; i >= 0; i = i - 2) { total = total + a[i]; }

h. int total = 0; for (int i = 0; i < 10; i++) { total = a[i] - total; }

3.Consider the following array:

int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };

What are the contents of the array a after each of the following loops complete?

a. for (int i = 1; i < 10; i++) { a[i] = a[i - 1]; }

b. for (int i = 9; i > 0; i--) { a[i] = a[i - 1]; }

c. for (int i = 0; i < 9; i++) { a[i] = a[i + 1]; }

d. for (int i = 8; i >= 0; i--) { a[i] = a[i + 1]; }

e. for (int i = 1; i < 10; i++) { a[i] = a[i] + a[i - 1]; }

f. for (int i = 1; i < 10; i = i + 2) { a[i] = 0; }

g. for (int i = 0; i < 5; i++) { a[i + 5] = a[i]; }

h. for (int i = 1; i < 5; i++) { a[i] = a[9 - i]; }

4.Write a loop that fills an array values with ten random numbers between 1 and 100.

Write code for two nested loops that fill values with ten different random numbers between 1 and 100.

5.Write Java code for a loop that simultaneously computes both the maximum and minimum of an array.

6.What is wrong with each of the following code segments?

a. int[] values = new int[10];
for (int i = 1; i <= 10; i++) {
values[i] = i * i;
}

b. int[] values;
for (int i = 0; i < values.length; i++) {
values[i] = i * i;
}

7.What is an index of an array? What are the legal index values? What is a bounds error?

8.Write a loop that reads ten numbers and a second loop that displays them in the opposite order from which they were entered.

9.Trace the flow of the linear search loop as shown below, where values contains the elements 80 90 100 120 110. Show two columns, for pos and found. Repeat the trace when values contains 80 90 100 70.

int searchedValue = 100;
int pos = 0;
boolean found = false;
while (pos < values.length && !found) {
if (values[pos] == searchedValue) {
found = true;
} else {
pos++;
}
}
if (found) { System.out.println("Found at position: " + pos); }
else { System.out.println("Not found"); }

10.Trace both mechanisms for removing an element described as shown below. Use an array valueswith elements 110 90 100 120 80 (i.e., currentSize == 5), and remove the element at index 2.

remove code 1:

values[index] = values[currentSize-1];
currentSize--;

remove code 2:

for (int i=index+1; i values[i-1] = values[i];
}
currentSize--;

11.Trace the flow of the loop in the code shown below with the values array containing 32, 54, 67.5, 29, 35. Show two columns, one with the value of i and one with the output.

for (int i = 0; i < values.length; i++) {
if (i > 0) {
System.out.print(" | ");
}
System.out.print(values[i]);
}

12.Trace the flow of the loop in the linear search code in question 9, where values contains the elements 80 90 100 120 110. Show two columns, for pos and found.

Repeat the trace when values contains the elements 80 90 120 70.

13.Trace the algorithm for removing an element using each of the mechanisms described in question 10. Use an array values with elements 110 90 100 120 80 (currentSize == 5), and remove the element at index 2.

14.Write an algorithm that removes all negative values from an array, preserving the order of the remaining elements.

15.Suppose values is a sorted array of integers. Write a Java code fragment that inserts a new value in its proper position so that the resulting array stays sorted.

16.A run is a sequence of adjacent repeated values.

Write an algorithm for computing the length of the longest run in an array. For example, the longest run in the array with elements

1 2 5 5 3 1 2 4 3 2 2 2 2 3 6 5 5 6 3 1

has length 4.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.