# Smallest Integer Finder

• 10-31-2012, 07:10 AM
antnas
Smallest Integer Finder
This is the code I have so far that I am working on:
Code:

```public class MixerUpper{   public static void main(String[] args){         int[] myArray = new int[5];         printIntArray(myArray);       } public static void printIntArray(int[] myParamVar)   {     int LOCATION = 0;     while(LOCATION<myParamVar.length)     {       System.out.print("In LOCATION " + LOCATION + " ");       System.out.print("Computer has written ");       LOCATION = LOCATION + 1;       System.out.println(( ( int) ( 4.9999 * Math.random() ) ) );           }     return;   } }```
What this code does is print out an array of 5 integers with locations and different numbers in the locations. For example when I ran it this is what is printed out.
> run MixerUpper
In LOCATION 0 Computer has written 1
In LOCATION 1 Computer has written 0
In LOCATION 2 Computer has written 4
In LOCATION 3 Computer has written 7
In LOCATION 4 Computer has written 2
>

Now what I need to do is create code so that after it prints out this information. It prints out the smallest integer in the array by first printing its location and then the actual smallest number in the array. (like "Smallest Integer " " in Location " ")
I don't really know where to start on this one as I am very new to programming. Can anyone help out a little?
• 10-31-2012, 10:59 AM
Tolls
Re: Smallest Integer Finder
Quote:

Originally Posted by antnas
What this code does is print out an array of 5 integers with locations and different numbers in the locations. For example when I ran it this is what is printed out.

No it doesn't.
It prints out 5 random numbers.
It never actually puts them in the array.

As for your problem, you'll need to store the lowest number found so far, and check it on each iteration. You'll also need to store the value of LOCATION when it was found.
• 10-31-2012, 11:44 PM
antnas
Re: Smallest Integer Finder
I want to set up code that would be like System.out.println("Smallest integer " //code here to print out smallest integer(no clue here) "in location " //code to find location of that smallest integer here.

Is this even the right aproach?
• 11-01-2012, 02:31 AM
Junky
Re: Smallest Integer Finder
You have 2 issues. First finding the smallest value which has been explained by Tolls above. Second displaying the result which would need a print statement similar to what you post above. So what is your problem? Post your code, error messages and ask a specific question.
• 11-01-2012, 04:54 AM
DrummondAW
Re: Smallest Integer Finder
Quote:

Originally Posted by antnas
I want to set up code that would be like System.out.println("Smallest integer " //code here to print out smallest integer(no clue here) "in location " //code to find location of that smallest integer here.

Is this even the right aproach?

Step number 1 is getting your code to actually set the value of each element in the array. Once you've done that you could actually keep track of what the lowest integer is while you're building your array, rather than doing another loop through in the end. With a few minor tweaks you should have this thing buttoned up.
• 11-01-2012, 05:42 AM
pbrockway2
Re: Smallest Integer Finder
Since you want to access this array from multiple (static) methods, consider making it a (static) class variable. Then have methods that do specific things with it: fillArray(), printArray(), printMinimum(), etc.