Results 1 to 4 of 4
  1. #1
    Awt582 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Swapping numbers in an array

    Alright I have the program I am intended to write<i> basically</i> completely running correctly minus one little snag up I can't seem to wrap my mind around to fix.


    heres the actual question and the code i've worked up
    Java Code:
    Consider a integer array called myArray of size 20 that
    initially contains random numbers between 1 and 1000. You want to write a method,
    blockMax that accepts myArray and two integers, start and end as parameters.
    blockMax considers the sub-array of myArray denoted by start and end, and finds the
    maximum element in that sub-array. Subsequently, blockMax swaps the max element
    and the last element of the sub-array. For example, if initially
    myArray is {95, 90, 104, 85, 90, 110, 105, 88, 90, 97, 102, 77, 100, 90, 109,
    80, 34, 9, 567, 876}
    and we make the call
    blockMax(myArray, 4, 10)
    then the result reflected in myArray will be
    {95, 90, 104, 85, 90, 102, 105, 88, 90, 97, 110, 77, 100, 90, 109, 80, 34, 9,
    567, 876}
    my trouble is in swapping the last element to where the max element was located in the array i can only seem to replace the last element with the actualy maximum leaving the maximum in two places on the array

    Java Code:
    import javax.swing.JOptionPane;
    public class MaxVal{
            public static void main(String[] args) {
                    int[] myArray = new int[20];
            String startStr, endStr;
            int start, end;
            startStr = JOptionPane.showInputDialog("Input start..(0-19)");
            start = Integer.parseInt(startStr);
            endStr = JOptionPane.showInputDialog("Input end..(start-19)");
            end = Integer.parseInt(endStr);
            
                    for (int i = 0; i < myArray.length; i++){
                            myArray[i] = (int)(Math.random() * 1000);
                    }
                    
                    System.out.println("myArray is: ");
                    
                                    
                    printArray(myArray);
                            
                    System.out.println("The Array after blockMax: ");
                    blockMax(myArray, start, end);
    
                    printArray(myArray);
            }
            
            static void printArray (int[] array) {
                    for (int i = 0; i < array.length; i++)
                            System.out.print(array[i] + "  ");
                    	System.out.println();
            }
            
            static void blockMax(int[] array, int inputStart, int inputEnd) {
                    int max = array[inputStart];
                    for (int i = inputStart; i < inputEnd; i++){
                            if (max < array[i]){
                                    max = array[i];
                                   int temp = array[inputEnd];
                                    array[inputEnd] = max;
                                    
                            }
                    }
            }
    }
    Last edited by Awt582; 11-01-2010 at 01:30 AM.

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    i can only seem to replace the last element with the actualy maximum...

    In fact your code doesn't even attempt to do any more than overwrite the last element of the subarray.

    In your blockMax() method you have a variable max that is updated in the loop so that its value is eventually the maximum value within the subarray. However you don't have a variable that keeps track of the index of that maximum value. You need such a variable in order to be able to do the swap: otherwise you won't know which array element to swap with!

    (In actual fact you only need the index of the largest value as you can always figure out the maximum value from its postion within the array.)

    A second observation is the line

    Java Code:
    array[inputEnd] = max;

    This line overwrites the value of array[inputEnd] with max even though max is not the real maximum - it is just the biggest so far. Consider what would happen if the last value of the subarray were also the largest. This line would overwrite this largest value and it would be lost forever.

    This line is part of the swap operation, but it should be done after the for loop has finished.

  3. #3
    Awt582 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Ah thank you so much, I have a very loose understand of using arrays and my book for class is rather lacking in describing things conceptually, but your mention of indexing the array location is exactly what i needed to use just couldn't fathom it in my head.

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 09:49 PM
  2. How do I place these numbers into an array
    By mainy in forum New To Java
    Replies: 2
    Last Post: 07-19-2009, 07:57 PM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 04:50 AM
  4. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 07:30 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •