I have a program that will pick random numbers from an array. I need to modify it so it will not pick duplicate numbers (numbers can only appear once in the array). Possibly could I add the already picked values to another array and check if the value exists in both arrays? Any tips are appreciated, thank you.
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int size = 10;
int numDigits = 5;
int temp;
int x;
int finalArray[] = {};
srand(time(0));
for (int i = 0; i < 5; i++) {
array[i] = rand() % 10;
}
for (int i = 0; i < 5; i++) { // shuffle array
int temp = array[i];
int randomIndex = rand() % 10;
array[i] = array[randomIndex];
array[randomIndex] = temp;
finalArray[i] = array[randomIndex];
}
for (int i = 0; i < 5; i++) { // print array
printf("%d,",array[i]);
}
}
Aucun commentaire:
Enregistrer un commentaire