(Duplicate Elimination) Use a one-dimensional array to solve the following problem: Write an application that inputs five numbers, each between 10 and 100 , inclusive. As each number is read, display it only if it's not a duplicate of a number already read. Provide for the "worst case," in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user enters each new value.

Short Answer

Expert verified
Initiate an array of size 5. Input five numbers from the user, ensuring each is between 10 and 100. Check for duplicates before adding to the array. Display each unique number as entered and display all unique values after all inputs.

Step by step solution

01

Initialize the Array

Create a one-dimensional array with a size of 5 to store the unique numbers. Initialize all positions with a value that is outside the allowed range of input (for example, 0 or -1 if only positive numbers are allowed) to indicate they are empty.
02

User Input

Prompt the user to enter five numbers, one at a time. Ensure that each number is between 10 and 100, inclusive. If the user enters a number outside this range, ask them to re-enter a valid number.
03

Check for Duplicates

For each number entered by the user, check if it is a duplicate by comparing it against the elements already in the array. If the number is not found in the array, it is not a duplicate.
04

Store Unique Numbers

If the number is not a duplicate, add it to the array in the first available position (where the placeholder value indicates an empty position).
05

Display Non-duplicate Number

After each user input, if the number is not a duplicate, display the number to acknowledge its acceptance.
06

Display All Unique Values

After all five numbers are input, display the complete set of unique numbers by iterating over the array and displaying the numbers that do not have the placeholder value indicating they are empty positions.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

One-Dimensional Arrays
In programming, an array is a collection of elements that can be accessed by indexing. In Java, a one-dimensional array is like a list of items with a fixed size, where each item can be a number, character, object, etc., and has a specific position referred to by an index.

For the given duplicate elimination problem, a one-dimensional array is ideal because it allows us to store a sequence of numbers. Since the size of input is known and fixed (five numbers), we can use an array of size five to hold these values. Initialization of the array is crucial, as it sets up each element with a default value that signifies an empty or unused position - typically a value outside the acceptable input range. This initialization helps in tracking which positions in the array are still available for storing unique numbers.
Java User Input Validation
Input validation is an essential aspect of reliable applications. It ensures that the user's input meets the application's criteria before proceeding with further processing. In Java, the Scanner class is commonly used to read user input.

For our exercise, input validation involves checking whether the provided number is between 10 and 100. This is critical to prevent erroneous data from being processed. If a user tries to input a number outside of this range, the program should prompt them to re-enter a valid number. This loop of validation continues until the user complies, which safeguards the integrity of the array's data.
Unique Value Storage
Storing unique values is a frequent necessity in software development to avoid duplicate data, which can lead to inaccurate results. In our case, we aim to store unique numbers from user input. To achieve this, every new number entered by the user must be checked against existing numbers in the array.

If the current input number doesn't match any of the numbers already stored in the array, it is considered unique, and therefore, it's stored in the array. We use the first empty position for the new unique number, indicated by our predefined placeholder value. This systematic approach to storing unique values helps maintain the integrity and the intended functionality of the array.
Array Iteration
Array iteration is the process of accessing each element in an array, one by one, usually with the purpose of performing some operation on the elements. In Java, this can be done using a for-loop or an enhanced for-loop, also known as the for-each loop.

In the context of our duplicate elimination problem, iteration is used after all inputs have been received to display all unique values. We loop through the array, checking if the element is not the placeholder value to determine if it is a unique number that should be displayed. Iterating over the array allows us to systematically access and process each element, ensuring that all unique entries are presented to the user.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java application. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this problem, you'll simulate the operation of the turtle and create a computerized sketchpad. Use a 20 -by- 20 array floor that's initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0,0) of the floor with its pen up. The set of turtle commands your application must process are shown in Fig. 7.29 Command Meaning 1 Pen up 2 Pen down 3 Turn right 4 Turn left 5,10 Move forward 10 spaces (replace 10 for a different number of spaces) 6 Display the 20-by-20 array 9 End of data (sentinel) Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and display a 12 -by-12 square, leaving the pen in the up position: \\[ \begin{array}{l} 2 \\ 5,12 \\ 3 \\ 5,12 \\ 3 \\ 5,12 \\ 3 \\ 5,12 \\ 1 \\ 6 \\ 9 \end{array} \\] As the turtle moves with the pen down, set the appropriate elements of array floor to 1 s. When the 6 command (display the array) is given, wherever there's a 1 in the array, display an asterisk or any character you choose. Wherever there's a \(0,\) display a blank. Write an application to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.

(Total Sales) Use a two-dimensional array to solve the following problem: A company has four salespeople \((1 \text { to } 4)\) who sell five different products \((1 \text { to } 5) .\) Once a day, each salesperson passes in a slip for each type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month's sales and summarize the total sales by salesperson and by product. All totals should be stored in the two- dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by salesperson for last month. Your output should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.

a) Error: Assigning a value to a constant after it has been initialized. Correction: Assign the correct value to the constant in a final int ARRAY_SIZE declaration or declare another variable. b) Error: Referencing an array element outside the bounds of the array (b[10]). Correction: Change the \(<=\) operator to \(<\) c) Error: Array indexing is performed incorrectly. Correction: Change the statement to a \([1][1]=5 ;\)

\(\quad\) (Sieve of Eratosthenes) A prime number is any integer greater than 1 that's evenly divisible only by itself and \(1 .\) The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create a primitive-type boolean array with all elements initialized to true. Array elements with prime indices will remain true. All other array elements will eventually be set to false. b) Starting with array index \(2,\) determine whether a given element is true. If so, loop through the remainder of the array and set to false every element whose index is a multiple of the index for the element with value true. Then continue the process with the next element with value true. For array index \(2,\) all elements beyond element 2 in the array that have indices which are multiples of 2 (indices 4,6,8,10 , etc.) will be set to false; for array index \(3,\) all elements beyond element 3 in the array that have indices which are multiples of 3 (indices \(6,9,12,15,\) etc.) will be set to \(f\) alse; and so on. When this process completes, the array elements that are still true indicate that the index is a prime number. These indices can be displayed. Write an application that uses an array of 1000 elements to determine and display the prime numbers between 2 and \(999 .\) Ignore array elements 0 and 1

Write statements that perform the following one-dimensional-array operations: a) Set the 10 elements of integer array counts to zero. b) Add one to each of the 15 elements of integer array bonus. c) Display the five values of integer array bestscores in column format.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free