7. Random
Q) Purpose of srand()
What is the purpose of adding the following statements to a C++ program?
int n = time(nullptr);
srand(n);
a) Make the pseodorandom number generator create different numbers each time the program runs.
b) Get a random number less than number of seconds since Jan 1, 1970.
c) Pass a null pointer to time() to make it simpler to use.
d) Get the next random number from a pseudorandom sequence.
Q) Working with AND and OR
Consider the following code:
int x = ???;
int y = ???;
int z = ???;
if (x != 9 && (y < z || z % 2 == 1)) {
cout << "YES!" << endl;
}
Which of the following provides values for x, y, and z which make the above code print "YES".
a) x = 9, y = 3, z = 4
b) x = 3, y = 4, z = 4
c) x = 9, y = 2, z = 5
d) None of the above
Q) Code Operation and Style
Consider the following code:
int score = 0;
while (score >= 0) {
print("Enter your score: ");
cin >> score;
bool isAMinus = (score >= 86 && score < 90);
if (isAMinus) {
println("great");
} else {
println("Not an A-.");
}
}
Assume an A- is "great", and is earned for a score which is 86 or more but less than 90. Which of the following is true about this code?
a) It always prints "great".
b) It never prints "great".
c) It uses an explanatory variable.
d) It should use || instead of &&.
Answers
a) Make the pseodorandom number generator create different numbers each time the program runs.
d) None of the above
c) It uses an explanatory variable.