The function named myRandomNum that returns a random integer in the range [1, 100] is defined as follows: int myRandomNum () { return rand()%100 + 1; } Now, write a program (code fragment) that repeatedly generates and prints random integers in the range [1, 100]. The program must stop when the absolute value of the difference between successive printed values is less than 5. Two sample output is given to help you understand the problem: 1. The program can possibly output this: 76 43 77 94 54 66 64 2. The program can possibly output this: 42 45 Note: 1) The srand statement has already been provided for you (see below). 2) You must use the given function myRandomNum to generate random integers.
Raw_code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// myRandomNum() function
int myRandomNum(){
return rand()%100 + 1;
}
// main() function
int main(){
// srand function with time(NULL) as parameter
srand((unsigned)time(NULL));
// initializing two variable with random numbers by calling
myRandomNum() function
int num1 = myRandomNum();
int num2 = myRandomNum();
// variable for storing absolute difference of both random
numbers,
// initialized with absolute difference of num1 and num2
int diff = abs(num1-num2);
// printing both the random numbers
printf("%d\t%d\t", num1, num2);
// while loop runs until the diff is lesser than 5
while(diff >= 5){
// assingning num2 to num1
num1 = num2;
// generating new random number and assingning to num2
num2 = myRandomNum();
// calculating the absolute difference
diff = abs(num1-num2);
// printing the random number generating during this
iteration
printf("%d\t", num2);
}
}
// you can change srand statement and 8 different ouputs have been placed
Get Answers For Free
Most questions answered within 1 hours.