hello all, I'm new to c++ and am currently reading the book "C++ how to program" by Deitel. I'm working on exercise 7.15 from his book and am just stuck with the problem.

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 is not 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.

>>Provide for the "worst case in which all 20 numbers are different

>>Use the smallest possible array to solve this problem.

The answer is an array of 20.

please post your pseudo code that you have written so far.

sk for user input for a value between 10 and 100
-check to see if the value entered by the user is between 10 and 100
-if between 10 - 100 check if the input is already in the array
-if input is between 10 - 100 and not in the array store input in the current array, go to beginning of loop

I think I need two nested loops, one to keep track of my current array location for input storage, and another loop that will check my input with all the values in the array already. I think my biggest problem is not being able to see how to create this nested loop.
ask for user input

>I think my biggest problem is not being able to see how to create this nested loop.
Okay, you know you need to loop to fill up the array, right?

array [0..MAX] of integer
num as integer

while size < MAX do
  get num from stdin

  if eof ( stdin ) then
    end loop
  endif

  array[size] := num
  size := size + 1
loop

But you also need to range check the number. This can be a simple test in the main loop:

array [0..MAX] of integer
num as integer

while size < MAX do
  get num from stdin

  if eof ( stdin ) then
    end loop
  endif

  if num >= 10 and num < 100 then
    array[size] := num
    size := size + 1
  endif
loop

The check for duplicates isn't so easy because you have an array of numbers to work with. That means, like you guessed, that a nested loop is needed. A generally safe way to check for the existence of a value in an array is to walk the entire array until you find that item. After the loop, if the index is the size of the array, the value wasn't found. You can express this trick in code like so:

array [0..MAX] of integer
num as integer
i as integer

while size < MAX do
  get num from stdin

  if eof ( stdin ) then
    end loop
  endif

  if num >= 10 and num < 100 then
    # Check for duplicates
    for i := 0 to size do
      if array[i] = num then
        end loop
      endif
    loop

    if i = size then
      array[size] := num
      size := size + 1
    endif
  endif
loop

It's a trick because you don't have to maintain a separate boolean flag for if the item was found or not, and the code is shorter as a result.

This article has been dead for over six months. Start a new discussion instead.