I'm fairly new to coding and am currently learning C in college. The class instructor gave us the assignment of creating a program that generates non-repetitive random numbers, stores the number in an array, and prints the array. Here's the code I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 16
int main(void) {
int counter, counter2;
int Array[SIZE] = { 0 };
printf("Nonrepetitive array values are:\n");
for (counter = 0; counter < SIZE; counter++) {
Array[counter] = 1 + (rand() % 20);
for (counter2 = 0; counter2 < counter; counter2++) {
if (Array[counter] == Array[counter2]) {
Array[counter] = 1 + (rand() % 20);
}
}
printf("Array[ %d ] = %d\n", counter, Array[counter]);
}
return 0;
}
The problem that I'm facing is checking the current array value with all of the previous array elements. Currently, I'm only able to figure out checking with the value right before the current one. How can I solve this?
Any advice is greatly appreciated. Thank you!
Aucun commentaire:
Enregistrer un commentaire