Question

In: Computer Science

write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.

write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array

Solutions

Expert Solution

Below code has comment section explains the program flow.

Code:

import java.util.Scanner;

public class Second_Small{

public static void main(String []args)
{
int n, res;
  
Scanner s = new Scanner(System.in);
  
System.out.print("Enter no. of elements you want in array(Minimum 2):");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
//Get array elem#ents
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
//calling method to find 2nd smallest element in the array
res = index_second_smallest(a);
  
System.out.println("Second smallest element in the array: " + a[res]);
System.out.println("Index of second smallest element in the array: " + res);
}

public static int index_second_smallest(int[] arr)
{
int i, j, count, small, sec_small;
int index[] = new int[10];
  
small=arr[0];
//find the smallest element in the array
for(i=0;i {
if(arr[i] {
small=arr[i];
}
}
  
//find the second smallest element in the array by comparing the smallest element
sec_small=arr[0];

for(i=0;i {
if(arr[i] {
sec_small=arr[i];
//index = i;
}
}
  
//If more than 1 sec_small elements in the array, find the second smallest index
j=0;
count= -1;
  
for(i=0;i {
if(arr[i]==sec_small)
{
index[j]=i;
j++;
count++;
}
}
  
if(count==0)
return index[0];
else
return index[1];
}
}

Output:


Related Solutions

Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Problem Description: Write a method that returns the smallest element in a specified column in a...
Problem Description: Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex) Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns. The program will then invoke the columnMin method and displays the minimum value of all columns.
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT