Question

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:

  1. Create an array of 100 integers.
  2. Store 100 random integers (between 1 and 100) in the array.
  3. Print out the elements of the array.
  4. Sort the array in ascending order.
  5. Print out the sorted array.
  6. 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.
  7. Display each number from 1 to 100 and the number of times each is found in the array.
  8. Display the average of the numbers in the array.
  9. Display the highest number in the array.
  10. Display the lowest number in the array.

Other requirements.

  1. Everything should be done using METHODS.
  2. NO NEED FOR A MENU SYSTEM. Just call the methods in the sequence above.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

createArray() function creates a random object and assign 100 random numbers to a new array a and return the array.

displayArray() function traverses the array using for loop and prints every element one by one.

sortArray() function sorts the array using bubble sort method.

searchinArray() function asks the user for a number to be searched, takes the input using Scanner class object and then searches the array for the element and print 'Found' if element is present, otherwise 'Not Found'.

count() function print the count of each element from 1 to 100, how many times each number is in the array.

average() function finds the average of the total numbers in array.

highest() function finds the highest element in the array.

lowest() function finds the lowest value element in the array.

*******************************************************************************
import java.util.Random;
import java.util.Scanner;

public class Main
{
public static int[] createArray()
{
Random r = new Random();
int a[] = new int[100];
  
for(int i=0; i<100; i++)
{
a[i] = r.nextInt(100)+1;
}
  
return a;
}
  
public static void displayArray(int a[])
{
for(int i=0; i<100; i++)
{
System.out.print(a[i]+" ");
}
  
System.out.println();

}
  
public static void sortArray(int a[])
{
for (int i=0; i<99; i++)
{

for (int j=0; j<100-i-1; j++)
{
if (a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
  
public static void searchinArray(int[] a)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be searched: ");
int n = sc.nextInt();
int f=0;
for(int i=0; i<100; i++)
{
if(a[i]==n)
{
System.out.println("Found");
f=1;
break;
}
}
  
if(f==0)
{
System.out.println("Not found");
}

}
  
public static void count(int[] a)
{
for(int i=1; i<=100; i++)
{
System.out.print(i+": ");
int count = 0;
for(int j=0; j<a.length; j++)
{
if(a[j]==i)
count++;
}
  
System.out.print(count);
System.out.println();
  
}
}
  
public static void average(int a[])
{
double avg = 0.0;
for(int i=0; i<100; i++)
{
avg = avg+a[i];
}
  
avg = (float)avg/100;
  
System.out.println("Average: "+avg);
}
  
public static void highest(int a[])
{
int highest = a[0];
for(int i=1; i<100; i++)
{
if(a[i]>highest)
{
highest = a[i];
}
}
  
System.out.println("Highest number: "+highest);
}
  
public static void lowest(int a[])
{
int lowest = a[0];
for(int i=1; i<100; i++)
{
if(a[i]<lowest)
{
lowest = a[i];
}
}
  
System.out.println("Lowest number: "+lowest);
}
  
  
  
   public static void main(String[] args) {
      
       int a[] = createArray();
      
       displayArray(a);
      
       sortArray(a);
      
       displayArray(a);
      
       searchinArray(a);
      
       average(a);

count(a);
      
       highest(a);
      
       lowest(a);
   }
}

Output:

83 71 65 24 31 36 14 37 84 57 20 71 63 72 71 74 7 95 18 32 24 100 95 13 11 72 79 56 48 21 29 76 5 57 17 32 12 67 13 8 56 10 60 17 73 77 43 35 94 35 14 49 57 46 15 15 90 60 97 60 68 65 68 92 13 80 65 36 29 58 67 42 47 40 26 100 80 38 23 24 93 37 15 54 71 82 56 45 38 73 31 77 67 100 13 98 20 78 18 38
5 7 8 10 11 12 13 13 13 13 14 14 15 15 15 17 17 18 18 20 20 21 23 24 24 24 26 29 29 31 31 32 32 35 35 36 36 37 37 38 38 38 40 42 43 45 46 47 48 49 54 56 56 56 57 57 57 58 60 60 60 63 65 65 65 67 67 67 68 68 71 71 71 71 72 72 73 73 74 76 77 77 78 79 80 80 82 83 84 90 92 93 94 95 95 97 98 100 100 100
Enter the number to be searched:
5
Found
Average: 50.279998779296875
1: 0
2: 0
3: 0
4: 0
5: 1
6: 0
7: 1
8: 1
9: 0
10: 1
11: 1
12: 1
13: 4
14: 2
15: 3
16: 0
17: 2
18: 2
19: 0
20: 2
21: 1
22: 0
23: 1
24: 3
25: 0
26: 1
27: 0
28: 0
29: 2
30: 0
31: 2
32: 2
33: 0
34: 0
35: 2
36: 2
37: 2
38: 3
39: 0
40: 1
41: 0
42: 1
43: 1
44: 0
45: 1
46: 1
47: 1
48: 1
49: 1
50: 0
51: 0
52: 0
53: 0
54: 1
55: 0
56: 3
57: 3
58: 1
59: 0
60: 3
61: 0
62: 0
63: 1
64: 0
65: 3
66: 0
67: 3
68: 2
69: 0
70: 0
71: 4
72: 2
73: 2
74: 1
75: 0
76: 1
77: 2
78: 1
79: 1
80: 2
81: 0
82: 1
83: 1
84: 1
85: 0
86: 0
87: 0
88: 0
89: 0
90: 1
91: 0
92: 1
93: 1
94: 1
95: 2
96: 0
97: 1
98: 1
99: 0
100: 3
Highest number: 100
Lowest number: 5

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!


Add a comment
Know the answer?
Add Answer to:
Please write a Java program that does the following: Create an array of 100 integers. Store...
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 complete this code for java Lab Write a program as follows: Create an array with...

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

  • IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and...

    IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and carb intake for a week. Prompt the user for 7 days of minutes walked and carb intake; store in the array.   Write a sort ( your choice which sort ) to report out the sorted minute values -lowest to highest. Write another to report out the sorted carb intake - highest to lowest. These methods MUST be your original code.   Your program should output...

  • Write a program in Java language to prompt the user to enter 3 integers (A, B,...

    Write a program in Java language to prompt the user to enter 3 integers (A, B, and C) then display these numbers from the lowest to the highest (sorted ascendingly) . Note that there are 6 different cases to handle proper order. As an example, a user entered 10 for A, 5 for B and 14 for C: the output of the program should look like this Enter number A? 10 Enter number B? 5 Enter number C? 14 Your...

  • I need help wrting a java program that creates an array with 20 randomly chosen integers....

    I need help wrting a java program that creates an array with 20 randomly chosen integers. The program should prompt the user for an index of the array and display the corresponding element value. If the specified index is out of bounds,display the message "Out of Bounds".

  • Write a program that uses a two-dimensional array to store daily minutes walked and protein intake...

    Write a program that uses a two-dimensional array to store daily minutes walked and protein intake for a week. Prompt the user for 7 days of minutes exercise and protein intake; store in the array. Write a bubble sort to report out the sorted minutes walked values -lowest to highest. Write another to report out the sorted protein intake - highest to lowest. Your program should output all the values stored in the array and then output the 2 sorted...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • In JAVA write a program that contains an array containing 10 integers (1 to 10). user...

    In JAVA write a program that contains an array containing 10 integers (1 to 10). user insert an integer from (1 to 10) , method is library sort or gapped insertion sort that will each sort the array and print it out to the console.

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

  • cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...

    cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented assignment. This assignment is due at the start of class on July 16, Create a program that populates and array with 100 random integers between 0 and 99. Then print out the array. Then prompt the user for a number, and do a sequential search on the unsorted array and return whether or not the number was in the array. Then sort the array...

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