Question

In Java2. Array Exercise ( 10 points) Write a Java program that will prompt the user to input a size of an array. Create an array of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1) The java Code for the above question is below:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Scanner sc =new Scanner(System.in);
       System.out.println("Enter the Size of array");
       int size = sc.nextInt();
      
       int [] arr = new int[size];
      
       System.out.println("Enter the Array elements:");
       for(int i=0; i<size; i++){
       arr[i]= sc.nextInt();
       }
      
       sortArray(arr);
       System.out.print("Sorted Array: ");
           for(int i=0; i<size; i++){
       System.out.print(arr[i]+" ");
       }
      
       int frequentElement = mostFrequent(arr);
       System.out.println("\nMost Frequent Element is: "+ frequentElement);
   }
  
   // sortArray() function which uses Bubble sort Algorithm to sort the elements.
   public static void sortArray(int arr[])
{
   for (int i = 0; i < arr.length; i++)
{
   for (int j = 0; j < (arr.length - 1 - i); j++)
{
   //if left value is great than right value
   if (arr[j] > arr[j + 1])
{
   //swap values
    int temp = arr[j];
   arr[j] = arr[j + 1];
   arr[j + 1] = temp;
}
}
}
}
// mostFrequent() method which returns most frequency element in array.
public static int mostFrequent(int arr[])
{
// find the max frequency using linear traversal
int max_count = 1, res = arr[0], curr_count = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1])
curr_count++;
else {
if (curr_count > max_count) {
max_count = curr_count;
res = arr[i - 1];
}
curr_count = 1; }
}
// If last element is most frequent
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[arr.length - 1];
}
return res;
}
}

CODE SCREENSHOT

1 6 9 } Main.java import java.util.Scanner; 2 public class Main 3- { 4 public static void main(String[] args) { 5 Scanner scarr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // most Frequent() method which returns most frequency element in array. publ

OUTPUT SCREENSHOT

Enter the size of array 5 Enter the Array elements: 8 2 1 3 2 Sorted Array: 1 2 2 3 8 Most Frequent Element is: 2 ...ProgramEnter the Size of array 4 Enter the Array elements: 10 90 40 10 Sorted Array: 10 10 40 90 Most Frequent Element is: 10 - ..Pr

Add a comment
Know the answer?
Add Answer to:
In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the...
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
  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

  • C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that...

    C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that creates an array of 10 integers. The program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array displayLargest() Used to find and display the largest number in the array (prior to sort). displaySmallest() Used to find and display the smallest number in the...

  • For Java - Write a program that creates an array of 10 integers. Ask the user...

    For Java - Write a program that creates an array of 10 integers. Ask the user to enter the subscript (index) of an element, then displays the element located at that subscript. If the subscript entered is out of bounds (less than 0 or greater than length - 1), display the message "Out of Bounds".

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

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

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