import java.util.Scanner; public class numArray{ public static void main(String[
ID: 3659180 • Letter: I
Question
import java.util.Scanner;
public class numArray{
public static void main(String[] args){
//Scanner
Scanner input = new Scanner(System.in);
System.out.print("Pleae input the size of the array: ");
int userIn = input.nextInt();
//create array
int[] numArray = new int[userIn];
//output
System.out.println("The array is: ");
//generate random num
for(int i = 0; i < userIn; i++){
numArray[i] = (int)(1 + Math.random() * 100);
System.out.print(numArray[i] + " ");
}
System.out.println();
//sort array
java.util.Arrays.sort(numArray);
//output
System.out.println("The sorted array is: ");
for(int i = 0; i < numArray.length; i++){
System.out.print(numArray[i] + " ");
}
System.out.println();
if(findMode(numArray) == 0)
System.out.println("There is no mode");
else
System.out.println("The mode is: " + findMode(numArray));
}
//Method
public static int findMode(int[]array){
int counter = 0;
int max = 0;
int temp = 0;
for(int i = 1; i < array.length; i++){
if(array[i - 1] == array[i]){
counter++;
if(counter > max){
max = counter;
temp = array[i];
}
}
else
counter = 0;
}
return temp;
}
}
Explanation / Answer
I found a small mistake in ur finding mode function..... please try the code as below:: public static int findMode(int[]array) { int max = 0; int count = 0; int sourceIndex = 0; int maxIndex = 0; int currentIndex ; Type maxElement = null; for( currentIndex =0 ; currentIndexRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.