Question

In: Computer Science

please answer part 2 . the code for part 1 is provided at the end. Part...

please answer part 2 . the code for part 1 is provided at the end.

Part 1

There is a pair of functions in stdlib.h (make sure to include it) that are used for generating pseudo-random numbers. These are "srand" and "rand". Examples are on pages 686 and 687. E.g., function "srand(3)" provides the seed value of 3 to the srand function. (3 is just a number, and probably isn't even a good number for this purpose, but that's a discussion for another time). The reason that we use a seed value is so that we can reproduce the series of random numbers. Thus, we call them pseudo-random numbers. Being able to reproduce them is important to verify and debug code.

Write a C program to generate a set of random numbers for a given seed value (call it lab8_part1.c). Prompt the user for the seed value, or 0 to quit. Then print 5 random values. When you run your program, try a few different seed values, then try one that you've already tried. Verify that you get the same sequence.

Part 2

Copy the lab8_part1.c program to a new file, lab8_part2.c, to use in this part.

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 for part 1:

CODE -

#include<stdio.h>

#include<stdlib.h>

int main()

{

    int seed;

    // Taking seed value as input from the user

    printf("\nEnter a seed value (0 to quit): ");

    scanf("%d", &seed);

    // Running the loop until user enters 0 to quit

    while(seed != 0)

    {

        // Passing seed value to the function srand()

        srand(seed);

        // Printing 5 random values

        for(int i=0; i<5; i++)

            printf("%d ", rand());

        // Taking next seed value as input from the user

        printf("\nEnter a seed value (0 to quit): ");

        scanf("%d", &seed);

    }

    return 0;

}

Solutions

Expert Solution

Approach :

1.Generate a random number using random function.Lets called it rand_num

2. Take remainder of rand_num with 4 as any number % 4 will give result either 0,1,2,3

Eg. 20 % 4= 0

14 % 4 = 2

3. Declare a array count[4] and count frequency of each 0,1,2,3

4. Print count and percentage of each element 0,1,2,3

Below is commented code

#include<stdio.h>
#include<stdlib.h>
double percentage(double num, double total) {
    double ans = (num / total) * 100.0;
    return ans;
}
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], total = 0;;
    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]++;
            total++;
        }
        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]);
    // Print percentage
    for (int i = 0; i < 4; i++)
        printf("Percentage of %d = %.1f\n", i, percentage(count[i], total));
    return 0;
}

Below is screenshot of code along with output

Feel free to ask doubts in comments if any.


Related Solutions

Submit your calculation/answer for Part 1 and MATLAB code/result/answer for Part 2 below: Servicing Customers A...
Submit your calculation/answer for Part 1 and MATLAB code/result/answer for Part 2 below: Servicing Customers A supermarket you work part-time at has one express lane open from 5 to 6 PM on weekdays (Monday through Friday). This time of the day is usually the busiest since people tend to stop on their way home from work to buy groceries. The number of items allowed in the express lane is limited to 10 so that the average time to process an...
Please answer both of the 2 multi-part questions: 1. 78% of people report that they are...
Please answer both of the 2 multi-part questions: 1. 78% of people report that they are sleepy two hours after they wake up. But you believe that among people who drink coffee, this rate is lower. 1a) Determine the null and alternative hypothesis. 1b) What would it mean here to make a Type I error? 2. Write the hypotheses and the conclusion for this situation: It is published that a certain kind of light bulb has a mean lifetime of...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d)....
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d). Godspeed, and good luck!!! CC11 Cookie Creations Natalie and her friend Curtis Lesperance decide that they can benefit from joining Cookie Creations and Curtis’s coffee shop. In the first part of this problem, they come to you with questions about setting up a corporation for their new business. In the second part of the problem, they want your help in preparing financial information following...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
Please answer Part 2 (b) And please type your answer,hand-writing is sometimes hard to figure out...
Please answer Part 2 (b) And please type your answer,hand-writing is sometimes hard to figure out Before mid-night thanks Part 1 Waterways has a sales mix of sprinklers, valves, and controllers as follows. Annual expected sales: Sale of sprinklers 460,000 units at $26.50 Sale of valves 1,480,000 units at $11.20 Sale of controllers 60,000 units at $42.50 Variable manufacturing cost per unit: Sprinklers $13.96 Valves $ 7.95 Controllers $29.75 Fixed manufacturing overhead cost (total) $760,000 Variable selling and administrative expenses...
Answer ASAP PLEASE!! 1) Checkable deposit account balances are A) part of M2 but not part...
Answer ASAP PLEASE!! 1) Checkable deposit account balances are A) part of M2 but not part of M1. B) counted in the calculation of the money supply. C) considered credit, but not money. D) only a small component of the money supply. 2)Active policy making would include all of the following EXCEPT A) increased government spending by the Congress. B) tax increases. C) interest rate changes by the Fed. D) unemployment insurance benefits. 3)Which one of the following is a...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising company ABC on its merger and acquisition case. The buyer company offers ABC two options. Option #1= $100 million cash at the acquisition date. Option #2 = $25 million cash at the acquisition date and another additional $90 million AFTER one year. The management team of ABC perceives a 30 percent annual discount rate. Which option should ABC choose? Show your work. Part II:...
PART 2 Please answer these with true and false answers, but please give your explainations, use...
PART 2 Please answer these with true and false answers, but please give your explainations, use diagrams if necessary 5. Suppose the government funds the provision of a pure public good from tax revenue. The total burden to the economy of providing the good exceeds the amount spent on the good. 6. A decrease in posted (nominal) interest rates necessarily means a decrease in real interest rates. 7. At the end of 2016 the Canadian dollar exchange rate with the...
What are the typical functionalities provided by CRM software? Please answer in 2-3 paragraphs.
What are the typical functionalities provided by CRM software? Please answer in 2-3 paragraphs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT