Question
Please complete this code for java
Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a match found message. Also print at which index the match was found, else print a match not found message to the console Finally, print the array to the console also
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
import java.util.Scanner;

public class SearchInRandomArray {

    public static void main(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for(int i = 0; i < arr.length; ++i) {
            arr[i] = 1 + random.nextInt(100);
        }
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = in.nextInt();
        boolean found = false;
        for(int i = 0; i < arr.length; ++i) {
            if(arr[i]==num) {
                found = true;
                break;
            }
        }
        if(found) {
            System.out.println("match found");
        } else {
            System.out.println("match not found");
        }
        System.out.print("Array is: ");
        for(int i = 0; i < arr.length; ++i) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}

Enter a number: match found Array is: 14 87 10 73 5 96 53 75 92 54 Process finished with e xit code 0

Please\;let\;me\;know\;if\;you\;need\;me\;to\;change\;anything\\ Please\;upvote\;this\;answer!

Add a comment
Know the answer?
Add Answer to:
Please complete this code for java Lab Write a program as follows: Create an array with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Write a JavaScript program that will do the following: Create an array of size 50. Store randoml...

    Write a JavaScript program that will do the following: Create an array of size 50. Store randomly generated number (between 0 and 10) into this array. Ask users what number they want to search in the array. Make sure user given number is between 0 and 10. Search the user given number in the array: If the program found the number in the array then display all the indexes where the number was found. If the program does not find...

  • Create a java program that with an integer array that has ten numbers in it. Ask...

    Create a java program that with an integer array that has ten numbers in it. Ask the user for an integer. Search your array and if found it tell them at what position it was found. If not found tell them the number was not found.

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • Write a JAVA program that declares and initializes an array with the following numbers: 10, 89,...

    Write a JAVA program that declares and initializes an array with the following numbers: 10, 89, 50, 24, 60, 1, 15. Then ask the user for a number and check if the array contains that number. If it does, return a message to the user (print to screen) that says

  • Create a program that uses a Jagged Array. The program will create an array with 50...

    Create a program that uses a Jagged Array. The program will create an array with 50 rows. Each of the values for each element of the array will be randomly generated. The random values will be between 1 and 20. Depending on the value generated for each row, you will create an array with that number of columns for that row. Each column created will contain the value to the left plus 1. After you create and populate the entire...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT