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?
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.
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.