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
|
#include <iostream>
#include <string>
#include <ctime>
#include <iterator>
#include <cstdlib>
using namespace std;
int randomNum(int lowerBound, int upperBound){
int random;
random = lowerBound + rand() % (upperBound - lowerBound);
return random;
}
int checkDuplicate(int size,int value, int *pholdArray){
int *arrholder = pholdArray;
for(int g =0;g<size;g++){
if(arrholder[g] == value){
return 3;
}
}
return 4;
}
/*
int* generateArray(int size){
int *parray = new int[size];
return parray;
}
*/
int* fillArray(int size, int lowerBound, int upperBound){
int counter_repeat;
int loopcount = 0;
int repeat =0;
int random;
int receive;
int setrand;
int pholdArray[size];
if(checkDuplicate(size,random,pholdArray) == 3);
counter_repeat++;
for(int i =0;i<size;i++){
random = randomNum(lowerBound,upperBound);
if(checkDuplicate(size,random,pholdArray) == 3){
counter_repeat++;
while(checkDuplicate(size,random,pholdArray) == 3){
random = randomNum(lowerBound,upperBound);
counter_repeat++;
}
}
pholdArray[i] = random;
if(i == size-1){
int *finalArray = pholdArray;
return finalArray;
}
}
//int *finalArray = pholdArray;
//return finalArray;
}
int main(){
srand (time(0));
// int *p = generateArray(10);
int *n = fillArray(10,0,10);
cout << n[1]; // works
cout << n[2]; // garbage
for(int d = 0;d<10;d++){
cout << n[6] << "\n"; // garbage
}
return 0;
}
|