Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Finding a Smallest Integer

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Finding a Smallest Integer

    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Finding a Smallest Integer

    How would you get the smallest number without the use of a computer?
    Write that answer down into steps.
    Then write code to perform those steps.
    Ask more questions if you get stuck.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Finding a Smallest Integer

    Im getting no where with this. Can anyone start me out with a few lines of code giving me a hint?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Finding a Smallest Integer

    The best hint I have to give is, you don't need code. You need to understand the problem to be solved first. Then you need to come up with a plan to solve the problem. From this plan you can fine tune a series of steps to follow. From there you can write the code. If you get to the code writing part and still do not know what to do, post the steps you plan to take in solving the problem along with your question and someone will help you get back on track again.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Finding a Smallest Integer

    I dont get what you mean... I just want it to make another line under that would say in this case "Smallest Integer " " in LOCATION " " "
    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; October 31st, 2012 at 05:10 PM.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Finding a Smallest Integer

    I dont get what you mean...
    What I mean is, you need to figure out how you will solve the problem. You need to print the "Smallest Integer", right?
    How will you do that?

    Easy. Print out the smallest integer. One step process. But this step can be broken down into more steps. So the steps to printing the smallest integer are given below.


    Well to print the smallest integer you have to know which integer is the smallest.
    You also have to be able to print.

    Now that you know the smallest integer and know how to print, go ahead and print it out.


    But wait. You don't know the smallest integer yet. There are more steps involved in knowing the smallest integer. We will get back to this in a minute.
    Considering how to print, well that is something you know how to do in code with System.out.print so that does not need broken down into more steps.
    Now back to the smallest integer. To get the smallest integer from a group of integers, you have to have a group of integers and some way to iterate through them, and some way to compare to see which is the smallest.


    Take this as a hint for a starting point, and continue to break the steps down to the point you know how to write a line or two of code to accomplish each little task. Once you get through breaking the process down into little steps you can write code for, the results become an outline for the code. Refer to the process you design as you write the code to perform the steps.

Similar Threads

  1. program that determines largest and smallest
    By Reverie in forum Loops & Control Statements
    Replies: 4
    Last Post: February 26th, 2012, 07:41 PM
  2. Finding the range of 2 integer inputs
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2012, 02:54 PM
  3. For each K, output the smallest palindrome larger than K.
    By alekkk in forum Member Introductions
    Replies: 2
    Last Post: July 23rd, 2011, 02:50 PM
  4. Finding the smallest element and shifting....
    By DeadlySwordz in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 02:57 PM
  5. [SOLVED] My smallest and largest integers will not change.
    By toiletsauce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2011, 06:50 PM