Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c++ using the ceil a division is not working?

Tags:

c++

int

math

well i tried this it don't work with ceil in c++

int n, m, a;
double r1, r2;
cin >> n;
cin >> m;
cin >> a;
r1 = (n/a);
r2 = (m/a);
cout << (ceil(r1)*ceil(r2));

the n/a operation shall return a double in case of n = 3 and a =2 but it doesn't

also..

the m/a operation shall return a double in case of m = 3 and a =2 but it doesn't

like image 341
sgiri Avatar asked Jan 07 '23 07:01

sgiri


1 Answers

Here is the problem, n/a and m/a will evaluate to integers since n,m,a are integers and the integer will get stored into double , to avoid this you can type cast like this

r1=(double)n/a;
r2=(double)n/a;

Or simply change the data type of a from int to double, maybe (It will help you to #A. Theatre Square)

like image 98
Gagan Avatar answered Jan 19 '23 00:01

Gagan