How to divide 2 int values in MS C++?

Dear experts!

Regardless I’m professional programmer with good experienceI’m like baby on MS C++ 2008. Could you please help me with this simple question (but may be not so easy in C, xexe )?

 I should divide 2 Integer values and to get back to integer. The precision is not very necessary, if result is for example 2.7 will be nice to get it as 3, but if I get it like 2 will be acceptable. I do not want to include any additional libraries for a divide, my project is 99 % ready.

My experience with Vb6 and C# tell me to try like this, with attempt to make round

   Int InterruptAtMS = 10630; double mFramePerMsSeconds=40;

   int nFrame= (int)((double)InterruptAtMS / mFramePerMsSeconds);

but it doesn’t work. Suggestion?
dvplayltdAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
in c you have case sensitive names, so i wonder what "Int" type is cause "int" type is lower-case only.

generally you don't need a cast when dividing an int by a double or vice versa. the compiler automatically would use a temporary double for the integer operand. so the result of the computation always is double regardless whether you do a cast on operands or not.

so 10630/40 is 265.75

if you assign the double result to an integer it was always rounded down. you also don't need a cast for that but some compilers will give a warning because of the truncation of the fraction:

int x = 10630;
double y = 40.;
int z = x/y;   // z = 265

you would get rid of the warning - if any - by

int z = (int)(x/y);

if you want the result to be rounded you should add 0.5 to the result before assigning it to the integer.

int z = (int)(x/y + 0.5);  // now z = 266

that works cause by adding 0.5 you would 'move' a number with fraction .5 or higher to next integer boundary.

Sara


0
 
Todd GerbertIT ConsultantCommented:
Hmm, works for me (I should mention C# is more my thing than C++).  What's the error you're getting?

And if both your operands are int's, and you want an int result, why not make mFamePerMsSeconds an int instead of a double?

// test.cpp : Defines the entry point for the console application.
//
#include <conio.h>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 10630;
	double b = 40;

	int c = (int)((double)a / b);

	printf("Result: %i\r\n", c);
	printf("Press any key to exit...");

	getch();

	return 0;
}

Open in new window


// test.cpp : Defines the entry point for the console application.
//
#include <conio.h>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 10630;
	int b = 40;

	int c = a / b;

	printf("Result: %i\r\n", c);
	printf("Press any key to exit...");

	getch();

	return 0;
}

Open in new window

0
 
dvplayltdAuthor Commented:
Hyyym. First - in this example I put 40, but it can be like 33.33

Well, my sutiation are little more complicated. Let me put all my code , may be it is matter of sytax.


double mFramePerMsSeconds[17];
LCard=3;
Int InterruptAtMS = 10630; double mFramePerMsSeconds[lCard]=40;

   int nFrame= (int)((double)InterruptAtMS / mFramePerMsSeconds[lCard]=);

the error which I get while debug is: There no source code avialable for current location. If i put a int nFrame=250 and remove    int nFrame= (int)((double)InterruptAtMS / mFramePerMsSeconds[lCard]=);

execution contunie fine. Help me - what   catch (COleException ex) I need to get err messages? I put here COleException only for example

, and I GET IT ONLY IF PASS VALUES WHICH PRODUCE FOR EXAMPLE 2.7, if devide result is 2.0 then all is OK.


0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
dvplayltdAuthor Commented:
To tgerbert

Well, i review your profile and look like you are one of the old "dog" here :-). But this C or C++ is not like C# and any other ..it is awful! :-(
0
 
Todd GerbertIT ConsultantCommented:
Hmm, still works for me:

#include <conio.h>
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 10630;
	double b = 33.33;
	int c = (int)((double)a / b);

	printf("Result: %i\r\n", c);

	getch();

	return 0;
}

Open in new window


But the "proper" way would probably (or might, I should say) be to round your double to an int; using floor(n + .5) will round n to the nearest whole integer (though if I understand correctly, this should be the same as casting the double to an int).  Then do int = int / int

#include <conio.h>
#include <math.h>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 10630;
	double b1 = 33.33;
	int b2 = floor(b1 + .5);

	int c = a / b2;

	printf("Result: %i\r\n", c);

	getch();

	return 0;
}

Open in new window


Are you building a debug version of your project, or a release? I'm wondering if there's some other statement not shown above that might be causing your exception....
0
 
Todd GerbertIT ConsultantCommented:
>> though if I understand correctly, this should be the same as casting the double to an int

No, I believe that's wrong - casting would discard the decimal portion, which in effect is always rounding down - using floor() might round up, or down.

I'm pretty sure the basic math operations should be the same in C++ as C#...?
0
 
Todd GerbertIT ConsultantCommented:
One last thing. ;)

>> There no source code avialable for current location

That would seem to suggest that the debugger can't find source code for the executable that it's currently debugging (i.e. your program), not necessarily that there's a problem with your code.
0
 
sarabandeCommented:
so 10630/40 is 265.75

should be

so 10630/40. is 265.75
0
 
sarabandeCommented:
the 'there is no source code available" would happen if you change code while debugging and don't stop at a breakpoint before changing. then the debugger would not (try to) compile the new code in "edit-and-compile" mode but refuse to show the changed source.

Sara
0
 
dvplayltdAuthor Commented:
10x. I implement it already and now work. Well, appears that the problem was other (in uncorrect try catch block), later debugger as you say correctly make some problems (but make it only on this divide, I put here hard nFrame=260 for test and it work, that is why I decide it is problem in divide, not in Debug process. In short .. I almost lost isomewhere in the C jungle ... :-). Thanks for your help!

Also thanks for round example, with this you win points.

P.S: And Int is int in C code, just I use Word to correct english and he make first letter Big .. :-)
0
 
Infinity08Commented:
I'd like to add one more thing :

>> int nFrame= (int)((double)InterruptAtMS / mFramePerMsSeconds[lCard]=);

There's an extra '=' character in there, which would cause a compilation error.

But more importantly, you're accessing the mFramePerMsSeconds array out of bounds. The array has only lCard items in it, but you're trying to access the item at index lCard, which is one past the end of the array. Don't do that. All kinds of nasty stuff can happen !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.