Solved

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

Posted on 2011-09-22
11
579 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:dvplayltd
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36585074
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
 

Author Comment

by:dvplayltd
ID: 36585153
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
 

Author Comment

by:dvplayltd
ID: 36585165
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36585207
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36585214
>> 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
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 36585219
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36585222
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
 
LVL 33

Expert Comment

by:sarabande
ID: 36585226
so 10630/40 is 265.75

should be

so 10630/40. is 265.75
0
 
LVL 33

Expert Comment

by:sarabande
ID: 36585231
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
 

Author Closing Comment

by:dvplayltd
ID: 36585307
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 36585398
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

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question