#include <stdio.h> #include <stdlib.h> int main(){ int count = 0; int num = 0; i
ID: 3870559 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
int main(){
int count = 0;
int num = 0;
int value[100];
printf("How many random values to generate: ");
scanf("%d", &num);
int i = 0;
int j = 0;
for (i = 0; i < 100; i++){ //initializing loop
value[i] = 0;
}
for (j = 0; j < num; j++){
srand(0);
value[j] = (rand()%100);
if (value[j] > 0)
count++;
}
printf("%d random numbers generated %d out of 100 possible values ", num, count);
return 0;
}
Name this program random.c - This program looks at random numbers (integers) in the range of 0 through 99 The program prompts for the number of values to generate in the range of 0 to 99, generates that many random numbers, and determines how many different (unique) numbers were actually generated. Running on the cs-intro server gives you the following results (three different executions of the program are shown). How many random 1 values to generate: 1 random numbers generated 1 out of 100 possible values How many random values to generate: 10 0 random numbers generated 9 out of 100 possible values How many random values to generate: 250 250 random numbers generated 93 out of 100 possible valuesExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t;
srand((unsigned) time(&t));
int count = 0;
int num = 0;
int value[100],tmp;
printf("How many random values to generate: ");
scanf("%d", &num);
int i = 0;
int j = 0;
for (i = 0; i < 100; i++){ //initializing loop
value[i] = 0;
}
for (j = 0; j < num; j++)
{
tmp = (rand()%100);
if(value[tmp]==0)
{
count++;
value[tmp]=1;
}
}
printf("%d random numbers generated %d out of 100 possible values ", num, count);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.