Hope you all are having a good day. I am trying to return the index of the second largest value from a set of numbers of size n(inputted from the user). I was able to find the largest value as well as the second largest value, but it appears that whenever I try to output my secondLargestIndex variable of int type, regardless of what I do, it constantly = 0 for output. I have updated the index in the algorithm with the variables largestIndex, and secondLargestIndex, and put them in global scope of the entire program(in the main method), so if I'm not mistaken, the variables are constantly being updated in global scope as the program runs. There are no duplicates of any variables which makes me even more sure, which is why I'm confused. I've tracked them and modified them as appropriate while keeping track of the values, yet still outputs 0. If you could take a look at my code and provide some feedback, I would really appreciate it. Thank you for taking the time to help me.
import java.util.*;
public class hwk3_1 {
/* write an algorithm to find the 2nd largest element in a set containing n entries
*/
public static void main( String[] args) {
// declare needed variables
int[] arr;
int largest = 0, second_largest = 0, n = 0, largestIndex = 0,
secondLargestIndex = 0;
Scanner input = new Scanner(System.in);
// prompt user for size n
System.out.print("Please enter the size of your array: ");
n = input.nextInt();
arr = new int[n];
// take in input of size n from user
System.out.print("Please enter your set of int values: ");
for(int i = 0; i < n; i++){
arr[i] = input.nextInt();
}
// display to user their set of numbers
System.out.println("You entered: " + Arrays.toString(arr));
// output to the user what the 2nd largest number is
System.out.println("Second largest is: " + secondLargest(arr, largest,
second_largest, largestIndex, secondLargestIndex));
System.out.println("Second largest Index is: " +
findIndex_2ndLargest(arr, second_largest));
}// end main
// method secondLargest
public static int secondLargest(int[] arr, int largest, int second_largest,
int largestIndex, int secondLargestIndex) {
// traverse array to find 2nd largest num
for (int i = 0; i < arr.length; i++) {
// if the current index's value is greater than current largest,
then modify max & max2
if (largest < arr[i]) {
second_largest = largest;
secondLargestIndex = largestIndex;
largest = arr[i];
largestIndex = i;
// now we can traverse and see whether if there is a num larger than
2ndmax
} else if (second_largest < arr[i])
second_largest = arr[i];
secondLargestIndex = i;
}// end for loop
return second_largest;
}// end method secondLargest
// find the index of the second largest num
public static int findIndex_2ndLargest(int[] arr, int secondLargest){
int index = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] == secondLargest)
index = i;
}
return index;
}
}
MY OUTPUT Please enter the size of your array: 5 Please enter your set of int values: 5 4 3 2 1 You entered: [5, 4, 3, 2, 1] Second largest is: 4 Second largest Index is: 0
DESIRED OUTPUT Please enter the size of your array: 5 Please enter your set of int values: 5 4 3 2 1 You entered: [5, 4, 3, 2, 1] Second largest is: 4 Second largest Index is: 1