I have to create a ten element array with numbers between 0 and 99 inclusive, and then identify the largest and smallest numbers in the array and their locations. I then need to swap those two numbers, and redisplay the array showing those two elements in their swapped locations. The problem I am running into, is do I use a method for the identification of the two numbers (and if so, is a brand new array created in the method) or should I just identify and swap programmatically (e.g. not use a method); and then how would I accomplish either of these?
Here is the code that I have so far:
/*
* Create a ten element array with random numbers between
* 0 and 99, identify the largest and smallest elements and
* their locations and swap them.
*/
package swapmaxmin;
public class SwapMaxMin {
public static void main(String[] args) {
int [] mySwap = new int[10]; //Create a ten element array
int i;
for (i = 0; i < mySwap.length; i++)
mySwap[i] = (int)(Math.random() * 99); //Create ten random numbers between 0 and 99
for (i = 0; i < mySwap.length; i++)
System.out.println("mySwap[" + i + "] = " + mySwap[i]); //Print original array showing elements and position
}
public static int minMaxMethod(int myMin, int myMax) { //As you can see I started creating a method but stopped, not being sure if it would create a new array and how that would be handled if that is indeed what it would or should do.
int findNums;
}
}

New Topic/Question
Reply



MultiQuote









|