Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The function rand() returns values in the interval [0, RAND_MAX] . If we declare

ID: 3529788 • Letter: T

Question

The function rand() returns values in the interval [0, RAND_MAX] . If we declare the variable median and initialize it to have the value RAND_MAX/2, then rand() returns a value that is sometimes larger than median and sometimes smaller. On average, however, there should be as many values that are larger as there are values that are smaller than the median. Test this hypothesis. Write a program that calls rand(), say 500 times, inside a for loop, increments the variable plus_cnt every time rand() returns a value larger than median, and increments the variable minus_cnt every time rand() returns a value less than median. Each time through the for loop, print out the value of the difference of plus_cnt and minus_cnt. This difference should oscillate near zero. Does it? Normally, if you implement this program correctly, output is 14 or -14. This is because rand() generates pseudo random numbers (similar to getting number from a list of numbers which is static). Example(output of your program): -14

Explanation / Answer

#include<stdio.h>

#define RAND_MAX 10000;

int main()

{

int i,randNumber,median;

int pls_cnt=0,mns_cnt=0;

for(i=0;i<500;i++)

{

randNumber=random(10000);

median=10000/2;

if(randNumber<median)

{

pls_cnt++;

}

else

{

if(randNumber<median)

{

mns_cnt++;

}

}

printf("difference b/w plus_count and minus count is%d ",pls_cnt-mns_cnt);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote