WeekzGod
WeekzGod

Reputation: 333

Find the location of the smallest int in an array using a method using Java

I have written the following method to find the location/index of the smallest number within the array.

private static int indexOfMin( int[] a, int cnt )
    {
        int loc = 0;//the variable that will hold the index position
        int min = a[loc];//the variable that will compare the value of loc against its location
        for (int i = 1; i < a.length; i++)
        {
            if (a[i] < min )//if value is less
            {
               min = a[i];// make value of i equal to min
               loc = i; loc takes on value of the index of the value of min
            }
       }
       return loc ;
    }

Instead of returning the location of the smallest int, it returns the location of the last int. How do I find the location of the smallest int and return it into int loc?

FURTHER EDITS: This is the program in it's entirety. The other two methods I'm still working on. So ignore them.

public static void main (String[] args) throws Exception
    {
        final int CAP = 20; // capacity
        Scanner infile = new Scanner( new File(args[0]) );
        int[] arr = new int[ CAP ];
        int count = 0;

        while ( count < arr.length && infile.hasNextInt() )
        {
            arr[ count ] = infile.nextInt();
            count++;
        }
        infile.close();
        printArray( arr, count );

        // this method is given to you as as. don't modity it
        int minVal = minOf( arr, count );
        System.out.println( "Smallest number is: " + minVal);

        // YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMin METHOD
        int indOfMin = indexOfMin( arr, count );
        System.out.println( "Smallest number is located at index position: " + indOfMin );

        // YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR maxOf METHOD
        int maxVal = maxOf( arr, count );
        System.out.println( "Largest number is: " + maxVal);

        // YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMax METHOD
        int indOfMax = indexOfMax( arr, count );
        System.out.println( "Largest number is located at index position: " + indOfMax );

    } // END main

    // GIVEN AS IS - DO NOT MODIFY
    private static int minOf( int[] a, int cnt )
    {
        int min = a[0];
        for ( int i=0 ; i<cnt ; i++ )
        {
            if (a[i] < min)
                min = a[i];
        }
        return min;
    }

    // YOU WRITE  DEFINTION OF indexOfMin
    // returns the INDEX of the min value NOT the min value itself
    private static int indexOfMin( int[] a, int cnt )
    {
        int loc = 0;
        int min = a[loc];
        for (int i = 1; i < a.length; i++)
        {
            if (a[i] < min )
            {
               min = a[i];
               loc = i;
            }
       }
       return loc ;
    }

The date file contains the following information:

86 95 84 94 32 8 56 51 98 20 90 1 75 6 21

Upvotes: 2

Views: 5150

Answers (6)

user3677311
user3677311

Reputation: 1

Why don't you use Arrays.sort() and return the first element of the array?

//Snippet
void minInArray(){
    int[] arr = new int[]{55,42,11,20,584,63,21,27,84,96,32,30};
    int[] cArr = Arrays.copyOf(arr, arr.length);

    Arrays.sort(cArr);
    int idx = Arrays.asList(arr).indexOf(cArr[0]);
    String s = new StringBuilder("The min value is: ")
               .append(cArr[0])
               .append(" located at index: ")
               .append(idx)
               .toString();
    System.out.println(s);
}

Upvotes: -1

Devi Das
Devi Das

Reputation: 1

the first code is correct.you can still remove cnt which was never used.2nd one is absolutely wrong.

import java.util.Scanner;

public class small {

private static int indexOfMin( int[] a ,int cnt)

{
   int loc=0;
    int min = a[0];
    for (int i = 1; i <cnt; i++)
    {
        if (a[i] < min )
        {
           min = a[i];
           loc = i;
        }
   }
   return loc ;
}
public static void main(String args[]){
    Scanner s= new Scanner(System.in);
    System.out.println("how many numbers do you want to enter:");
    int n=s.nextInt();
    int a[]=new int[n];
    System.out.println("enter the numbers:");
    for(int i=0;i<n;i++){
      a[i]=s.nextInt();
    }
    int p=indexOfMin(a,n);
    System.out.println((p+1)); //p+1 as we have taken location same as i which starts with 0
 }
}

how many numbers do you want to enter:7

enter the numbers: 34 45 33 234 123 116 555

the location of the smallest no is:3

I hope u find it helpful.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

Based on your comments and edit, I think you wanted

private static int indexOfMin(int[] a, int cnt) {
  int loc = 0;
  int min = a[loc];
  for (int i = 1; i < cnt; i++) {
    if (a[i] < min) {
      min = a[i];
      loc = i;
    }
  }
  return loc;
}

Then to verify,

// this method is given to you as as. don't modity it
int minVal = minOf( arr, count );
System.out.println( "Smallest number is: " + minVal);

// YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMin METHOD
int indOfMin = indexOfMin( arr, count );
System.out.println( "Smallest number is located at index position: " + indOfMin);
if (arr[indOfMin] == minVal) {
  System.out.println("Min value passed");
} else {
  System.out.println("Min value failed");
}

Upvotes: 2

Jack C
Jack C

Reputation: 1084

As stated above, your first segment of code is working fine.

Arrays in java start at index = 0.

By the looks of it, cnt is supposed to be the length of the array of data you are passing, and it can be used as a substitute for a.length (although passing the extra data is very unnecessary).

In the second segment of code, you return an index before completing the whole loop (potentially). This will stop once you get to the first item that is smaller than a[0] and return that location (which probably occurs at index 3 in your case).

For future reference, it will be helpful for us to know the data that you are testing with.

Upvotes: 0

Fulligan
Fulligan

Reputation: 75

int[] arr = {1, 5, 3, 4, 0, 9};
System.out.println(indexOfMin(arr));

Prints:

4

This is absolutely correct, because the index of an array starts with 0

Upvotes: 1

Xabster
Xabster

Reputation: 3720

The first method posted works.

You do not have a problem.

You might have forgotten that indexes start at 0 and thus interpreted the results as being wrong?

Upvotes: 0

Related Questions