-3

I'm trying to make a card-game, and my plan is to make an array that first holds 1-10, then J-A, then the colors, so three different arrays and different variables with ints and chars.

However, I'm stuck with the problem now that I want to remove duplicates from the array, but i dont really know how to do it, I've read some facts but I need a better explanatin and hur it would look code-wise.

This is how my code looks so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{

    int arrays[10] = { 1,2,3,4,5,6,7,8,9,10 };
    char* color[4] = { "Diamond", "Spade", "Heart", "Clubs" };
    int i;
    int *number;


    srand(time(NULL));

    for (i = 0; i < 10; i++) {
        number = arrays[rand() % 10];
        if (number ) { //Its here where i want to remove the duplicates
            continue;
        }
        else {
            printf("%d ", number);
        }
    }
    system("pause");
    return 0;

}

I would be greatly thankful for an explanation on how to do it, I tried with variables holding the value of arrays[i] but then all the numbers will be removed.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Philly
  • 13
  • 1
  • 4
  • 4
    In your example you don't have any duplicates, what do you mean by *remove duplicates*? Please provide an example, e.g.: `number == 5` -> I want X to happen. – BeyelerStudios Dec 16 '15 at 10:16
  • 2
    "remove duplicates from arrays". Your arrays do not contain any duplicates. Can you please explain what you mean more clearly? – kaylum Dec 16 '15 at 10:17
  • @JoachimPileborg you are right, I'm removing it – user3085931 Dec 16 '15 at 10:19
  • 1
    Not directly related to your problem, but `arrays[10]` is not needed at all, you can remove this array and simplify your program. I leave this as an exercicse to the reader. – Jabberwocky Dec 16 '15 at 10:20

2 Answers2

1

I think that it would be better to create one array of all the cards (like deck) and then shuffle the array, instead of using random select.. Then you can take the top 10 cards without any duplicates.

Theoretically with random select, you can select each time cards that already "drawn", and it will be waste of time (the more cards you drawn the less chance you hit no-drawn card).

Community
  • 1
  • 1
nrofis
  • 8,975
  • 14
  • 58
  • 113
0

I assume you mean that in the loop if you have "drawn" a number, you don't want to draw the same number again? Then you could have another array with a flag saying if the index have been "drawn" before, and if it have then draw again.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621