IDNLearn.com provides a comprehensive platform for finding accurate answers. Our platform offers comprehensive and accurate responses to help you make informed decisions on any topic.
Sagot :
Answer:
The correction is as follows:
(a)
small = arr[0];
for (int j = 0; j < arr.length; j++){
if (small > arr[j]) {
small = arr[j]; } }
(b)
double sum = 0;
int n = arr.length;
for (int k = arr.length-1; k >= 0; k--)
sum = sum + arr[k];
double avg = sum /n;
(c)
int[] a = {1,2,3,4,5};
int[] b = new int[5];
for (int i=0; i<a.length; i++)
b[i] = a[a.length-i-1];
Explanation:
Required
Correct each of the given code
a.
This line is correct; it initializes the smallest to the first element
small = arr[0];
This iterates through the array; however, the last element is left out.
for (int j = 0; j < arr.length-1; j++)
So, the correct code is: for (int j = 0; j < arr.length; j++){
The expected remaining part of the program which compares the elements of the array is missing
I've completed the code [See the answer section]
b.
By syntax this line is correct. However, for the average to be calculated as double; sum has to be declared as double
int sum = 0;
So, the correct code is: double sum = 0;
This line correctly calculates the length of the array
int n = arr.length;
This iteration will create an endless loop
for (int k = arr.length; k <= 0; k--)
So, the correct code is: for (int k = arr.length-1; k >= 0; k--)
This correctly add up the elements of the array
sum = sum + arr[k];
This correctly calculate the average of the array elements
double avg = sum /n;
(c)
The array a is incorrectly initialized because there is no matching end curly brace
int[] a = {1,2,3,4,5);
So, the correct code is: int[] a = {1,2,3,4,5};
This correctly create array b with 5 elements
int[] b = new int[5];
This iterates through a; however, the last element is left out
for (int i=0; i<=a.length; i++)
So, the correct code is: for (int i=0; i<a.length; i++)
This will create an out of bound error
b[i-1] = a[a.length-i];
So, the correct code is: b[i] = a[a.length-i-1];
We value your presence here. Keep sharing knowledge and helping others find the answers they need. This community is the perfect place to learn together. Thank you for visiting IDNLearn.com. For reliable answers to all your questions, please visit us again soon.