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 4 of 4

Thread: Largest Number Loop Problem

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Largest Number Loop Problem

    The assignment is:
    Write a program that has 100 numbers between 0-100 in an array that tells you the index and value of the largest one.
    This is my code that I am struggling with.
    When I run the code, I receive a long list of sentences that tell me the largest number is (a number) with index1/2/3/etc. And the weird thing is that it writes the sentences as many times as (a number) is. So, if the sentence is "The largest number is 36 with index 1", it will write that sentence 36 times.

    Also, I am trying to find a way to close the loop as soon as it finds a number larger than y but I am not sure how.

    Thank you and I apologize if my method of presenting my problem is incorrect, I am new here and am not 100% sure if this is proper. Please correct me if there is something I'm doing wrong in asking my question.


     public class LargestRandom {
        public static void main(String[] args){
     
            int N = 100;       
            int[] randoms = new int [N];       
     
            for (int i = 1; i<100; i++){
                randoms[i] = (int) (Math.random()*100);
     
     
            for (int y = 99; y<=99 && y>0; y --){    
     
            if  (randoms[i]>=y) 
                System.out.println("The largest number is " + randoms[i] + " with index " + i + ".");
     
     
     
     
     
     
                }  
            }
     
     
        }
     
    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Largest Number Loop Problem

    What you might want to do is create a variable that will hold the largest
    random value, iterate through the loop and fill that value (remember to test
    each time if the new random value is larger than the previous one).

    Once you have that value, you can print it with the index.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (September 6th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Largest Number Loop Problem

    Also, iterate the array correctly to both fill and inspect it. Remember that Java arrays are zero-indexed so that iterating the loop should include the index 0 and should not exceed array.length - 1. The typical for() loop to iterate an array looks like:

    for ( int i = 0 ; i < array.length ; i++ ) {}

    Consider how you'll handle duplicate occurrences of the largest value. Will you report the first index, the last index, or multiple indices that contain the largest value.

    You asked about "closing the loop" and comparing the values to 'y', the loop control variable. A loop can be exited with a break statement. Comparing the values of the array elements to the loop control variable 'y' has no part in this problem.

    I recommend you understand and document what the code is supposed to do before writing any code. In this case, you might have jotted down the steps:

    // create an array to contain 100 integers

    // fill the array with 100 random values between 0 and 100

    // find the largest value in the array

    // report the index (or indices) of the largest value

    Notice I wrote the steps like comments, because I suggest you include those steps in a program skeleton and then fill in the code that does those things after each step. At your stage, a program skeleton is a class with an empty main() method.

  5. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Largest Number Loop Problem

    Thank you for both your responses. I inspected my code carefully after finding some errors and fixed some logical mistakes that did not show up in the error reports.
    Thank you for your in depth responses, it has helped me immensely.
    This is my new code that works.
     public class LargestRandom  {
        public static void main(String[] args)  {
     
     
     
            // create array with 100 integers
     
            int N = 100;       
            int[] randoms = new int [N];                           
            int n; 
            int largestr = 0;        
     
            // fill array with 100 values ranging from 0 to 100
            for (int i = 0; i<randoms.length; i++){  
                randoms[i] = (int) (Math.random()*100);
     
            // find the largest value in the array    
                if (i > 0){
                    n = randoms[i]; 
                    if (n > randoms[i-1]) 
                        largestr = n; 
                }   
            } 
     
            System.out.println("The largest variable is " + largestr);        
            }                                            
        }

Similar Threads

  1. The largest prime factor of the specific number.
    By Freikorps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 30th, 2013, 05:56 AM
  2. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  3. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM
  4. Perfect Number Java Loop Problem - Please Help!?
    By Th3T3chGuy in forum Loops & Control Statements
    Replies: 2
    Last Post: November 13th, 2011, 06:09 PM
  5. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM