Random generating a array of enum with no repeat elements

I am trying to generate a array of enum with no same elements, but what I have done still gives me repeat elements. What is wrong with my codes?

P.S I haven't learnt vectors yet thus am not allowed to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

const int MAXNO = 7;
const int MAX = 10;

typedef void* VoidPtr;

enum Fruit {Durian, Papaya, Apple, Orange, Mango, Jackfruit, Berry};

void constructVPArray (VoidPtr *, int);
void printVPArray (VoidPtr*, int);

VoidPtr getAFruitVP ();

Fruit getVP (VoidPtr);

char * getFruitStr (Fruit);

bool isDuplicate(VoidPtr, int size, VoidPtr);


int main ()
{
	srand (time (NULL));
	
	VoidPtr* vpArray;
	
	int size = rand () % 7;
	
	cout << "size: " <<size << endl;
	
	vpArray = new VoidPtr [size];
	
	constructVPArray (vpArray, size); 
	printVPArray (vpArray, size);	 	 

}

VoidPtr getAFruitVP ()
{
	int k = rand () % MAXNO;
	Fruit f = static_cast <Fruit>(k);
	
	Fruit *fruitPtr;
	fruitPtr = new Fruit;
	*fruitPtr = f;
	
	VoidPtr vp = fruitPtr;
	
	return vp;
}


void constructVPArray (VoidPtr *vpArray, int size)
{
	VoidPtr *p = &vpArray [0];
	VoidPtr *q = &vpArray [0];
	
	for (int i = 0; i < size; i++)
	{
		*p = getAFruitVP ();
		
		do
		{
			*p = getAFruitVP ();
			
		}while (isDuplicate(*q, i, *p));
		/*
		while (isDuplicate(*q, i, *p))
		{
			*p = getAFruitVP ();	
		} 
		*/	
		++p;
	}
}


void printVPArray (VoidPtr *vpArray, int size)
{
	Fruit f;
	
	VoidPtr *p = &vpArray [0];
	
	for (int i = 0; i < size; i++)
	{
		f = getVP (*p);
	
		cout << f << " --- " << getFruitStr (f) << endl;
		
		++p;
	}
}	 	 

Fruit getVP (VoidPtr vp)
{
	Fruit *fPtr = static_cast <Fruit *>(vp);
	Fruit f = *fPtr;
	
	return f;
}

char * getFruitStr (Fruit f)
{
	char fruitStr [][MAX] = {"Durian", "Papaya", "Apple", "Orange", "Mango", "Jackfruit", "Berry"};
	
	int k = static_cast <int> (f);
	
	char *str = new char [MAX];
	strcpy (str, fruitStr [k]);
	
	return str;
}

bool isDuplicate(VoidPtr q, int size, VoidPtr p)
{				
    for(int j = 0; j < size; j++)
	{
        if (q == p) 
			return true;
		else
			++q;
    }
    return false;
}
eh. without vectors or shuffle... you are doing way too much work here :)

you can do something like this.

int earray[size];
//fill it in with your values here of course.

for(int i = 0; i < size*2; i++)
{
swap(earray[rand()%size], earray[rand()%size]); //you have to write swap.
}
Last edited on
this is totally unrelated but watch and learn:

enum Fruit {Durian, Papaya, Apple, Orange, Mango, Jackfruit, Berry, max_Fruit};

now you can do this:

for(int i = Durian, i < max_Fruit, i++)
and the loop still works if you do this later:

enum Fruit {Durian, Papaya, Apple, Orange, Mango, Jackfruit, Berry, Grape, Pineapple, max_Fruit};

as would an array of them:

Fruit x[max_Fruit];

etc. Everything adjusts automatically when you insert more enums if you uses a max on them.

You seem to missing a few basics, pointers and arrays.
https://computer.howstuffworks.com/c22.htm
https://computer.howstuffworks.com/c34.htm
Topic archived. No new replies allowed.