/** * Week 1 Java program - FindMax * @author Leszek A Gasieniec */ import java.util.*; public class FindMax { public static void main(String[] args) { int CurrentMax=0; int PositionMax=0; int Cost=0; char x=' '; int[] A; A = new int[8]; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // DO NOT CHANGE ANYTHING ABOVE THIS LINE // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // feed the input array A with eight integers A[0]=1; A[1]=5; A[2]=2; A[3]=6; A[4]=3; A[5]=8; A[6]=7; A[7]=4; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // LOOK AT BUT DO NOT CHANGE ANYTHING BELOW THIS LINE // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // run FindMax algorithm CurrentMax = A[0]; PositionMax = 0; // priniting part starts here System.out.println("Round: "+0); for (int j = 0; j< 8; j++) { if (j == PositionMax) { System.out.print("<["+A[j]+"] "); } else {System.out.print(" "+A[j]+" ");} } // printing part ends here for (int i = 1; i < 8; i++) { if (CurrentMax < A[i]) { CurrentMax = A[i]; PositionMax = i; Cost++; } // priniting part starts here System.out.println(" "); System.out.println(" "); System.out.println("Round: "+i); for (int j = 0; j< 8; j++) { if (j == i) {x='<';} else {x=' ';} if (j == PositionMax) { System.out.print(x+"["+A[j]+"] "); } else {System.out.print(x+" "+A[j]+" "); } // printing part ends here } } System.out.println(" "); System.out.println(" "); System.out.println("The maximum value "+CurrentMax+" is located at position "+PositionMax); System.out.println("The cost, i.e., # of loop for body executions is "+Cost); } }