Results 1 to 6 of 6
  1. #1
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Smallest Integer Finder

    This is the code I have so far that I am working on:
    Java 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: Smallest Integer Finder

    Quote Originally Posted by antnas View Post
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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?
    Last edited by antnas; 10-31-2012 at 11:09 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  5. #5
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Smallest Integer Finder

    Quote Originally Posted by antnas View Post
    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

Similar Threads

  1. Finding a Smallest Integer
    By antnas in forum New To Java
    Replies: 3
    Last Post: 10-26-2012, 04:20 AM
  2. Path Finder
    By OmerHalit in forum AWT / Swing
    Replies: 2
    Last Post: 04-05-2010, 08:09 PM
  3. sign finder
    By iPetey in forum New To Java
    Replies: 6
    Last Post: 05-01-2009, 05:24 AM
  4. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  5. Library Finder 1.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-15-2007, 04:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •