Finding the smallest element and shifting....
Hello again. I been trying to do this java HW all day but i am starting to get a headache lol. This HW has to do with getting the user to input 6 numbers and save them into an array. After that is to invoke the min method to return the index of the smallest element and display the min value within. Then to invoke the moveMin method to shift the elements after the smallest element, one position to the left and fill the last element with the smallest element. Most of the code was given to us from our professor and told us we don't have to change anything in the main method, but to add code in the last 2 methods which were left blank for us.....this is the given code :
public class Lab2 {
// Main method
public static void main(String[] args) {
double[] numbers = new double[6];
int index;
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter six different double numbers: ");
for (int i = 0; i < numbers.length; i++)
numbers[i] = input.nextDouble();
index = min(numbers);
System.out.println("The min is " + numbers[index]);
moveMin(numbers, index);
System.out.println("The re-ordered Array is: ");
for (int i = 0; i < numbers.length; i++)
System.out.print(numbers[i] + ", ");
}
public static int min(double[] list) {
}
public static void moveMin(double[] list, int position) {
}
}
----------------------------------------------------------------------------------------------------------------------------------
and this is what i have typed so far in the last 2 methods but i don't know if its even right :
public static int min(double[] list) {
double min = 0;
for (index = 1; index < numbers.length; i++){
if (numbers[min] > numbers[index])
min = index;
}
public static void moveMin(double[] list, int position) {
double temp = min[index];
My professors input was 2, 3, 4, 1, 5, 6. And it printed out min as 1.0, and the re-ordered array is : 2.0, 3.0, 4.0, 5.0, 6.0, and 1.0.
Again from my last thread, I am still kind of new to java. Last java class i took was a yr ago (had to take time off from school) so i forgot quit a bit
Thanks in advanced.
Re: Finding the smallest element and shifting....
Re: Finding the smallest element and shifting....
Your min method is more or less right. This line you've but index twice and then i for the last
Code Java:
for (index = 1; index < numbers.length; i++)
Change these all to i.
You've also not returned min which you need to do.
As for the moveMin list. Best way I can see is to store the value at numbers[min] then use a loop to move the elements after it one place back like this.
Code Java:
list[position] = list[position+1] ;
And then add the stored minimum value to the end of the array. Try it and post if you get any problems.
Re: Finding the smallest element and shifting....
i couldn't figure it out. the lines i added are :
public static int min(double[] list) {
double min = 0;
for (int i = 1; i < numbers.length; i++){
if (numbers[min] > numbers[index])
min = index;
return min;
}
public static void moveMin(double[] list, int position){
double temp = min[index];
for(int i = 1; i < numbers.length; i++){
list[position] = list[position + 1];
}
list[numbers.length + 1] = temp;
}
but i got errors, i tried to fix them but the errors it was giving me didn't make any sense. these are the errors :
Lab2.java:31: illegal start of expression
public static void moveMin(double[] list, int position){
^
Lab2.java:31: illegal start of expression
public static void moveMin(double[] list, int position){
^
Lab2.java:31: ';' expected
public static void moveMin(double[] list, int position){
^
Lab2.java:31: '.class' expected
public static void moveMin(double[] list, int position){
^
Lab2.java:31: ';' expected
public static void moveMin(double[] list, int position){
^
Lab2.java:31: ';' expected
public static void moveMin(double[] list, int position){
^
Lab2.java:40: reached end of file while parsing
}
Re: Finding the smallest element and shifting....
That looks about right, you're just missing a closing bracket for the for loop in the min method, this needs to go before the return statement. That should get rid of those errors.