please fix the code at the bottom to also report the percentages as well as the...
90.2K
Verified Solution
Question
Programming
please fix the code at the bottom to also report the percentagesas well as the counts. the person who did it forgot this part . thecode 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 asmall value; there are many ways to do this, however, the way youchoose must be reproducible. That is, if it maps value X to thevalue 2, it should do that every time the value is X. Create afunction that accomplishes random number generation and mapping:the function should return a single integer that is a random valueof 0, 1, 2, or 3. It does not need any inputs. To verify that itworks, have your program print about 20 from it. If your programgives 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 thereis a problem.
Then create an array of 4 ints. Initialize that array to 0values. 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., everytime the function produces a 0, the integer at array position 0should be incremented. Once all the random numbers have beenprocessed, 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 knowthere is a problem because 4+6+5+3 = 18, not 20. Also have yourprogram report the percentages as well as the counts. Thepercentages should be shown with one digit after the decimal, andthey should add up to 100% (neglecting any round-off error).
Test your program a few times, and note the relative number ofeach generated value. Assuming an even distribution, you would seethe same counts for each value, i.e. 0 would be generated 25% ofthe time, 1 would be 25% of the time, etc. The more values theprogram generates, the closer to 25% each one should be.
Prepare the log like you normally do: use \"cat\" to show the Cprograms, use \"gcc\" to compile them, and show that the programsrun.
code:
#include
#include
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;
}
please fix the code at the bottom to also report the percentagesas well as the counts. the person who did it forgot this part . thecode 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 asmall value; there are many ways to do this, however, the way youchoose must be reproducible. That is, if it maps value X to thevalue 2, it should do that every time the value is X. Create afunction that accomplishes random number generation and mapping:the function should return a single integer that is a random valueof 0, 1, 2, or 3. It does not need any inputs. To verify that itworks, have your program print about 20 from it. If your programgives 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 thereis a problem.
Then create an array of 4 ints. Initialize that array to 0values. 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., everytime the function produces a 0, the integer at array position 0should be incremented. Once all the random numbers have beenprocessed, 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 knowthere is a problem because 4+6+5+3 = 18, not 20. Also have yourprogram report the percentages as well as the counts. Thepercentages should be shown with one digit after the decimal, andthey should add up to 100% (neglecting any round-off error).
Test your program a few times, and note the relative number ofeach generated value. Assuming an even distribution, you would seethe same counts for each value, i.e. 0 would be generated 25% ofthe time, 1 would be 25% of the time, etc. The more values theprogram generates, the closer to 25% each one should be.
Prepare the log like you normally do: use \"cat\" to show the Cprograms, use \"gcc\" to compile them, and show that the programsrun.
code:
#include
#include
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;
}
Answer & Explanation Solved by verified expert
Get Answers to Unlimited Questions
Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!
Membership Benefits:
- Unlimited Question Access with detailed Answers
- Zin AI - 3 Million Words
- 10 Dall-E 3 Images
- 20 Plot Generations
- Conversation with Dialogue Memory
- No Ads, Ever!
- Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Other questions asked by students
StudyZin's Question Purchase
1 Answer
$0.99
(Save $1 )
One time Pay
- No Ads
- Answer to 1 Question
- Get free Zin AI - 50 Thousand Words per Month
Unlimited
$4.99*
(Save $5 )
Billed Monthly
- No Ads
- Answers to Unlimited Questions
- Get free Zin AI - 3 Million Words per Month
*First month only
Free
$0
- Get this answer for free!
- Sign up now to unlock the answer instantly
You can see the logs in the Dashboard.