1) Write a loop that fills an array values with ten random numbers between 1 and
ID: 3812083 • Letter: 1
Question
1) 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.
2) Write Java code for a loop that simultaneously computes both the maximum and minimum elements of an array.
3) Write a loop that reads ten numbers and a second loop that displays them in the opposite order from which they were entered.
4) Trace the flow of the following linear search loop, 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");
}
5) Trace the flow of the loop from question 4, 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.
6) Implement each of the operations on partially filled arrays below as a method.
a.Sort the elements in decreasing order.
b.Print all elements, separated by a given string.
c.Count how many elements are less than a given value.
d.Remove all elements that are less than a given value.
e. Place all elements that are less than a given value in another array.
7) A run is a sequence of adjacent repeated values.
Implement an int method that computes and returns 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
8) Solve the following problem by sorting the array first.
How do you need to modify the algorithm for computing the total?
You are given the quiz scores of a student.You are to compute the final quiz score, which is the sum of all scores after dropping the lowest one.For example, if the scores are
8 7 8.5 9.5 7 4 10
then the final score is 50.
9) Solve the following problem using an algorithm that removes and inserts elements instead of switching them.
You are given an array whose size is an even number, and you are to switch the first and the second half.
For example, if the array contains the eight numbers
9 13 21 4 11 7 1 3
then you should change it to
11 7 1 3 9 13 21 4
Write the pseudocode for the algorithm, assuming that methods for removal and insertion exist.
Trace the algorithm and explain why it is less efficient than the swapping algorithm shown below.
i=0
j = size/2
While (i < size / 2):
Swap elements at positions i and j
i++
j++
End While
10) It turns out that we must be careful about updating the index value when we remove elements from an array list, since the size of the list changes inside the loop.
Show how we can avoid this problem by traversing the array list backwards.
11) Are each of the following statements about arrays true or false? If false, why?
a.All elements of an array are of the same type.
b. Arrays cannot contain strings as elements.
c. A method cannot change the length of an array argument.
12) Implement a void method that removes all negative values from an array, preserving the order of the remaining elements.
13) Implement a void method that, given a sorted array of integers, inserts a new value in its proper position so that the resulting array stays sorted.
Explanation / Answer
1) 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.
Answer::
int a[] = new int[10];
Random r = new Random();
for(int i=0;i<a.length; i++){
a[i] = r.nextInt(100)+1;
}
2) Write Java code for a loop that simultaneously computes both the maximum and minimum elements of an array.
Answer:
import java.util.Random;
public class Test {
public static void main(String[] args) {
int a[] = new int[10];
Random r = new Random();
for(int i=0;i<a.length; i++){
a[i] = r.nextInt(100)+1;
}
int max = a[0];
int min = a[0];
for(int i=0;i<a.length; i++){
if(max < a[i]) {
max = a[i];
}
if(min > a[i]) {
min = a[i];
}
}
System.out.println("Max: "+max);
System.out.println("Min: "+min);
}
}
3) Write a loop that reads ten numbers and a second loop that displays them in the opposite order from which they were entered.
Answer:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int a[] = new int[10];
Scanner scan = new Scanner(System.in);
for(int i=0;i<a.length; i++){
a[i] = scan.nextInt();
}
for(int i=a.length-1; i>=0;i--){
System.out.println(a[i]);
}
}
}
4) Trace the flow of the following linear search loop, where values contains the elements 80 90 100 120 110.
Answer:
Found at position: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.