The method I am trying to write is by passing two arrays, one an array of integers and boolean array, then find the smallest, then next smallest, then next next smallest, etc values in the array "a". Outside of this method, the boolean at the corresponding index to smallest value will be changed to true if it was previously found to be smallest value in the array. All numbers get changed outside of this method except for the numbers set to true. Then method is repeated until every number has been selected.

The object smallest is only a means of returning the smallest value and index at the same time. Thoes are the only two instance variables and are only used in this method.

public Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        x.setSmallest(a[1]);            // a[0] will always be 0
        x.setIndex(1);


        for(int i = 2; i<a.length;i++){
            if(b[i] != true){
              if(a[i] > 0 && a[i] < x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);
                    x.setSmallest(a[i]);
                    System.out.println(x.getIndex() + " - " + x.getSmallest());
              }
            }
        }
        return x;
    }

Sorry, yes I am having a hard time trying to figure out why my code is not working. Say the arrays passed are:

a = {0, 10, 20, 16, 10, 22}
b = {false,true,false,false,false,false}

meaning the second iteration, first iteration found 10 at index 1 as the smallest.

Second pass should find 10 at index 4 to be the smallest, but it returns index 2 again. I thought this was taken care of by using the boolean array to disqualify previous found indexes.

Do you have code for testing the method you posted? A small, selfcontained program with all the data etc needed.

please post your driver program as well if you can. from a quick glance it doesn't seem to be looking at the second 10 because you are comparing with greater than or less than and should be using <= or >= so it can detect the second 10, but you may need to check for duplicates

Edited 4 Years Ago by corby

public class Smallest {
    private int index;
    private int smallest;

    public Smallest(){
        index = 0;
        smallest = 0;
    }

    public Smallest(int i, int s){
        index = i;
        smallest = s;
    }

    public int getIndex(){
        return index;
    }

    public int getSmallest(){
        return smallest;
    }

    public void setIndex(int i){
        index = i;
    }

    public void setSmallest(int s){
        smallest = s;
    }
}

public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        x.setSmallest(a[1]);            // a[0] will always be 0
        x.setIndex(1);


        for(int i = 2; i<a.length;i++){
            if(b[i] == true){i++;}
            else if(a[i] > 0 && a[i] < x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);
                    x.setSmallest(a[i]);
                }
            }

        System.out.println(x.getIndex() + " - " + x.getSmallest());

        return x;
    }


    outputs:

    1-10


public static void main(String Args[]){
        //finalProject one = new finalProject("C:/Users/Joe/Documents/CSCI-260/Classwork/CSCI260/src/finalProject/distanceAndTime.dat");
        //finalProject one = new finalProject("C:/Users/Joe/Documents/CSCI-260/Classwork/CSCI260/src/finalProject/test.dat");
        //one.shortestPath();

        int[] a = {0, 10, 20, 16, 10, 22};
        boolean[] b = {false,true,false,false,false,false};

        indexOfSmallest(a,b);


    } 

Try debugging the code by adding println statements to show the values of variables as they are changed and used. The print out will show you what the computer sees and help you understand where the problem is.

//i made a small change in your indexOfSmallest function

public class Smallest {
    private int index;
    private int smallest;
    public Smallest(){
        index = 0;
        smallest = 0;
    }
    public Smallest(int i, int s){
        index = i;
        smallest = s;
    }
    public int getIndex(){
        return index;
    }
    public int getSmallest(){
        return smallest;
    }
    public void setIndex(int i){
        index = i;
    }
    public void setSmallest(int s){
        smallest = s;
    }

    public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        x.setSmallest(a[1]);            // a[0] will always be 0
        x.setIndex(1);
        for(int i = 2; i<a.length;i++){
            if(b[i] == true){i++;}
            else if(a[i] > 0 && a[i] <= x.getSmallest()){
                x.setSmallest(a[i]);
                x.setIndex(i);
                x.setSmallest(a[i]);
            }
        }
        System.out.println(x.getIndex() + " - " + x.getSmallest());
        return x;
    }

    public static void main(String Args[]){
        int[] a = {0, 10, 20, 16, 10, 22};
        boolean[] b = {false,true,false,false,false,false};
        indexOfSmallest(a,b);
    } 
}

Edited 4 Years Ago by corby

@corby Are you here to help the OP learn how to debug the code
or are you doing his job for him? What will the OP have learned by copying your code?

@NormR1 my apologies but it was just one very small thing I changed. It's not like I rewrote the entire program for him/her or anything like that

Thanks corby, I think I did try making that change prior to posting on daniweb but it messed up my first iteration (when entire b is false)

On the first iteration I would need it to pick out the first appearance of the repeated smallest value and not the second, which this change would produce.

Edited 4 Years Ago by crazymidget01

Did you try adding printlns to see what the values of the variables are as the code executes?

Yes I did. I suppose I have to use a temp variable to check for duplicates to make it select the first occurance. If there is a better/easier way to achieve this, I am all ears....or eyes. ha

Edited 4 Years Ago by crazymidget01

What was printed out? Did the loop work as you expected? Was the if test ever true?

When I now execute the code I get:

4 - 10, b=[false, true, false, false, true, false]
3 - 16, b=[false, true, false, true, true, false]
2 - 20, b=[false, true, true, true, true, false]

I found a problem by looking at the println output and made a small change to get the above. I also set b[] true

Edited 4 Years Ago by NormR1

public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        x.setSmallest(a[1]);            // a[0] will always be 0
        x.setIndex(1);
        int tempi =0, temps=0;

        for(int i = 2; i<a.length;i++){
            if(b[i] == true){i++;}
            else if(a[i] > 0 && a[i] <= x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);

                }
            System.out.println(x.getIndex() + " - " + x.getSmallest());
            }

        return x;
    }

