(Print distinct numbers) Write a program that reads in 10 numbers and displays the number of distinct numbers and the distinct numbers in their input order and 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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter9: Records (struct)
Section: Chapter Questions
Problem 12SA
icon
Related questions
Question

this is what i have so far

public class Exercise07_05 {
public static void main(String[] args) {
// numbers array will store distinct values, maximum is 10
int[] numbers = new int[10];

// How many distinct numbers are in the array
int numberOfDistinctValues = 0;
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter ten numbers: ");

for (int i = 0; i < numbers.length; i++) {
// Read an input
int value = input.nextInt();

boolean isinthearray = false;
for(int j=0; j < numberofdistinctvalue; j++) {
if (value == number[j]) {
isinthearray = true;
}
}
if(!isinthearray){
number[numberofdistinctcalues] =value;

numberofdistinctvalues++;
}
}
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();
}

**7.5
(Print distinct numbers) Write a program that reads in 10 numbers and displays the
number of distinct numbers and the distinct numbers in their input order and
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.
Sample Run for Exercise07_05.java
Enter input data for the program (Sample data provided below. You may modify it.)
1 2 3 2 1 6 3 4 5 2
Show the Sample Output Using the Preceeding Input
Reset
Execution Result:
JDK8>java Exercise07_05
Enter ten numbers: 1 2 3 21 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are 1 2 3 6 4 5
JDK8>
Transcribed Image Text:**7.5 (Print distinct numbers) Write a program that reads in 10 numbers and displays the number of distinct numbers and the distinct numbers in their input order and 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. Sample Run for Exercise07_05.java Enter input data for the program (Sample data provided below. You may modify it.) 1 2 3 2 1 6 3 4 5 2 Show the Sample Output Using the Preceeding Input Reset Execution Result: JDK8>java Exercise07_05 Enter ten numbers: 1 2 3 21 6 3 4 5 2 The number of distinct numbers is 6 The distinct numbers are 1 2 3 6 4 5 JDK8>
Suggested template
public class Exercise07_05 {
public static void main(String] args) {
// numbers array will store distinct values, maximum is 10
int[] numbers = new int[10];
// How many distinct numbers are in the array
int numberOfDistinctValues = 0;
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter ten numbers: ");
for (int i = 0; i < numbers.length; i++) {
// Read an input
int value = input.nextInt();
Step 1: Check if value is in numbers
Step 2: If value is not in numbers, add it into numbers
Step 3: Increment numberOfDistinctValues
Step 4: Display output
Transcribed Image Text:Suggested template public class Exercise07_05 { public static void main(String] args) { // numbers array will store distinct values, maximum is 10 int[] numbers = new int[10]; // How many distinct numbers are in the array int numberOfDistinctValues = 0; java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) { // Read an input int value = input.nextInt(); Step 1: Check if value is in numbers Step 2: If value is not in numbers, add it into numbers Step 3: Increment numberOfDistinctValues Step 4: Display output
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning