# forcing division with int result

This is a discussion on forcing division with int result within the C++ Programming forums, part of the General Programming Boards category; How can you force the a program to only pick numbers that are divisible to an integer? Code: for (pro ...

1. ## forcing division with int result

How can you force the a program to only pick numbers that are divisible to an integer?
Code:
```for (pro = 0; pro < numpro; pro++) //keeps a count until number of problems desired is reached
{
srand((unsigned)time(0)); // allows for random number generation
num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand(); // picks a second random number that will not be the first
num2 = rand() % 12 + 1;

cout << "\n\n " << num1 << " / " << num2 << " = " ;
cin >> ansr;
total = num1/num2;
{
if (total == ansr) // verifies input answer is correct
{
right++;
cout << "\n Correct!" << endl;
}
else if (total != ansr)
{
wrong++;
cout << "\n Incorrect. The correct answer is: " << total << endl;
}
}```
This way for instance you don't generate 5/12 and 0 is the correct answer. I'm assuming you could use an if else statement so that if the answer doesn't equal an int it picks to different numbers but how would you set that up since you can't make something like
Code:
```if (num1 + num2 = int)
cin >> ansr;
else if (num1 + num2 !=int)
srand((unsigned)time(0));
num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand();```
because int isn't really something assigned right?

2. Code:
```BOOL IsWholeDiv(int n1, int n2)
{
double dt = double(n1) / double(n2);
int nt = int(double(n1) / double(n2));
if(double(nt) == dt) return TRUE;
return FALSE;
}```

3. Code:
```num1 = rand();
num1 = rand() &#37; 12 + 1;
num2 = rand(); // picks a second random number that will not be the first
num2 = rand() % 12 + 1;```
I think it has been pointed out once to you that it is not necessary to call rand twice for each random number you want. (The red lines make no sense.)

So you want to draw two random numbers so that the first number is evenly divisible by second?

Firstly note that your comment is wrong. There is no guarantee that both numbers won't end up equal.

Secondly, since the first number may happen to be a prime (in addition to other complications), it might be easier to pick the result and the divider (num2) randomly, and then calculate the missing number (num1) using multiplication.

4. i apprecieate the quick response but i'm looking for some understanding of the language not just a quick answer but thank you agian for the help

5. Originally Posted by anon
Code:
```num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand(); // picks a second random number that will not be the first
num2 = rand() % 12 + 1;```
I think it has been pointed out once to you that it is not necessary to call rand twice for each random number you want. (The red lines make no sense.)

So you want to draw two random numbers so that the first number is evenly divisible by second?

Firstly note that your comment is wrong. There is no guarantee that both numbers won't end up equal.

Secondly, since the first number may happen to be a prime, it might be easier to pick the result and the divider (num2) randomly, and then calculate the missing number (num1) using multiplication.
It HAS been pointed out to me before but as I wrote in the reply that is how my professor wants it coded. Its not my option but thank you for pointing that out again.

6. Whoa, wait a minute. What kind of professor wants you to call rand, then call it again, just to overwrite the previous result?

7. If you want to "pick a second random number that will not be the first", then you need somthing like this
Code:
```num1 = rand() &#37; 12 + 1;
num2 = num1;
while(num2 == num1) num2 = rand() % 12 + 1;```

8. thats just how he recommended writing it and said he preferred it that way

10. Code:
```for (pro = 0; pro < numpro; pro++) //keeps a count until number of problems desired is reached
{
srand((unsigned)time(0)); // allows for random number generation
num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand(); // picks a second random number that will not be the first
num2 = rand() % 12 + 1;

cout << "\n\n " << num1 << " / " << num2 << " = " ;
cin >> ansr;
total = num1/num2;
{
if (total == ansr) // verifies input answer is correct
{
right++;
cout << "\n Correct!" << endl;
}
else if (total != ansr)
{
wrong++;
cout << "\n Incorrect. The correct answer is: " << total << endl;
}
}```
You don't need to indent such a ridiculous amount of tabs- The above will do just fine. It will just cause people to have to scroll.
Also note that you're missing a } at the end.

Code:
```if (num1 + num2 = int)
cin >> ansr;
else if (num1 + num2 !=int)
srand((unsigned)time(0));
num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand();```
I want to note to you that this code should be
Code:
```if (num1 + num2 = int)
cin >> ansr;
else if (num1 + num2 !=int)
srand((unsigned)time(0));
num1 = rand();
num1 = rand() % 12 + 1;
num2 = rand();```
Because only the first line is executed as part of the if. If you want the additional lines to be part of the if, as well, you need to use { and } around the else if.

Originally Posted by got1sleeve
It HAS been pointed out to me before but as I wrote in the reply that is how my professor wants it coded. Its not my option but thank you for pointing that out again.
Code:
`num1 = rand();`

Code:
`num1 = rand() % 12 + 1;`
Call rand again, divide by 12 and take the remainder and add 1, then store in num1, erasing any previous contents.

Doesn't make sense, does it?
Either your teacher isn't thinking right or you are not understanding what your teacher means.

Originally I wasn't using that buy monday my professor said he reccomends writing it out that way because the first one assigns a rand value to num1 and then the second line keeps it below 100.
Are you sure that he didn't mean:
Code:
```num = rand(); //first line picks a random number
num = num &#37; 100; //keeps it under 100```
Otherwise that would really be strange.

Anyway, if you can't use my multiplication idea for some reason, you'll need to take into account that the first number has to be at least twice as big as the second one (if the numbers have to be different - better not accept 1 in the first place), and if the first number happens to be a prime, the second number can only be 1.

You can pick the first number randomly and then keep picking random numbers up to half the size of the first until they are evenly divisible (which can be checked using modulus).

12. Originally Posted by got1sleeve
It HAS been pointed out to me before but as I wrote in the reply that is how my professor wants it coded. Its not my option but thank you for pointing that out again.
Sorry you're wrong about that. That is not how your professor wants it coded. You get a lot of people misinterpreting what they've been told to do on here, claiming that they were told to do the most ridiculous of things. It's practically always a misunderstanding on behalf of the poster.

Calling rand unnecesarily is harmful as it shortens the length of the random sequence.
The only possible interpretation I can see of what your professor meant is what anon shows.

13. Code:
```for (pro = 0; pro < numpro; pro++)
{
srand((unsigned)time(0));```
Calling srand in the loop? Does your prof want it written that way too?

14. ok so i took the advice your advice and 1) changed the num gen and 2) went with the multiplication idea...no problems really except for the fact that I added a portion that tells you what the percentage was that you got right. It always produces a 0% right if i do say 3 out of 4 correct. I applied the counter and stuff from another project so i don't really understand what is going wrong here.

