'c++ test if random number is positive or negative
I have school work with C++ and Im new to it. I have to generate 17 random numbers and then test if they are positive/negative/equal to zero. I can't seem to find a way to take the random generated number from the for loop to test it in the if/else if loop. Heeeeelp please :D (The b variable is just there from tests)
#include <ctime>
using namespace std;
int main() {
srand(
(unsigned) time(0)
);
int a;
int b;
int num;
for(int a = 0; a<17; a++) {
cout << "Skaitlis: " << rand()% 20 + -10 << "\n";
if(b > 0){
cout << b << " is a positive number." << endl;
}
else if (b == 0){
cout << b << " is equal to zero." << endl;
}
else {
cout << b << " is a negative number." << endl;
}
}
}
Solution 1:[1]
First of all, your variable b has no value at all. In your loop, you are asking 17 times if b is greater, lesser or equal to 0, where is your problem.
You have never never assigned to b. You will fix it by adding
b = rand() % 20 + -10 inside your loop. Now your b has an actual value.
Also, instead of
cout << "Skaitlis: " << rand()% 20 + -10 << "\n";,
change to
cout << "Skaitlis: " << b << endl;.
Also, your cout doensn't exist since it's never declared. What I want to say is that you don't have cout function in your program because cout is found in "iostream" library. You have to include "iostream" library as well, with #include <iostream> at the start of the code.
To clarify:
Your b has no value. Assign a value to b before asking if it's greater, lesser or equal to 0;
cout is not added in your program. Include "iostream" with #include <iostream>
Final code can look like:
#include <iostream> // so I can use cout << and cin >>
#include <ctime>
int main() {
int b;
srand(time(NULL)); // NULL is same as 0
for (int a = 0; a < 17; a++) {
b = rand() % 21 - 10; // Assign a value between -10 and +10 to b
if (b > 0) cout << b << "is greater than 0" << endl;
else if (b < 0) cout << b << "is lower than 0" << endl;
else cout << b << "is equal to 0" << endl;
}
return 0;
}
I hope this explanation helps.
Solution 2:[2]
You can use:
#include <random>
void function(){
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(-100, 100); // define the range
int randomNumber = distr(gen);
if(randomNumber > 0)
// bigger than 0
else if(randomNumber == 0)
// equals 0
else
// less than 0
}
Solution 3:[3]
I'm not sure if that's what you mean, but you can just assign the result of your random function to a variable, for example:
int rnd = rand() % 20 - 10;
cout << "Skaitlis: " << rnd << '\n';
if (rnd > 0)
cout << rnd << " is a positive number." << endl;
else if (rnd == 0)
cout << rnd << " is equal to zero." << endl;
else
cout << rnd << " is a negative number." << endl;
Also, if you are just starting programming, I don't think that's necessary to know, but keep in mind that rand() isn't really a good way to create random numbers. It's way better to use for example mt19937 (although it's a little bit more complicated). rand() function generates random numbers only up to 32767 (though it also depends on the compilator you're using) and if you're trying to get a random number in a specific range, there's also a higher chance , that the rand() function will return a smaller number.
Solution 4:[4]
Consider this sample code:
#include <iostream>
#include <string>
bool is_positive(int number)
{
if (number >= 0) return true; // Positive number or 0
else return false; // Negative number
}
int main()
{
std::srand(time(0));
for (int i = 0; i < 17; i++)
{
int number = std::rand() % 20 + -10;
if (is_positive(number))
{
std::cout << number << " is positive" << std::endl;
}
else
{
std::cout << number << " is negative" << std::endl;
}
}
}
Here I have defined a function is_positive that returns true if the number is greater than or equal to 0 else it returns false. I have also shown a simple implementation of the function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Heroes Of Balkan |
| Solution 2 | |
| Solution 3 | Hubizon |
| Solution 4 | Solved Games |
