java, 16,17 18,20? returns the sun of the values st called sunArray that accepts
ID: 3607551 • Letter: J
Question
java, 16,17 18,20?
returns the sun of the values st called sunArray that accepts a reference to an array o· stored in the array ints. If the two ints in the e a nethod called sunsubArray that accepts a reference to an arrayf vajues stored are valid indices, then the method should return the sum of the vai en re Indices are betveen the tuo ints (Inclusive). i the tas ints are net valla Probles 18) Suppose tha val t the variables a and b are references to arrays of integers. what is the after the following statements have been executed? Explain why. Your ue of b[0] and b[1] explanatlon should include a simple dravin g of Java b - aj a(1] 4 the following P ass Probl t- a[1] * a[a.length-1-11; t"result 2 containing integers from 1 to n-1, inclusive, with Problem 20) Let A be exactly one number repeated. Write a method an array of size n public static int findRepeatedNumber(int[] A) that returns the value of the repeated number in the array A. (int: Use two nested for-loops.) write a sin gn (a) DeclareExplanation / Answer
16
double sumArray(double[] array) {
double sum = 0;
for(int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
17
double sumSubArray(double[] array, int start, int end) {
double sum = 0;
if ( start >= 0 && end < array.length) {
for(int i = start; i <= end; i++) {
sum += array[i];
}
}
return sum;
}
18
a[0] = 1;
b[0] = 2;
b = a;
b[1] = 3;
a[1] = 4;
this will result in b to point to a in memeory
hence any assignment to a will also change b
so when b = a;
then b[0] is 1
now b[1] = 3 has set b[1] as 3 and a[1] as 3
and a[1] = 4 has set a[1] = 4 and b[1] = 4
so b[0] will be 1
b[1] = 4
20
public static int findRepeatedNumber(int[] A) {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
int n = A.length;
return sum - (n)*(n-1)/2;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.