Code:
```case 'D':
case 'd':
cout << "\n How many problems would you like to do?  " ;
cin >> numpro;
for (pro = 0; pro < numpro; pro++) //keeps a count until number of problems desired is reached
{
srand((unsigned)time(0));
total = rand() % 12 + 1;
num2 = rand() % 12 + 1;
num1 = total * num2;

cout << "\n\n " << num1 << " / " << num2 << " = " ;
cin >> ansr;
{
if (total == ansr) // verifies input answer is correct
{
total +=right;
cout << "\n Correct!" << endl;
}
else if (total != ansr)
{
total += wrong;
cout << "\n Incorrect. The correct answer is: " << total << endl;
}
}
}

percent = double (total/numpro) * 100;
cout << "\n You got " << percent << "% right. " ;
break;```
Thanks for the help and direction....I apologize for my stubborness and misunderstanding I really do appreciate the help i'm being given.

15. If total and numpro are both integers, the result of the division will be truncated (to 0 or 1 if they are equal) before you get to cast it to double. Try casting one of them before dividing, or multiply by 100.0* first.

Code:
`double percent = count * 100.0 / total;`
You should still remove the srand call from the loop. srand should be called only once in your program. Otherwise, if you managed to answer within a second you'd be presented the exact same numbers again (because time would return the same value and for each seed rand() produces a deterministic sequence).

* 100.0 is interpreted as double and this will force intermediate result to be a double; 100 is an integer literal.