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.

Each of the parts a. through c. below is preceded by a comment indicating what the code should do. There is a least one problem with each section of code and it fails to do what was intended. Show how to modify or rewrite the code the code so that it does work as intended. If there are multiple problems, correct each one. Assume that all variables used have already been declared.
a. // INTENT: given an array arr of int values // set small equal to the smallest of the array values small = arr[0]; for (int j = 0; j < arr.length-1; j++) if (small b. // INTENT: compute the average of all n values in the integer array arr; 1/ compute the average as a double and store it in the double variable avg int sum = 0; int n = arr.length; for (int k = arr.length; k <= 0; k--) sum = sum + arr[k]; double avg = sum /n; c. //INTENT: the array b should have the values of array a in reverse order int[] a = {1,2,3,4,5); int[] b = new int[5]; for (int i=0; i<=a.length; i++) b[i-1] = a[a.length-i];


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.