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. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    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. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    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;
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    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. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by anon View Post
    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. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Whoa, wait a minute. What kind of professor wants you to call rand, then call it again, just to overwrite the previous result?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    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;
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    thats just how he recommended writing it and said he preferred it that way

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Huh. You need to ask him about that then.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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.

    Quote Originally Posted by got1sleeve View Post
    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.
    OK, let's read that code.
    Code:
    num1 = rand();
    OK, call rand() and assign the return to num1.

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In your other thread you wrote:
    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).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by got1sleeve View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    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. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    Last edited by anon; 01-16-2008 at 03:49 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21