# Finding a Smallest Integer

• 10-25-2012, 11:30 PM
antnas
Finding a Smallest Integer
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 4
In LOCATION 1 Computer has written 0
In LOCATION 2 Computer has written 4
In LOCATION 3 Computer has written 0
In LOCATION 4 Computer has written 0
>

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.
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-26-2012, 12:45 AM
Re: Finding a Smallest Integer
So, first off, use counting loops (for loop) for lists of a known size - quite a bit easier to read and the syntax is more concise.

Second, you aren't actually storing the random numbers you're generating anywhere. You're blindly looping through an array, and then just making up numbers and printing them. I would think that a method called 'printIntArray()' would print numbers, not generate them.
• 10-26-2012, 02:08 AM
antnas
Re: Finding a Smallest Integer
Alright well my professor may not be the best. But he gave us framework which was this:
Code:

```public*static*void*printIntArray(int[]*myParamVar) { ***int*LOCATION*=*0; ***while(*LOCATION*<*myParamVar.length) ***{ ******System.out.print("In*LOCATION*"*+*LOCATION*+*"**"); ******System.out.print("Rosie*has*written*"); ******System.out.println(*myParamVar[LOCATION]); ***}//Whoops..I*really*forgot*something,*and*was*too*lazy*to test.*You*fix*my*infinite*loop.*You*got*what*you*paid*for. ***return;** ***//Make*the**method*return*AFTER*printing*the*entire*array. }```
He then tells us to stop the infinite loop ( which I did ) then he gave us that equation to use to generate the numbers. This is actually what im supposed to do. Print random numbers out in 5 different locations.
It may sound stupid but this is the assignment, haha.
• 10-26-2012, 04:20 AM
sibernewf
Re: Finding a Smallest Integer
Ok from what I can see, it looks like there should be more code.
The printIntArray looks like it already assumes that each element in the array has data in it.
Somewhere in the assignment should be some code that populates each element of the array.

you would have probaby covered methods and return types, so I wont go into that, but leave that exercise for you to go over your notes and look into returning values from methods.

So it looks like you are on the right track (If the myParamVar already has data in it.)