So there is a bug in my code that puts it in an infinite loop. I need that taken out and I cannot find it for the life of me.
Here is the code:
#include <iostream>
#include <cstdlib>
const int MAX = 6;
const int SIZE_OF_SAMPLES = 3;
const int REP = 5;
bool inArray (int[], int, int );
void UniqRandInt (int, int, int[]);
int main() {
// std::cerr<<"in main\n";
int arr[SIZE_OF_SAMPLES];
srand(9809); //Seed random number generator.
for (int i = 0; i < REP; i++) {
UniqRandInt(MAX, SIZE_OF_SAMPLES, arr);
for(int j = 0; j < SIZE_OF_SAMPLES; j++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
return 0;
}
void UniqRandInt(int max, int n, int result[]) {
int cntr = 0, r;
while(cntr < n) {
r = rand(); //Get random number
r = r % (max + 1);
if (inArray(result, cntr, r)) {
result[cntr] = r;
cntr++;
}
}
return;
}
bool inArray(int array[], int arrSize, int x) {
for (int i = 0; i < arrSize; ++i) {
if (array[i] == x) {
return true;
}
}
return false;
}
It is outputting somthing like: 222 000 555 000 Here is what is supposed to output: something like: 254 105 035 432 523 I need 5 rows with all different numbers.
Aucun commentaire:
Enregistrer un commentaire