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 4 of 4

Thread: Need help modifying a method

  1. #1
    Junior Member
    Join Date
    Nov 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help modifying a method

    class Main {
        public static void main (String[] args)
        {
            int[] arr = {16, 5, 5, 4, 13, 2, 0};
            System.out.println(findMin(arr, 3, 6));
        }
        public static int findMin (int[] arr, int minIndex, int maxIndex)
        {
            int min = arr[minIndex]; // set the min value at arr[3]
            for (int i = minIndex; i < maxIndex; i++) // start at arr[3]
            { // and go to arr[5]
                if (arr[i] < min) // if arr[3] < arr[3] then min = arr[3]
                {
                    min = arr[i];
                } // if arr[4] < arr[3] then min = arr[4]
            }
            return min; // return arr[4]
        }
    }

    I created a method that accepts an integer array as a parameter and returns the element with the smallest value as the return value; in essence, all that was required was a loop that iterated until the view became empty. Then, I changed my method to include two additional parameters: the method is supposed to find the minimum value in the array within the given index range instead of the entire array since int startIndex and endIndex are supposed to limit the view on the array. The function findMin(arr, 2, 5) checks indices 2, 3, 4, and 5 and returns 2 as the smallest number in the array's range. I also found a method to swap two array indices, such as swap(array, 3, 7) to replace index 3 with index 7.

    public static void swap (int[] arr, int index1, int index2)
        {
            int temp = arr[index1];
            arr[index1] = arr[index2];
            arr[index2] = temp;
        }

    Right now, I'm stuck on*modifying*the findMin method so that it returns the index of the min element rather than the actual min element. And maybe rename it to findMinIndex.

    Can someone help me?

  2. #2
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Need help modifying a method

    rename the variable i to index.

  3. #3
    Junior Member
    Join Date
    Nov 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help modifying a method

    class Main {
        public static void main (String[] args)
        {
            int[] arr = {16, 5, 5, 4, 13, 2, 0};
            System.out.println(findMin(arr, 3, 6));
        }
        public static int findMin (int[] arr, int minIndex, int maxIndex)
        {
            int min = arr[minIndex]; // set the min value at arr[3]
            int indexOfMinimum = minIndex; // set the index of the minimum value to minIndex
            for (int i = minIndex; i < maxIndex; i++) // start at arr[3]
            { // and go to arr[5]
                if (arr[i] < min) // if arr[3] < arr[3] then min = arr[3]
                {
                    min = arr[i]; // min = arr[4]
                    indexOfMinimum = i; // indexOfMinimum = 4
                } // if arr[4] < arr[3] then min = arr[4]
            }
            return indexOfMinimum; // return arr[4]
        }
    }

    With this code I received 5, we now have the index for arr[4].

    Do you by any chance know how to write the code for the selection sort algorithm?

    This is how it was explained to me:

    If we look at the selection sort algorithm, we start with a full array, search the minimum, swap it to the front, then reduce the view on the array by one. There are two variables needed—one for the start index and the other for the end index—and a loop that iterates over the array's elements repeatedly until the view is empty (start == end index). During the loop, we find the minimum, swap it to the front, and then decrease the view by one (startIndex++).

  4. #4
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Need help modifying a method

    You haven't taken my advice, so why should I expect even more such advice would be useful?

    I do have an opinion: The habitual and pedantic use of single-letter variables is a hold-over from ancient mathematics, and requires using an additional mapping separate from the formula. For example,
    • Let d be the distance covered, t be the time elapsed, and v be the velocity
    • v = d/t
    With the invention of computers and programming came the development of named variables, so that such information is integrated into the expression. It no longer requires a look-up table, thus reducing comprehension workload and freeing up developer problem-solving resources.

    velocity = distance / time
    Last edited by AngleWyrm; November 27th, 2022 at 09:02 PM.

Similar Threads

  1. Returning wrong value without modifying!!
    By spidey in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 2nd, 2018, 06:51 AM
  2. [SOLVED] Modifying the method to clear collection HELPPP!
    By miss-naina in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 11th, 2014, 10:03 AM
  3. Help with modifying a program for evaluating arithmetic expressions
    By rockout341 in forum Object Oriented Programming
    Replies: 5
    Last Post: October 13th, 2011, 11:01 AM
  4. values of variables changing despite not modifying them
    By AndyKow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:39 PM
  5. Interating through an ArrayOrderedList and modifying elements within
    By xecure in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 26th, 2010, 11:15 PM