Explore Westonci.ca, the leading Q&A site where experts provide accurate and helpful answers to all your questions. Connect with professionals ready to provide precise answers to your questions on our comprehensive Q&A platform. Get quick and reliable solutions to your questions from a community of experienced experts on our platform.
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];
Thank you for choosing our platform. We're dedicated to providing the best answers for all your questions. Visit us again. We appreciate your visit. Our platform is always here to offer accurate and reliable answers. Return anytime. Keep exploring Westonci.ca for more insightful answers to your questions. We're here to help.