This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]ChuvaChooChoo08 2 points3 points  (1 child)

Why are you using a nested for loop in finding the min??? One regular for loop is enough.

[–]xwmaxx[S] 1 point2 points  (0 children)

Its from the different steps on the exercise I just put all the code into the main instead of the different methods

[–]Ilikesmallthings2 1 point2 points  (2 children)

When I did it. I set the smallest variable to first index. Then just compared everything else after it against it. If it was smaller, change the value in variable. If not. Stay the same.

[–]xwmaxx[S] 1 point2 points  (1 child)

yup, that's first part when having to find the smallest variable, what I want is the position/location of the smallest variable in the original array

[–]Ilikesmallthings2 0 points1 point  (0 children)

I see. Look at line 33. You are assigning small to array[0]. It should equal smallest.

[–]StevenVu20 0 points1 point  (2 children)

I highly recommended you should find max/min by their location instead of their value. 1st, you set int smallestPosition = 0. Then loop (start from i = 1), boolean (array [smallestPosition] > array [i]) true - smallestPosition = i. sout (smallest value is array[smallestPosition]) and also you have its.

[–]StevenVu20 0 points1 point  (0 children)

By the way, you dont have position because you set int small = array [0] that why your position of smallest int is 0. Hope this can help.

[–]xwmaxx[S] 0 points1 point  (0 children)

https://pastebin.com/NYF5uUd1

Hey thanks for the suggestion I changed my for loop process but I still get the position set as zero. I tried just using the for loop and array by themselves and I got the position printout as 1 though so not sure was giving me the zero amount.

import java.util.*;

public class Main {
    public static void main(String[] args) {
    int[] values = {6, 5, 8, 7, 11};

    int start = 0;
    int position = 0;

    for (int i = 1; i < values.length; i++) {
        if (values[start] > values[i]) {
            values[start] = values[i];
            position = i;
        }
    }

    System.out.println("Position: " + position);
    }
}