(Duplicate Elimination) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100 , inclusive. As each number is read, validate it and store it in the array only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem.

Short Answer

Expert verified
Initialize a 91-element array, read and validate each number, check for duplicates during input, store only unique values, and then display these unique values.

Step by step solution

01

Initialize an Array

Start by initializing an array that will hold the numbers entered by the user. Given the scenario of them being all unique, the largest number of unique values between 10 and 100 is 91. So, the array should be capable of holding 91 integers.
02

Read Numbers and Validate

Create a loop that runs 20 times to read in the numbers. For each number inputted by the user, validate it to ensure it falls within the range of 10 to 100 inclusive.
03

Check for Duplicates and Store Unique Values

Inside the loop, before storing the number in the array, check if it's already present in the array. If it is not present, store it. Otherwise, skip to the next input.
04

Display Unique Values

After all 20 numbers have been read, iterate over the array and display the numbers stored in it. Since the stored numbers are all unique, you don't need any extra checks at this point.
05

Smallest Possible Array Justification

Smallest possible array in this case is one that exactly holds all unique numbers that can be inputted. Since the numbers range from 10 to 100, there are 91 possible unique numbers and so the array must have a size of 91 to handle the 'worst case' scenario.

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.

Understanding One-Dimensional Arrays
One-dimensional arrays in C++ are fundamental data structures that store a collection of elements of the same data type in a linear form. Think of it like a row of mailboxes, each with its own unique index, starting from zero. For tasks such as storing a limited set of elements that can be accessed easily based on their position, one-dimensional arrays come in handy.

To declare an array, you need to specify the data type of its elements and the number of elements it will hold. For instance, int numbers[91]; would declare an array named 'numbers' that can store up to 91 integers. It's important to note that the size of the array should be set based on the maximum number of elements that could be needed, as in the case of our problem where we could potentially have 91 unique values between 10 and 100, inclusive.

Accessing or modifying the array is done using the array's index, where numbers[0] refers to the first element and numbers[90] to the last, in a 91-element array. It's crucial to remember that array indices in C++ start with zero, so an array declared with 91 elements has valid indices from 0 to 90.
Number Validation Techniques
Validating input ensures that only acceptable data is processed by a program. In the context of array duplicate elimination, number validation is integrated to check whether entered numbers fall within the required range before considering them for storage.

To perform number validation, conditional statements are used. For example, if a user inputs a number, you could use a statement like if(number >= 10 && number <= 100) to ascertain that the number is within the inclusive range of 10 to 100. If the input doesn't satisfy the condition, it's typically ignored or the user is prompted to enter a valid number.

  • Ensures data integrity by accepting only numbers that meet specific criteria.
  • Prevents out of range errors which can occur when an input is outside the bounds expected by the program.
  • Improves user experience by guiding users to provide inputs that the program can handle correctly.
Unique Values Storage in Arrays
Storing unique values is fundamental when the need arises to eliminate redundancy in a dataset. In a one-dimensional array, such as the one used to solve the duplicate elimination problem, you require a method to check for uniqueness before any insertion takes place.

One approach is to iterate over the array each time a new number is read, comparing the input with stored values. If a match is found, it indicates a duplicate, and the number is not added again. This comparison before storage is critical:

  • It maintains the integrity of the dataset by ensuring no repeated elements.
  • It's space-efficient since only unique items occupy the storage.
  • Improves the accuracy of operations performed on the dataset post storage, as duplicates can lead to erroneous results in computations or analyses.
In our problem, once the validation confirms a number is within the correct range, the program must check the existing array content to verify if that number has not been previously stored, hence maintaining a collection of unique values.

To display the unique values, iterate through the array after the input phase is complete and print each element. Since all previously stored values are guaranteed unique, no further checks are necessary during this display phase.

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

(Fill in the Blanks) Answer each of the following: a) Lists and tables of values can be stored in ___ or ___. b) The elements of an array are related by the fact that they have the same ___ and __. c) The number used to refer to a particular element of an array is called its __. d) \(A(n)\) __ should be used to declare the size of an array, because it makes the program more scalable. e) The process of placing the elements of an array in order is called __ the array.vv f) The process of determining if an array contains a particular key value is called __ the array. g) An array that uses two subscripts is referred to as a(n) __ array.

(Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to \(n-1\), where \(n\) is the number of values in the array to be sorted. Each row of the two- dimensional array is referred to as a bucket. Write a function bucketsort that takes an integer array and the array size as arguments and performs as follows: a) Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7,3 is placed in row 3 and 100 is placed in row \(0 .\) This is called a "distribution pass." b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the onedimensional array is 100,3 and 97 c) Repeat this process for cach subsequent digit position (tens, hundreds, thousands, etc.). On the second pass, 100 is placed in row 0,3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row \(9 .\) After the gathering pass, the order of the values in the one-dimensional array is 100,3 and \(97 .\) On the third pass, 100 is placed in row 1,3 is placed in row zero and 97 is placed in row zero (after the 3). After the last gathering pass, the original array is now in sorted order. Note that the two-dimensional array of buckets is 10 times the size of the integer array being sorted. This sorting technique provides better performance than an insertion sort, but requires much more memory. The insertion sort requires space for only one additional element of data. This is an example of the space-time trade-off: The bucket sort uses more memory than the insertion sort, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.

(Dotable Array Questions) Consider a \(2-\mathrm{by}-3\) integer array t. a) Write a declaration for t. b) How many rows does t have? c) How many columns does t have? d) How many elements does t have? e) Write the names of all the elements in row 1 of \(t\) f) Write the names of all the elements in column 2 of t. g) Write a statement that sets the element of t in the first row and second column to zero. h) Write a series of statements that initialize each element of t to zero. Do not use a loop. i) Write a nested for statement that initializes each element of to zero. j) Write a statement that inputs the values for the elements of trom the keyboard. k) Write a series of statements that determine and print the smallest value in array t. l) Write a statement that displays the elements in row 0 of t. \(\mathrm{m}\) ) Write a statement that totals the elements in column 3 of t. n) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row.

( The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1\. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain \(1 .\) All other array elements will eventually be set to zero. You'll ignore elements 0 and 1 in this exercise. b) Starting with array subscript 2 , every time an array element is found whose value is 1 loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the clement with value 1 . For array subscript \(2,\) all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4,6 \(8,10, \text { etc. }) ;\) for array subscript \(3,\) all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts \(6,9,12,15,\) etc.); and so on. When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 2 and \(999 .\) Ignore element 0 of the array.

(Print a String Backuard) Write a recursive function stringReverse that takes a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string.

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