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.
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...
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...
Take an unknown number of integers as input and print the second smallest number. If there...
Take an unknown number of integers as input and print the second smallest number. If there is no second smallest number in the sequence the program should print an error message and quit. Write a function that takes a vector as argument and that returns an int containing the second smallest number. If no such number exists, the function should throw an exception that in turn is caught in main. Your program should compute its answer with a complexity no...
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT