Formatting numbers to n decimal places without rounding [duplicate] - c++

This question already has answers here:
How to truncate a floating point number after a certain number of decimal places (no rounding)?
(3 answers)
Closed 7 years ago.
I want to create a c++ program to calculate the average of 3 positive numbers
where (x,y,z)>0 and (x,y,z)<=10
I have this code:
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
int main()
{
int x,y,z;
cin >> x >> y >> z;
double x1,y1,z1,ma;
x1 = x;
y1 = y;
z1 = z;
if(x>0 && x<=10 && y>0 && y<=10 && z>0 && z<=10)
ma = (x1+y1+z1)/3;
else
return 0;
printf("%.2f" , ma);
return 0;
}
For x=9, y=9 and z=5 the average is 23/3=7.666666666666667 and when I format to 2 decimal places, the result will be 7.67, but I want to appear 7.66 not 7.67.
Please, can someone help me?
Thank you!

Without using other functions, you could do:
double x = (double)((int)(23 * 100 / 3)) / 100.0;
Or even a bit simpler:
double x = (double)(int)(23 * 100 / 3) / 100.0;
The int cast truncates the remaining digits (so there is no rounding).
In C++11 there is also the trunc() function to do just that.

Related

Number of permutations calculation in c++ [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 6 months ago.
I am learning c++ and having a bit of trouble with my homework.
We have to write code using the double variable type and use two variables to calculate the number of permutations of the potential team arrangements…
The question specifies that there are 18 people in the group and you want to divide the group into teams of 3 members.
This is my current code:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int fact(int n) {
if (n == 0) return 1;
if (n > 0) return n * fact(n - 1);
}
int main()
{
double n = 18.0;
double r = 3.0;
double answer = fact(n) / (fact(r) * fact(n - r));
cout << “The number of arrangements = “ << answer << endl;
system(“pause”);
return 0;
}
When I run the code I am receiving “The number of arrangements = 1”
This is not the correct answer. Can someone please help me figure out what I am doing wrong?
Thanks!
Your fact function is returning an int, not a double.
Since your factorial function is returning an int, fact(n) is an int, and fact(r) * fact(n - r) is also an int.
Together, they will perform integer division, i.e. floor division, not true division.
Edit: Looked at the code again, I realized the problem wasn't integer division, it was the fact that int overflowed, where as double wouldn't overflow until 171!.

Difference between x * 0.1 and x / 10?

I am a high school student and I saw a while ago that is better to multiply than to divide. Since then, without any proof found that it is true or not and without knowing how to do find it by myself at the moment, I tried to modify my codes for that slightly better time.
Here is a problem where I wanted to find the biggest digit in a number using recursion.
This one is working.
#include <iostream>
using namespace std;
int minn = 9;
int digit(int n)
{
if(minn > n % 10)
minn = n % 10;
if(!(n / 10))
return minn;
else
return digit(n / 10);
}
int main()
{
int x;
cin >> x;
cout << digit(x);
return 0;
}
But this is not working.
#include <iostream>
using namespace std;
int minn = 9;
int digit(int n)
{
if(minn > n % 10)
minn = n % 10;
if(!(n * 0.1))
return minn;
else
return digit(n / 10);
}
int main()
{
int x;
cin >> x;
cout << digit(x);
return 0;
}
The only difference is that the broken one use if(!(n * 0.1)) not if(!(n / 10)).
Can someone clarify it for me or anyone who is seeking help what is the difference between x * 0.1 and x / 10?
Thank you all for clicking the question and that you tried to help!
0.1 is a double type, 10 is an integer type.
When dividing two integers e.g. n / 10 you will get integer division (e.g. 6/10 will equal 0).
And your check will work differently when using 6 * 0.1 as that will equal 0.6.
n * .1 results in a floating point result. So, 3 * .1 produces the result of .3, which is definitely not 0.
On the other hand, 3/10 is 0. That's how integer division works in C++.
You define x as int, x*0.1 uses float arithmetics, while x/10 uses integer arithmetics
Can someone clarify it for me or anyone who is seeking help what is the difference between x * 0.1 and x / 10?
Difference is very significant. In the first case you use floating point multiplication in the second integer division. So first if() would happen when n == 0 while second when n < 10.
if( !(n*0.1) ) // for if to work result of n * 0.1 must be equal 0 which only happens when n == 0
if( !(n/10) ) // for if to work result of n / 10 must be equal to 0 which happens when abs(n) < 10 (including 0)

Trying to calculate Pi using a for loop only gives 3 [duplicate]

This question already has an answer here:
Dividing two integers to produce a float result [duplicate]
(1 answer)
Closed 6 years ago.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float pi =0;
bool add = true;
for (int i =1; i < 30000; i+=2) {
if (add) {
pi = pi + (4/i);
add = false;
} else {
pi = pi - (4 / i);
add = true;
}
}
cout << setprecision(18);
cout << pi;
return 0;
}
However the output i just 3! All the time.... Why so? What's wrong in my logic?
Is it some wrong in the code or just the Leibniz Series is not on good terms with computers?
pi = pi + (4/i);
Please write pi = pi + 4.0 / i; instead. Integer divided by integer is integer division which won't yield floating point result.

c++ why does ((1 / 2) * 2) return 0 [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Integer division always zero [duplicate]
(1 answer)
Closed 7 years ago.
first time I'v posted here but I must know what's wrong with this simple peace of code:
#include <iostream>
using namespace std;
int main()
{
double test = (1 / 2) * 2;
cout << test << endl;
return 0;
}
when ever I run this the code it displays 0, should I be casting something, it happens regardless of what compiler I use and it returns even stranger results if the '1' is divided be some form of decimal.
Because in integer maths 1 / 2 == 0 and 0 * 2 == 0.
Try with 1.0 and 2.0 instead.
In (1 / 2) both the 1 and 2 are integers which means that the result is also an integer. This means the expression returns 0. 0 * 2 is 0.
To get the result you want, try (1.0 / 2.0)
If you want to get right result you need to write:
#include <iostream>
using namespace std;
int main()
{
double test = ((double)1 / 2) * 2;
cout << test << endl;
return 0;
}
You're using int instead of double.
Fixing...
#include <iostream>
using namespace std;
int main()
{
double test = (1.0 / 2.0) * 2.0;
cout << test << endl;
return 0;
}

Conversion from double to integer [duplicate]

This question already has answers here:
C++: How to round a double to an int? [duplicate]
(5 answers)
round() for float in C++
(23 answers)
Closed 8 years ago.
I am stuck in problem where the double number is not getting properly converted to integer.
In this case->
int x=1000;
double cuberoot=pow(x,(1/(double)3));
int a=cuberoot;
cout<<"cuberoot="<<cuberoot<<endl;
cout<<"a="<<a<<endl;
Output:
cuberoot=10
a=9
Why here a=9 and not 10?
Any solution to this problem??
Also I don't want to round the value..if a=3.67 then it should be converted to 3 only
and not 4.
Because the cuberoot is very close to 10 but not quite 10. std::cout truncates and rounds the number to 10, but a double to integer conversion will strip the decimal which is why a = 9. To solve this problem, you can use std::round():
int a=round(cuberoot);
Try this and see why!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
int x = 1000;
double cube = pow(x, 1.0/3.0);
int a = cube;
cout<<"cube="<< fixed << setprecision(16) << cube<<endl;
cout<<"a="<<a<<endl;
}