----A mode in an array numArray of numbers is the number that appears most often
ID: 3654253 • Letter: #
Question
----A mode in an array numArray of numbers is the number that appears most often
? You task is to first ask the user to input a number N, which indicates the size of numArray
? Next you need to generate N random integers in range 1 to 100, store them in numArray, and print their values on the screen.
? Then you pass numArray into the following method which finds the mode of an array and print the result in the main method: public static int findMode(int[] array)
Hint:
? An array could have more than 1 modes, butin this exercise, you can find any of those
? You can sort the array first and then gothrough the array just once to find the mode
? Use java.util.Arrays.sort(numArray) to sort
Sample running result:
Please input the size of the array:20
The array is:
64 38 69 100 38 81 45 46 58 14 46 66 46 54 84
72 52 46 13 70
The sorted array is:
13 14 38 38 45 46 46 46 46 52 54 58 64 66 69 70
72 81 84 100
The mode is:46
Explanation / Answer
class sorting
{
public static void main(String[] input)
{
int k=input.length;
String temp=new String();
String names[]=new String[k+1];
for(int i=0;i<k;i++)
{
names[i]=input[i];
}
for(int i=0;i<k;i++)
for(int j=i+1;j<k;j++)
{
if(names[i].compareTo(names[j])<0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
System.out.println(“Sorted order is”);
for(int i=0;i<k;i++)
{
System.out.println(names[i]);
}
}
please rate
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.