Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One common algorithm is finding the largest dement m the array-that a. the array

ID: 3819624 • Letter: O

Question

One common algorithm is finding the largest dement m the array-that a. the array element with the largest value-as well at finding the location of the largest element in the array. for all sections, assume that the array contains unique value (no repetition). We Will Start by finding the largest value Initially. we assume that the first element in the list is the largest element and create a variable maxValue that is initialized to the first element in the array We then compare the element that maxValue is storing with every dement in the list Whenever we find an element in the array larger than the element that maxValue is Storing, we update maxValue so that it stores the index of the newer larger element. Algorithm ArraylargestElement Declare and initialize maxValue to the first element in the array Use a FOR loop to traverse the array.//You already checked the first element, so you should start the loop at the second element If the element at this index is larger than maxValue Update maxValue Output maxValue Write a program MyArray that declares an array (you can preload the array elements instead of using an input statement) containing the following elements: 45, 38. 27.46. 81. 72. 56. 61.20. 48. 76.91. 57. 35. and 78. and outputs the largest value. Re-test your program using the following testing cases: 300, 38, 27, 46. 81, 72, 56, 61. 20. 48. 76. 91, S7. 35. and 78 45. 38.27.46.81.72.56.61.20.48. 76.91. 57.35. and 800 100 Modify the program described above so that it outputs the index where the largest value is positioned. Use a variable maxlndex to store the location. Include in the program described above a variable sum that. while traversing the array to find the largest element, sums up all the elements and outputs the result At the end of the program, write a FOR loop that prints the array in reverse (you are not reversing the array; you are simply printing it in reverse order)

Explanation / Answer

JAVA :

import java.util.Arrays;

public class Main{

public static void main(String[] args) {
int array[] = {2,1,5,3,4};
System.out.println("Array is "+Arrays.toString(array));
int pos = 0;
int maxValue = array[0];
int sum = array[0];
for(int i=1;i<array.length;i++){
if(array[i]>maxValue){
maxValue = array[i];
pos = i;
}
sum += array[i];
}
System.out.println("MaxValue at "+pos);
System.out.println("Sum is "+sum);
System.out.println("Array in reverse");
for(int i=array.length-1;i>=0;i--){
System.out.println(array[i]);
}
}
}

OUTPUT :

Array is [2, 1, 5, 3, 4]
MaxValue at 2
Sum is 15
Array in reverse
4
3
5
1
2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote