(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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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`

Buttons:
- **Show the Sample Output Using the Preceding Input**
- **Reset**

**Execution Result:**

```
JDK8>java Exercise07_05
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are 1 2 3 6 4 5

JDK8>
```

Explanation: The program receives a series of ten numbers and processes them to determine and display the distinct numbers and the count of these distinct numbers. In the sample execution, the input numbers result in six distinct numbers being printed.
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` Buttons: - **Show the Sample Output Using the Preceding Input** - **Reset** **Execution Result:** ``` JDK8>java Exercise07_05 Enter ten numbers: 1 2 3 2 1 6 3 4 5 2 The number of distinct numbers is 6 The distinct numbers are 1 2 3 6 4 5 JDK8> ``` Explanation: The program receives a series of ten numbers and processes them to determine and display the distinct numbers and the count of these distinct numbers. In the sample execution, the input numbers result in six distinct numbers being printed.
### Suggested Template

```java
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
    }
}
```

### Explanation

This Java program template is designed to prompt the user to enter ten numbers and then determine how many distinct numbers were entered. 

1. **Array Declaration**: An integer array `numbers` is created to store up to 10 distinct values.

2. **Initialization**: The variable `numberOfDistinctValues` is used to track the count of distinct numbers entered by the user.

3. **User Input**: A `Scanner` object is used to read user input from the console.

4. **Logic**:
   - The program prompts the user to input ten numbers.
   - For each input, the code checks if the number is already in the `numbers` array.
   - If it is not present, the number is added to the array.
   - The counter `numberOfDistinctValues` is incremented each time a new number is added.

5. **Output**: The distinct values and their count are displayed (implementation of Step 4 is required).

Note: The sections marked as "Step" require complete implementation logic to function as expected.
Transcribed Image Text:### Suggested Template ```java 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 } } ``` ### Explanation This Java program template is designed to prompt the user to enter ten numbers and then determine how many distinct numbers were entered. 1. **Array Declaration**: An integer array `numbers` is created to store up to 10 distinct values. 2. **Initialization**: The variable `numberOfDistinctValues` is used to track the count of distinct numbers entered by the user. 3. **User Input**: A `Scanner` object is used to read user input from the console. 4. **Logic**: - The program prompts the user to input ten numbers. - For each input, the code checks if the number is already in the `numbers` array. - If it is not present, the number is added to the array. - The counter `numberOfDistinctValues` is incremented each time a new number is added. 5. **Output**: The distinct values and their count are displayed (implementation of Step 4 is required). Note: The sections marked as "Step" require complete implementation logic to function as expected.
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education