Where is the mistake in this code import java.util.Scanner; public class Exercis
ID: 3884967 • Letter: W
Question
Where is the mistake in this code
import java.util.Scanner;
public class Exercise {
/** Main method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Create a Scanner
int[] numbers = new int[10];
// Create an array of length 10
// Prompt the user to enter ten numbers
System.out.print("Enter ten number: ");
for (int i = 0; i < numbers.length; i++)
numbers[i] = input.nextInt();
// Get distinct numbers
int[] distinctNumbers = eliminateDuplicates(numbers);
// Display the result
System.out.print("The distinct numbers are:");
for (int e: distinctNumbers)
{
if (e > 0)
System.out.print(" " + e);
}
System.out.println();
}
/** eleminateDuplicates returns a new array with duplicate values eliminated
*/
public static int[] eliminateDuplicates(int[] list) {
int[] distinctList = new int[list.length];
int i = 0;
// index distinctList
for (int e: list)
{
if (linearSearch(distinctList, e) == -1)
{
distinctList[i] = e; i++;
}
}
return distinctList;
}
/** linearSearch */
public static int linearSearch(int[] array, int key) {
for (int i = 0; i < array.length; i++)
{
if (key == array[i])
return i;
}
return -1;
}
}
Please run the code before you answer.
Explanation / Answer
import java.util.Scanner;
public class Exercise {
/** Main method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Create a Scanner
int[] numbers = new int[10];
// Create an array of length 10
// Prompt the user to enter ten numbers
System.out.print("Enter ten number: ");
for (int i = 0; i < numbers.length; i++)
numbers[i] = input.nextInt();
// Get distinct numbers
int[] distinctNumbers = eliminateDuplicates(numbers);
// Display the result
System.out.print("The distinct numbers are:");
for (int e: distinctNumbers)
{
if (e > 0)
System.out.print(" " + e);
}
System.out.println();
}
/** eleminateDuplicates returns a new array with duplicate values eliminated
*/
public static int[] eliminateDuplicates(int[] list) {
int[] distinctList = new int[list.length];
int i = 0;
// index distinctList
for (int e: list)
{
if (linearSearch(distinctList, e) == -1)
{
distinctList[i] = e; i++;
}
}
return distinctList;
}
/** linearSearch */
public static int linearSearch(int[] array, int key) {
for (int i = 0; i < array.length; i++)
{
if (key == array[i])
return i;
}
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.