80.8k views
1 vote
/*

(Print distinct numbers) Write a program that reads in ten numbers and displays
the number of distinct numbers and the distinct numbers separated by exactly one
space (i.e., if a number appears multiple times, it is displayed only once). (Hint:
Read a number and store it to an array if it is new. If the number is already in the
array, ignore it.) After the input, the array contains the distinct numbers.
*/
import java.util.Scanner;
public class Exercise_07_05 {
/** Main Method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] distinctNumbers = new int[10]; // Array of length 10;
int num; // User input
int count = 0; // Number of distinct numbers
// Prompt the user to enter ten numbers
System.out.print("Enter ten numbers: ");
for (int i = 0; i < 10; i++) {
num = input.nextInt();
// Test if num is distinct
if (isDistinct(distinctNumbers, num)) {
distinctNumbers[count] = num;
count++; // Increment count
}
}
// Displays the number of distinct numbers and the // distinct numbers separated by exactly one space
System.out.println("The number of distinct numbers is " + count);
System.out.print("The distinct numbers are");
for (int i = 0; i < distinctNumbers.length; i++) {
if (distinctNumbers[i] > 0)
System.out.print(" " + distinctNumbers[i]);
}
System.out.println();
}
/** Method isDistinct returns true if number is not in array false otherwise */
public static boolean isDistinct(int[] array, int num) {
for (int i = 0; i < array.length; i++) {
if (num == array[i]) return false;
}
return true;
}
}

User Mantrum
by
8.0k points

1 Answer

6 votes

Answer:

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] distinctNumbers = new int[10]; // Array of length 10;

int num; // User input

int count = 0; // Number of distinct numbers

// Prompt the user to enter ten numbers

System.out.print("Enter ten numbers: ");

for (int i = 0; i < 10; i++) {

num = input.nextInt();

// Test if num is distinct

if (isDistinct(distinctNumbers, count, num)) {

distinctNumbers[count++] = num;

}

}

// Displays the number of distinct numbers and the // distinct numbers separated

// by exactly one space

System.out.println("The number of distinct numbers is " + count);

System.out.print("The distinct numbers are");

for (int i = 0; i < count; i++) {

System.out.print(" " + distinctNumbers[i]);

}

System.out.println();

}

/** Method isDistinct returns true if number is not in array false otherwise */

public static boolean isDistinct(int[] array, int count, int num) {

for (int i = 0; i < count; i++) {

if (num == array[i])

return false;

}

return true;

}

Step-by-step explanation:

The original program doesn't work if there are zeros entered. This is because arrays are initialized with zeros. If you take the count along everywhere, you can fix this.

The code will become more readable and more robust if you use a collection like a Set or a List.

User RagingRoosevelt
by
7.3k points

No related questions found