Assistance needed with array loop - java -


for assignment, unable last method loop again , find next largest element in series of random numbers. input solve problem great. thank you.

public static void main(string[] args) {

    int[] array = randomintarray (10);        sortarray (array);  }    public static int randomint (int low, int high){ // create serie of random numbers     int x = 0;     (int = 0; < 10; i++){         x = (int)(math.random ()* (high - low) +low);      }       return x;  } public static int[] randomintarray (int n) { // size of array     int[] = new int [n];     (int = 0; <a.length; i++){         a[i] = randomint (-5, 15);     }     return a; }  public static int indexofmaxinrange (int[] a, int low , int high){      int index = low;     (int = low +1; < high; i++){         if (a[i] > a[index]){ // if position of greater index             index = i; // index equal position highest element         }              }     return index;   } public static int swapelement (int []a, int index , int i){     int tmp =0;    for(i = 0; < (a.length); i++) {     tmp = index;      a[i]= a[tmp] ;   }      return a[tmp] ; }  public static void sortarray (int[] array){ //the sortarray calls indexofmaxinrange index of largest element, uses swapelement swap index's position position a[i].      (int b= 0; b <array.length; b++){      system.out.println (array[b]+"\t"+ b); // print out original list of random numbers     }     system.out.println ();      (int = 0; <array.length; i++){         int index = indexofmaxinrange (array, 0, 10 );     int sort = swapelement (array, index, 0);      system.out.println (sort+"\t"+ i);           }  } 

this looks homework, since sorting array handled umpteen available libraries, going try give hints rather full answer.

your randomint function 10 times more work needs to. won't change behavior of program it's waste.

your swapelement function doesn't @ right. don't need return anything, , don't need loop. swap elements in positions "index" , "i". maybe call arguments "a" , "b" or "pos1" , "pos2" less confusing.

inside sortarray, after find largest element , put in position 0, need keep going , find 2nd largest element , put in position 1, , forth. need have loop this. have pieces ready. hint: can find 2nd largest element, after you've put largest 1 in position 0, finding largest remaining element (i.e. call indexofmaxinrange pass 1 start position).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -