Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Finding the smallest element and shifting....

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by DeadlySwordz; February 13th, 2011 at 04:54 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding the smallest element and shifting....

    bump...anyone?

  3. #3
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default 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
    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.
    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.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    }

  5. #5
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default 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.

Similar Threads

  1. Output Largest and Smallest Integers using only If statements
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 05:55 PM
  2. [SOLVED] My smallest and largest integers will not change.
    By toiletsauce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2011, 07:50 PM
  3. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  4. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  5. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM