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

Use functions in the implementation %uF0B7 Use rand() and srand() functions from

ID: 3538134 • Letter: U

Question

Use functions in the implementation

%uF0B7 Use rand() and srand() functions from the library.

%uF0B7 Generate random numbers using the above functions.

           

Generate a random number between 0 and 3 using rand function from the stdlib.h library. Function rand() returns value between 0 and RAND_MAX where RAND_MAX is atleast 32767.

Use the following function:

int generateNumber(void): This function generates a number between 0-3 and returns that number. Use the rand() function and the above example to generate number between 0-3.

void printResult(int ): This function takes an integer value (which is between 0-3) and print the color information based on the integer value.

0 means Green

1 means Red

2 means Blue

3 means Yellow.

Use switch statement instead of if else statement to implement this function.

Check sample output below to see how to display the results.

main():Call the above two functions in the main. In order to generate random number using rand function (as discussed above).You need the seed for the random number generator this is done by including following line in the main();

         sample output

Number generated is 1 and the color selected is red

Number generated is 3 and the color selected is yellow

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>



int generateNumber()

{

srand(time(NULL));

int number = ( rand() % 4 ) + 1;

return number;

}

void printResult(int a)

{

switch(a)

{

case 0:

{

printf("Number generated is %d and the color is Green ",a);

break;

}

case 1:

{

printf("Number generated is %d and the color is Red ",a);

break;

}

case 2:

{

printf("Number generated is %d and the color is Blue ",a);

break;

}

case 3:

{

printf("Number generated is %d and the color is Yellow ",a);

break;

}

}


}

int main()

{

int a= generateNumber();

printResult(a);

scanf("%d",&a);

}