Make a program that randomly generates 80 numbers in the range of the set from 1 to 100, which are not repeated, and then generates a list with the 20 numbers that are missing, example with a set of 10 numbers generating 8 randomly: {1,2,4,5,6,7,9,10} the list with the missing ones are 3 and 8.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int aleatorio, i, restantes;
srand(time(0));
for (i=0; i<80; i++){
aleatorio = 1+ rand() %(101-1);
printf("\n %d", aleatorio);
}
return 0;
}
I already have the random numbers but I don't know how to print the remaining 20 random numbers, can someone help me please (C language)?
The simple way is to use an auxiliary array like a
cache
to know which random numbers have already appeared and which have not.Your numbers go from
1..100
, however, an array has its first index at 0. So you have to do two things:0
s and use them as a boolean value. 0 if the number has not been drawn, and 1 when the number has already been drawn.-1
from the random number. For example, if you get the number 1, its "position" in the array is the number 0.The array must be integers, since as such, the boolean data type does not exist in
C
unless you usestdbool.h
enC99
.After drawing 80 different random numbers, you can go through the auxiliary array and print the index of those positions in which its value is equal to 0, since it would indicate that these numbers never appeared.
I leave you the code with comments to make it easier to understand the solution.