therrontelford

Smallest Element

Jan 29th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public class SmallestElement {
  2.  
  3.     public static void main(String[] args) {
  4.        
  5.         double[] array2 = {0, 1.2, 9.6, 6.8, -1.3,-8.5, -8.5, 8.2, -8.5};
  6.         int smallestIndex=0;
  7.         double smallestNumber= array2[0];
  8.         for (int i =0; i<array2.length-1; i++) {
  9.             if (smallestNumber > array2[i+1]) {
  10.                 smallestNumber=array2[i+1];
  11.                 smallestIndex= i+1;
  12.             }
  13.         }
  14.         // this print statement is not needed for the code.  It is only for checking to see if it works
  15.         System.out.println("smallest number is "+ smallestNumber+ " and smallest index is "+smallestIndex);
  16.  
  17.     }
  18.  
  19. }
Advertisement
Add Comment
Please, Sign In to add comment