Soru
Which one of the following statements correctly creates a random integer number between -50 and 50? Select one: a. int x=-50+rand()% 101 b. int x=50-rand()% 50 c. int x=-50+rand()% 50 d. int x=50-rand()% 100
Çözüm
4.4115 Voting
Eray
Elit · 8 yıl öğretmeniUzman doğrulaması
Cevap
The correct answer is option c: int $x=-50+rand()\% 50$.<br /><br />Explanation:<br />In this option, the expression $-50 + rand() \% 50$ is used to generate a random integer number between $-50$ and $50$.<br /><br />The function `rand()` generates a random number between $0$ and $RAND\_MAX$, where $RAND\_MAX$ is a constant defined in the `<cstdlib>` header file. The value of $RAND\_MAX$ is typically $32767$ on most systems.<br /><br />When we take the modulus of the result of `rand()` with $50$, we get a number between $0$ and $49$. Adding $-50$ to this result gives us a number between $-50$ and $-1$.<br /><br />Therefore, the expression $-50 + rand() \% 50$ correctly generates a random integer number between $-50$ and $50$.
Derecelendirmek için tıklayın: