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

    Default Finding a Smallest Integer

    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 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?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

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

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

    Default Re: Finding a Smallest Integer

    Alright well my professor may not be the best. But he gave us framework which was this:
    Java 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.

  4. #4
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

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

Similar Threads

  1. Finding the Smallest Element in an Array
    By Cod in forum New To Java
    Replies: 1
    Last Post: 02-20-2011, 02:05 AM
  2. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. Replies: 3
    Last Post: 12-04-2009, 05:21 PM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 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
  •