Question

In: Computer Science

please fix the code at the bottom to also report the percentages as well as the...

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;
}

Solutions

Expert Solution

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!!!


Related Solutions

Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
Can you fix this code please. the removing methods id no doing anything. this is java...
Can you fix this code please. the removing methods id no doing anything. this is java code import java.util.NoSuchElementException; public class DoublyLinkedList<E> {    public int size;    public Node head;    public Node tail;             @Override    public boolean isEmpty() {               return size == 0;    }    @Override    public int getSize() {               return 0;    }    @Override    public void addAtFront(E element) {       ...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
Please code in Assembly Language Code solution using the provided template AL_Template_Irvine32.asm located towards the bottom...
Please code in Assembly Language Code solution using the provided template AL_Template_Irvine32.asm located towards the bottom of the question.. Debug programs with Visual Studio2017/19. Please add single line or block comments explaining the purpose or functionality of your code statements. 10.) Fibonacci Generator Write a procedure that produces N values in the Fibonacci number series and stores them in an array of doubleword. Input parameters should be a pointer to an array of doubleword, a counter of the number of...
please use the temperature adjustment listed at the bottom for part b as well. assume: steady...
please use the temperature adjustment listed at the bottom for part b as well. assume: steady state, constant temp and pressure, ideal gas solution, B is insoluble in A, and Nb,z in gas phase =0. Evaporation of volatile liquid.  A tank with its top open to the atmosphere contains liquid benzene.   The tank and atmosphere are at 25 oC.  The inner diameter of the cylindrical tank is 1.0 m, the height of the tank is 3.0 m, and the liquid...
Please fix this code I am having issues compiling it all together there is 3 codes...
Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it. package assignement2; import java.util.ArrayList; import java.util.Scanner; public class reg1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of items: "); int number = input.nextInt(); input.nextLine(); for (int i = 0; i <...
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT