Please give your suggestions to this code

Status
Not open for further replies.

mahaju

Full Member level 2
Joined
Mar 17, 2007
Messages
125
Helped
7
Reputation
14
Reaction score
2
Trophy points
1,298
Activity points
2,252
#include<iostream.h>
void main()
{
int a,b,q,r;
cout<<"Enter a ";
cin>>a;
cout<<"Enter b ";
cin>>b;
q=a/b;
r=a-q*b;
cout<<"Remender="<<r;
}

Hi
How good is this method in terms of precision? Is this method applicable to all cases? Can I use this code as a function for the replacement of the % operator?
 

Hello,
I think you could be able to replace % (but why???) with this function, but if you want a function working in all cases, you need to use the biggest integer type your compiler support (such as long long int) - unless you are absolutely sure your operands are never bigger than ints.
When you use %, the compiler decides the operation to perform according to operands' size. With your function, the compiler always recast to <int> and this can be a waste of memory and CPU cycles.
Stick to %, if you can
 
Reactions: mahaju

    mahaju

    Points: 2
    Helpful Answer Positive Rating
The code does not take into consideration negative numbers...when either or both a,b are negative.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…