3

I am supposed to create an array of 1000 ints and write a method to find the largest int and then print it. This is what I have so far:

public static int findLargest(int[] numbers){  
    int largest = numbers[0];  
    for(int i = 1; i < numbers.length; i++){  
        if(numbers[i] > largest){  
            largest = numbers[i];  
        }  
    }  
    return largest;
}

First, how do I create an array with 1000 randomly generated ints? I tried int[] array = new (int)(Math.random()); but I don't know how to get it to do 1000 random numbers. Second, how do I print the result? Thanks in advance for any help.

0

7 Answers 7

5

Eyeballing it, your findLargest method looks good -- it is using the correct approach.

To generate the list of 1000 numbers, you need to initialize the array to 1000 elements. Right now you are initializing it to have a random number of elements, which is not what you want. After you initialize the numbers to be an int[] of length 1000, you need to loop over the array putting a random number in. Some something like

int [] numbers = new int[1000]; // generate a new int[]

for (i = 0; i < number.length; i++) {
  numbers[i] = xxxx; // generate a random number
}

You should probably create some sort of init method that creates the original array for you. You would invoke it in main, get the reference to the array, then pass that array to your find method, then print the result.

You are almost there.

Sign up to request clarification or add additional context in comments.

8 Comments

The return statement is fine, it was just badly indented.
@user949300 yeah i was leaving that for the student to figure out.
I swapped (int)(Math.random()*100); for the "XXXX" but it always prints out 99. What am I doing wrong?
note that i dont have an int in front of the i in the for loop
@user1157541: Perhaps 99 is the largest value in your randomly generated array of 1000 numbers from 0 to 99.
|
5
int n[] = {1,5,7,3};
    for(int i=0;i<n.length;i++){
        if(n[i] > n[0]){
        n[0] = n[i];
        }
    }
    System.out.println(n[0]);

Comments

1
int[] randArray = new int[1000];
for(int i = 0; i < 1000; i++){
    randArray[i] = (int)(Math.random()*1000);
}
System.out.println(findLargest(randArray));

1 Comment

you are 10 minutes after the second answer to be posted, and your answer is not adding anything unique. You should delete this.
1

An alternative method of finding the largest int would be to use java.util.Arrays.sort(int[]) and then print the last element of the array.

Comments

0

You could use Java's Random class to generate random integers. Then just fill the array with random integers and call your method, passing the array to it.

// Random number generator
Random r = new Random();

// Create array of 1000 ints
int[] intArr = new int[1000];

// Fill array with random ints
for ( int i = 0; i < intArr.length; i++ ) {
    intArr[i] = r.nextInt();
}

// Print result
System.out.println( findLargest( intArr ) );

1 Comment

you should leave something for the student to actually do.
0

This program: -Uses Arraylist to store the numbers -Uses Collections to sort the numbers -Uses for loop to loop through the Arraylist and fill it with random numbers using Math.Random(). -Prints the largest number in the Arraylist. I hope it answers your question. Please give us a feedback

import java.util.ArrayList;
import java.util.Collections;

public class prac
{
    public static void main(String args[])
    {
        ArrayList<Double> MyArrayList=new ArrayList<Double>();


        for(int i=0;i<100;i++)
        {               
            MyArrayList.add(Math.random());
        }

        Collections.sort(MyArrayList);


        for(int j=0;j<MyArrayList.size();j++)
        {
            System.out.println(MyArrayList.get(j));
        }

        System.out.println("The largest Number is:");
        System.out.println(MyArrayList.get(-1+MyArrayList.size()));

    }

}

1 Comment

There's no need to use ArrayList if you're simply dealing with doubles. You'll get better performance from using primitive double. Don't forget java.util.Arrays.sort()
0

I actually have a pre made class that I setup for finding the largest integer of any set of values. You can put this class into your project and simply use it in any class like so:

     System.out.println(figures.getLargest(8,6,12,9,120));

This would return the value "120" and place it in the output. Here is the methods source code if you are interested in using it:

public class figures {

public static int getLargest(int...f) {
    int[] score = new int[f.length];
    int largest=0;
    for(int x=0;x<f.length;x++) {
        for(int z=0;z<f.length;z++) {
            if(f[x]>=f[z]) {
                score[x]++;
            }else if(f[x]<f[z]) {

            }else {
                continue;
            }
            if(z>=f.length) {
                z=0;
                break;
            }
        }
    }
    for(int fg=0;fg<f.length;fg++) {
        if(score[fg]==f.length) {
            largest = f[fg];
        }
    }
    return largest;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.