Your activity: write a same java program that will accept 20 integer in a list and returns the index of the first  occurrence of the smallest element in the array. Also, write a program to test your method.     Write a Java method, smallestIndex, that takes as its parameters an int array and its size, and returns the  index of the (first occurrence of the) smallest element in the array.   public class Array_labExercise2  {   public static void main(String[] args)   {   int[] list = {56, 34, 67, 54, 23, 87, 66, 92, 15, 32, 55, 15, 88, 22, 30};   System.out.print("List elements: ");   printArray(list);   System.out.println();   System.out.println("Index of the (first occurenc of the) " + "smallest element in list is: "  + smallestIndex(list));   System.out.println("Smallest element in list is: " + list[smallestIndex(list)]);   }   public static void printArray(int[] list)   {   int counter;   for (counter = 0; counter < list.length; counter++)   System.out.print(list[counter] + " ");   }   public static int smallestIndex(int[] list)   {   int counter;   int minIndex = 0; //Assume first element is the largest   for (counter = 1; counter < list.length; counter++)   if (list[minIndex] > list[counter])   minIndex = counter;   return minIndex;   }  }  Output:

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question

Your activity: write a same java program that will accept 20 integer in a list and returns the index of the first  occurrence of the smallest element in the array. Also, write a program to test your method.

 

 

Write a Java method, smallestIndex, that takes as its parameters an int array and its size, and returns the  index of the (first occurrence of the) smallest element in the array.  

public class Array_labExercise2 

 public static void main(String[] args) 

 { 

 int[] list = {56, 34, 67, 54, 23, 87, 66, 92, 15, 32, 55, 15, 88, 22, 30}; 

 System.out.print("List elements: "); 

 printArray(list); 

 System.out.println(); 

 System.out.println("Index of the (first occurenc of the) " + "smallest element in list is: "  + smallestIndex(list)); 

 System.out.println("Smallest element in list is: " + list[smallestIndex(list)]); 

 } 

 public static void printArray(int[] list) 

 { 

 int counter; 

 for (counter = 0; counter < list.length; counter++) 

 System.out.print(list[counter] + " "); 

 } 

 public static int smallestIndex(int[] list) 

 { 

 int counter; 

 int minIndex = 0; //Assume first element is the largest 

 for (counter = 1; counter < list.length; counter++) 

 if (list[minIndex] > list[counter]) 

 minIndex = counter; 

 return minIndex; 

 } 

Output: 

 

 

Output:
---JGRASP exec: java Array_labExercise2
List elements: 56 34 67 54 23 87 66 92 15 32 55 15 88 22 30
Index of the (first occurenc of the) smallest element in list is: 8
Smallest element in list is: 15
---JGRASP: operation complete.
Transcribed Image Text:Output: ---JGRASP exec: java Array_labExercise2 List elements: 56 34 67 54 23 87 66 92 15 32 55 15 88 22 30 Index of the (first occurenc of the) smallest element in list is: 8 Smallest element in list is: 15 ---JGRASP: operation complete.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT