Question:

C copy elements not in array

by Guest55997  |  earlier

0 LIKES UnLike

If the user is given an option of inputing a certain amount of int elements (MAX being 10) and then inputs those elements, how can I copy the elements he did NOT put into the array into another array?

For example,

He/She enters 4 then inputs 2 4 5 6

How could I copy 1 3 7 8 9 into another array?

Please include examples.... thanks!

 Tags:

   Report

2 ANSWERS


  1. I'm going to make you write your own code, but you could do this with two for loops.  The outside loop will iterate from 1-9 (or 0-9, or whatever your valid input range is), the inside (nested) for loop iterates through the user generated array and compares to the current value of the outside for loop.  If there is a match, you skip it, if there isn't, you add it to a third array.  When you are done this third array will contain all the unused numbers.

    Another way you could do this is to generate an array the size of the number of possible values (10 in the case of inputs 0-9).  Then you go through all the user and use your "possible values" array as a flag, so if they entered 1, then posValues[1] gets 1 added.  Then when you are done, all positions that have a value of 0 have not been used.


  2. In a realistic sense, unless you constrain the user in some way to certain inputs, there's no way to read their mind and know what other elements they didn't provide.

    And so some extent, if you do constrain them, they needn't enter any input at all and you should be able to determine the entire array.

    Sorry.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.