0

I'm doing a project in C involving arrays. What I have is an array of 7 chars, I need to populate an array with 4 random elements from the 7. Then I compare an array I fill myself to it. I don't want to allow repeats. I know how to compare each individual element to another to prevent it but obviously this isn't optimal. So if I remove the elements from the array as I randomly pick them I remove any chance of them being duplicated, or so I think. My question is how would I do this?

Example:

char name[2+1] = {'a','b'};
char guess[2+1] = {};

so when it randomly picks a or b and puts it in guess[], but the next time it runs it might pick the same. Removing it will get rid of that chance. In bigger arrays it would make it faster then doing all the comparing.

Guys it just hit me.
Couldn't I switch the element I took with the last element in the array and shrink it by one? Then obviously change the rand() % x modulus by 1 each time?

4
  • to be more clear can you give an example. Such as: I have [1,15,26,75,...] want to add [24,56.. ] Commented Dec 11, 2014 at 12:41
  • Examples, and some code to see what you already have tried and where you are stuck, would be good. Commented Dec 11, 2014 at 12:43
  • Do you want to create a new array with the unique characters from the current array? Commented Dec 11, 2014 at 12:44
  • Copy into a new array and shuffle it with Knuth. Commented Dec 11, 2014 at 12:57

3 Answers 3

1

I can give you steps to do what you intend to do. Code it yourself. Before that let's generalize the problem.

You've an array of 'm' elements and you've to fill another 'n' length array by choosing random elements from first array such that there are no repetition of number. Let's assume all numbers are unique in first array.

Steps:

  1. Keep a pointer or count to track the current position in array. Initialize it to zeroth index initially. Let's call it current.
  2. Generate a valid random number within the range of current and 'm'. Let's say its i. Keep generating until you find something in range.
  3. Fill second_array with first_array[i].
  4. Swap first_array[i] and first_array[current] and increment current but 1.
  5. Repeat through step 2 'n' times.

Let's say your array is 2, 3, 7, 5, 8, 12, 4. Its length is 7. You've to fill a 5 length array out of it.

  • Initialize current to zero.
  • Generate random index. Let's say 4. Check if its between current(0) and m(7). It is.
  • Swap first_array[4] and first_array[0]. array becomes 8, 3, 7, 5, 2, 12, 4
  • Increment current by 1 and repeat.
Sign up to request clarification or add additional context in comments.

Comments

1

Here are two possible ways of "removing" items from an array in C (there are other possible way too):

  1. Replace the item in the array with another items which states that this item is not valid.

    For example, if you have the char array

    +---+---+---+---+---+---+
    | F | o | o | b | a | r |
    +---+---+---+---+---+---+
    

    and you want to "remove" the b the it could look like

    +---+---+---+------+---+---+
    | F | o | o | \xff | a | r |
    +---+---+---+------+---+---+
    
  2. Shift the remaining content of the array one step up.

    To use the same example from above, the array after shifting would look like

    +---+---+---+---+---+---+
    | F | o | o | a | r |   |
    +---+---+---+---+---+---+
    

    This can be implemented by a simple memmove call.

    The important thing to remember for this is that you need to keep track of the size, and decrease it every time you remove a character.

Of course both these methods can be combined: First use number one in a loop, and once done you can permanently remove the unused entries in the array with number two.

3 Comments

Do you mind giving an example? if possible of course.
Variant on 2 when the order does not matter: Overwrite the used entry with the last entry, and decrease the valid size.
shifting elements would be less efficient. Instead keep a pointer(let's say current) to first element initially, and whenever a random index(check if its between current and end of array) is generated swap the value at that index and current and increment current.
0

To don't forget that an array is just a pointer on the beginning of a set of items of the same type in C. So to remove an element, you simply have to replace the element at the given index with a value that shows that it is not a valid entry i.e. null.

As the entry is a simple number, there is no memory management issue (that I know of) but if it were an object, you would have to delete it if it is the last reference you have on it.

So let's keep it simple:

array2[index2] = array1[index1];

array1[index1] = null;

The other way is to change the size of the original array so that it contains one less element, as Joachim stated in his answer.

1 Comment

What if array1 is not a pointer type? The OP mentions chars, so perhaps an "invalid" value for a char (255?) would be better as an example (as suggested in Joachim's answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.