b is all false for this try

Output:
1 - 10
1 - 10
4 -10
4 - 10

So I believe the code works. The problem is there are duplicates and it finds the last value as the smallest. I need it to find the first occurance of the smallest value. Going to try to reverse the for loop so it starts from end of the array first?

Edited 4 Years Ago by crazymidget01

OK Still not working...printed out all values and for loop variable

public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        x.setSmallest(a[1]);            // a[0] will always be 0
        x.setIndex(1);

        for(int i = a.length-1; i > 0;i--){
            if(b[i] == true){
                System.out.println("If b[i] == true");
                i--;}
            else if(a[i] > 0 && a[i] <= x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);

                }
            System.out.println("Index " + x.getIndex() + " Value:" + x.getSmallest() + " i=" + i);
            }

        System.out.println("Final: " + x.getIndex() + " " + x.getSmallest());
        return x;
    }

Input 1:

int[] a = {0, 10, 20, 16, 10, 22};
boolean[] b = {false,true,false,false,false,false};
indexOfSmallest(a,b);

Output 1:

Index 1 Value:10 i=5
Index 4 Value:10 i=4
Index 4 Value:10 i=3
Index 4 Value:10 i=2
If b[i] == true
Index 4 Value:10 i=0
Final: 4 10

Input 2:

int[] a = {0, 10, 20, 16, 10, 22};
boolean[] b = {false,true,false,false,true,false};
indexOfSmallest(a,b);

Output 2:

Index 1 Value:10 i=5
If b[i] == true
Index 1 Value:10 i=3
Index 1 Value:10 i=2
If b[i] == true
Index 1 Value:10 i=0
Final: 1 10

Edited 4 Years Ago by crazymidget01

Where do you print out the value of a[i] so you can see what the if condition is doing?

If b[i] == true

What is the value of i when this is printed?

Edited 4 Years Ago by NormR1

Output for last case above:

Index 1 Value:10 a[5] = 22
If b[i] == true  i=:4
Index 1 Value:10 a[3] = 16
Index 1 Value:10 a[2] = 20
If b[i] == true  i=:1
Index 1 Value:10 a[0] = 0
Final: 1 10

exactly what is expected: b[4] and b[1] are true and are skipped over.
Corresponding numbers in integer array are correct

16 should be selected because it doesn't have a true at corresponding index and is the next smallest value

Ok i see. So I can not set it to check against first element again after first iteration. So how do i determing what element to check against?

Tried setting first value that doesn't have a corresponding true value as the smallest. Still no luck.

 public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        for(int j = 1; j<b.length;j++){
            if(b[j] != true)//Check for first value w/out true
            {
                x.setSmallest(a[j]);
                x.setIndex(j);
                System.out.println("j: " + j + "  b[j] is false.  Smallest: " + x.getSmallest());
                break;
            }
        }

        for(int i = a.length-1; i > 0;i--){
            if(b[i] == true){
                System.out.println("If b[i] == true  i=:" + i);
                i--;}
            else if(a[i] > 0 && a[i] <= x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);

                }
            System.out.println("Index " + x.getIndex() + " Value:" + x.getSmallest() + " " + "a["+i+"] = " + a[i]);
            }

        System.out.println("Final: " + x.getIndex() + " " + x.getSmallest());
        return x;
    }

Input:

        int[] a = {0, 10, 20, 16, 10, 22};
        boolean[] b = {false,true,false,false,true,false};
        indexOfSmallest(a,b);

Output:

    j: 2.  b[j] is false.  Smallest: 20
    Index 2 Value:20 a[5] = 22
    If b[i] == true  i=:4
    Index 2 Value:20 a[3] = 16
    Index 2 Value:20 a[2] = 20
    If b[i] == true  i=:1
    Index 2 Value:20 a[0] = 0
    Final: 2 20

a[i] <= x.getSmallest()
16 is most definitely smaller than 20.

Edited 4 Years Ago by crazymidget01

Move the println to the first place in the for loop so it prints out the values before they are compared.
The code changes the value of i so the print out after that change does not show what was tested.

Edited 4 Years Ago by NormR1

In that case...when b(4) is true, it decrements, then decrements again at the start of next loop skipping 3.

j: 2  b[j] is false.  Smallest: 20
Index 2 Value:20 a[5] = 22
Index 2 Value:20 a[4] = 10
If b[i] == true  i=:4
Index 2 Value:20 a[2] = 20
Index 2 Value:20 a[1] = 10
If b[i] == true  i=:1
Final: 2 20




public static Smallest indexOfSmallest(int[] a, boolean[] b){
        Smallest x = new Smallest();
        for(int j = 1; j<b.length;j++){
            if(b[j] != true){   //Check for first value w/out true
                x.setSmallest(a[j]);
                x.setIndex(j);
                System.out.println("j: " + j + "  b[j] is false.  Smallest: " + x.getSmallest());
                break;
            }
        }

        for(int i = a.length-1; i > 0; i--){
            System.out.println("Index " + x.getIndex() + " Value:" + x.getSmallest() + " " + "a["+i+"] = " + a[i]);
            if(b[i] == true){
                System.out.println("If b[i] == true  i=:" + i);
                }
            else if(a[i] > 0 && a[i] <= x.getSmallest()){
                    x.setSmallest(a[i]);
                    x.setIndex(i);

                }

            }

        System.out.println("Final: " + x.getIndex() + " " + x.getSmallest());
        return x;
    }

This should work then.

What happens when all the numbers have been found and there are no more to return?

Another technique when searching a list for smallest is to set the comparing value to the largest possible number so that any value will be smaller. That assumes that the largest possible number won't be in the list.

Edited 4 Years Ago by NormR1

This question has already been answered. Start a new discussion instead.