Random number generator and restricting conditions

Hi guys, working on a program that is meant to be a math quiz for the 3rd grade level. It is for homework and for the extra credit I can have the program provide addition, subtraction, multiplication, and division. The restrictions are that all numbers need to be between 0-100. Including the answer. Another restriction is that for division, a weird fraction can not be the answer. For example 81 / 9 would be valid but 15 / 6 would not be valid.

The problem that I am having is figuring out how do I tell the computer to set the restriction for no weird fractions?

Here is what I have so far.

if ((FirstNum / SecondNum) <= 100 )
{
cout << FirstNum << " / " << SecondNum << " = ";
cin >> answer;
}
if ((FirstNum / SecondNum) == answer)
{
++correct;
}
if ((FirstNum / SecondNum) != answer)
{
++incorrect;
}
}
++count;
Last edited on
You can use std::floor to remove the fractional part and compare it to see if it's still the correct answer.

1
2
3
4
5
double correctAnswer = firstNum / secondNum;
if (correctAnswer != std::floor(correctAnswer))
{
	// The answer has a non-zero fractional part...
}

http://www.cplusplus.com/reference/cmath/floor/
Last edited on
If you do accept only whole numbers, then make it so:
1
2
3
4
int second = // random from [1..100]
int correct = .// random from [0..100/second]
int first = correct * second;
std::cout << "How much is " << first << '/' << second <<'\n';

Peter87, so basically floor just rounds down the number? I'm not exactly sure that is what my professor is looking for. I believe he just wants it to come out as a clean number (easy enough for a 3rd grader like 10 /2 =5 or 81/9 =9).

keskiverto, I didn't include the part where I have my numbers randomly generated already. I know that some of my fellow classmates check the forums for help and I don't want us to end up with the same program and it be considered cheating. I'm not exactly sure how your code would fix my problem. there is still a chance to come out having a weird fraction depending on the random numbers that are generated.
there is still a chance to come out having a weird fraction depending on the random numbers that are generated.

Please explain.

Integers have no fractions. Integer division produces an integer. Just like with floor(), the fraction is discarded.

The denominator (second) is a random integer. It must be more than 0, unless you want to ask how much is x / 0

The randomly generated correct answer is an integer too. Its value is at most 100/second.

The nominator (first) is computed from two integers by multiplication. Therefore, it must be an integer too.
The value of first is at most second*100/second, i.e. <= 100.

Keskiverto, ok I understand what is going on now, only problem is that the first number ends up being a number greater than 100. second and correct may end up being between 0-100 but first is what is going to be divided by second and that unfortunately is not allowed in my program. I like the idea though and I believe I may have found a solution using modulo, if I can work out the kinks.
The first cannot become too big, if you do follow my idea.

Lets take an example. The second becomes 3.
The 100/3 is 33. Therefore, we generate a number for correct that is at least 0, but no more than 33.
The largest that the second can become is thus 3*33 = 99. That is not over 100.


Does you idea about modulo by any chance resemble what is mentioned in
http://www.cplusplus.com/reference/cstdlib/rand/
This is how I ended up using modulo to ensure that there would be no remainders for the division problem.


if (problem_type == 3)
{
int FirstNum = rand();
FirstNum %= 101;
int SecondNum = rand();
SecondNum %= 101;

if (((FirstNum / SecondNum) <= 100) && ((FirstNum % SecondNum) == 0 ))
{
cout << FirstNum << " / " << SecondNum << " = ";
cin >> answer;
}
if ((FirstNum / SecondNum) == answer)
{
++correct;
}
if ((FirstNum / SecondNum) != answer)
{
++incorrect;
}
}
Last edited on
What is the chance that you do get a valid pair? You have to repeat until you get a valid pair.

What is the chance that SecondNum==0 and you divide by 0?

1
2
3
int SecondNum = rand() % 100 + 1;
int Answer = rand() % (100/SecondNum + 1);
int FirstNum = Answer * SecondNum;
Honestly I had forgotten about if it tried to divide by zero until I went to run my program and got nan. I fixed the problem though before I turned in my program. My professor didn't like the way I handled checking for my numbers being in range of 0-100 though. Not exactly sure what he wanted since he just told me he didn't like it, without showing me a better way to do it.
Topic archived. No new replies allowed.