In: Computer Science
please fix the code at the bottom to also report the percentages as well as the counts. the person who did it forgot this part . the code is bellow the instructions:
We will generate random values, but they should be limited to 0, 1, 2, or 3. To do this, think of a way to map the random value to a small value; there are many ways to do this, however, the way you choose must be reproducible. That is, if it maps value X to the value 2, it should do that every time the value is X. Create a function that accomplishes random number generation and mapping: the function should return a single integer that is a random value of 0, 1, 2, or 3. It does not need any inputs. To verify that it works, have your program print about 20 from it. If your program gives you values like -1 or 4, you know you have a problem. Also, if it never generates one of the values (0, 1, 2, or 3), then there is a problem.
Then create an array of 4 ints. Initialize that array to 0 values. Have your program prompt the user for an integer number, read it in, then generate that many random values (0, 1, 2, or 3). Do not show the random values. Instead, count them, i.e., every time the function produces a 0, the integer at array position 0 should be incremented. Once all the random numbers have been processed, display the counts. Verify that the numbers make sense; if your program says that there were 4 zero values, 6 ones, 5 twos, and 3 threes, but it was supposed to generate 20 values, you know there is a problem because 4+6+5+3 = 18, not 20. Also have your program report the percentages as well as the counts. The percentages should be shown with one digit after the decimal, and they should add up to 100% (neglecting any round-off error).
Test your program a few times, and note the relative number of each generated value. Assuming an even distribution, you would see the same counts for each value, i.e. 0 would be generated 25% of the time, 1 would be 25% of the time, etc. The more values the program generates, the closer to 25% each one should be.
Prepare the log like you normally do: use "cat" to show the C programs, use "gcc" to compile them, and show that the programs run.
code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int seed;
// Taking seed value as input from the user
printf("Enter a seed value (0 to quit): \n");
scanf("%d", &seed);
// Running the loop until user enters 0 to quit
// count array will count frequency of 0 , 1 , 2 ,3
int count[4];
for (int i = 0; i < 4; i++) count[i] = 0;
while (seed != 0)
{
// Passing seed value to the function srand()
srand(seed);
// Printing 5 random values
for (int i = 0; i < 5; i++) {
// generae a random number
// and take it modulo with 4
int rand_num = rand() % 4;
printf("%d ", rand_num);
count[rand_num]++;
}
printf("\n");
// Taking next seed value as input from the user
printf("Enter a seed value (0 to quit): \n");
scanf("%d", &seed);
}
// print count of each element [0-3]
for (int i = 0; i < 4; i++) {
printf("Count of %d = %d\n", i , count[i]);
}
return 0;
}
Program Code [C]
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// Required a function which generates a random integer and return it
int getRandomNumber() {
int rand_num = rand() % 4; // Range 0-4
return rand_num;
}
int main()
{
srand(time(0)); // seed for random number
generator
// Asking user for number of random values to
generate
int num;
printf("Enter number of random values to generate:
");
scanf("%d", &num);
// count array will count frequency
// Blelow is our array of ints
int count[] = {0, 0, 0, 0}; // Initialize with 0
// Now generating that much random values using
function
int i;
for (i=0; i<num; i++) {
int rand_num =
getRandomNumber();
// For each random number generated
count them
printf("%d\n", rand_num); // Also
showing random number, you can comment it if you want
count[rand_num]++;
}
// print count of each element [0-3]
for (i = 0; i < 4; i++) {
printf("Count of %d = %d\n", i ,
count[i]);
}
printf("------------------------------\n");
// Now in another loop also showing
percentages
for (i=0; i<4; i++) {
// Also showing
percentages
float percent = (count[i] * 100.0)/
(float)num;
printf("Percentage of %d =
%.2f%%\n", i, percent);
}
return 0;
}
Sample Output:-
-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!