How do I do this? Find the largest element in the \"numbers\" array. Savethe lar
ID: 3613962 • Letter: H
Question
How do I do this? Find the largest element in the "numbers" array. Savethe largestvalue in a variable named"largestNum". Save the index of the
largest value in"indexLargestNum"
Display "largestNum" and "indexLargestNum".
int numbers[] = { 11, 8, -7, 22, 3, 17, 6, -25, 10, 5,-12, -9, 14, 20, -13 } How do I do this? Find the largest element in the "numbers" array. Savethe largest
value in a variable named"largestNum". Save the index of the
largest value in"indexLargestNum"
Display "largestNum" and "indexLargestNum".
int numbers[] = { 11, 8, -7, 22, 3, 17, 6, -25, 10, 5,-12, -9, 14, 20, -13 }
Explanation / Answer
Loop through each element, comparing that element to the previouslylargest value. publicstatic void main(String[]args) { // your array int[] numbers= newint[] {11,8,-7,22,3,17,6,-25,10,5,-12,-9,14,20,-13}; // store largest numberand index // initialize todefault intindexLargestNum = -1; int largestNum= Integer.MIN_VALUE; // loop through eachelement in numbers for(int i = 0; ilargestNum) { // store new largest number andindex indexLargestNum = i; largestNum = numbers[i]; } } // output System.out.println("Largestnumber: "+largestNum); System.out.println("Index:"+indexLargestNum); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.