Solved

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

Posted on 2011-09-22
11
576 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

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 32

Expert Comment

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

should be

so 10630/40. is 265.75
0
 
LVL 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now