Arithmetic expression is 0 instead of 1 [duplicate] - c++

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
cout << a/b*c;
return 0;
}
For a=5 b=10 c=2 the output gives 0 which is obviously wrong.
As far as I understood << / * are binary operators / and * "bond" stronger than << and for / and * it is calculated from the left to right so first 5 is devided by 10, then the result (0.5) is multiplicated with 2 which is 1 and that is delivered by << to cout.
So can anyone give me an explanation for that result (0)?

This is the integer division issue: if a==5 and b==10, then a/b==0.
See the standard, Multiplicative operators [expr.mul]
For
integral operands the / operator yields the algebraic quotient with any fractional part discarded

This is the result of integer rounding. When integer division is performed any fractional remainder is simply cut off. So 2 / 3 == 0.
To keep the results as accurate convert use doubles or convert the variables to doubles in the expression. So something like this
static_cast<int>(static_cast<double>(a) / static_cast<double>(b) * static_cast<double>(c))
will result in the correct value.

No its not wrong
Since all variables are integers.
5/10 is 0 in the integer world and that multiplied with 2 is still 0
9/10 is also 0 but 11/10 is 1
if you want something useful from this declare the variables as double instead or IOW:
int main() {
double a,b,c;
cin >> a >> b >> c;
cout << a/b*c;
return 0;
}

Related

setprecision(2) value not working in if statement even when the conditions are true [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Division of two numbers always returns an integer value
(3 answers)
Closed 3 months ago.
I dont understand why setprecision(2) not working when using if else statement
I tried doing this and it displays the else statement. I dont see any problem, maybe im using setprecision() wrong? I even displayed the quotient to prove that the if statement should be the one running.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float x = 2;
float y = 3;
float quotient, answer;
quotient = x / y;
cout << fixed << setprecision(2);
cout << quotient << " (is the answer)\n";
cout << " What is " << x << " divided by " << y << " ? ";
cin >> answer; // answer should be 0.67
if (quotient == answer)
cout << " You got the right answer! ";
else
cout << " Nice Try :( ";
return 0;
}
The line
quotient = x / y;
will assign the value of 0.0 to the variable quotient, because 2/3 is 0, using the rules of integer division. Therefore, if the user enters 0.67, this value will not compare equal to 0.0.
If you want the division 2/3 to evaluate to something like 0.6666666667, then you must make at least one of the operands a floating-point number, for example by using a cast:
quotient = static_cast<float>(x) / y;
However, even if you did this, your comparison would still not work, because the expression
cout << fixed << setprecision(2) << quotient;
will only change the way the variable quotient is printed. It will not change the actual value of the variable.
In order to round the actual value of the variable, you can use the function std::round. Note that this will only round to the nearest integer, so if you want to round to the nearest multiple of 0.01, then you will first have to multiply the number by 100 before performing the rounding operation. If you want, you can then divide the number by 100 again, to get the original number again, rounded to the nearest multiple of 0.01.
However, you should be aware that these operations may introduce slight floating-point inaccuracies. For this reason, it may be better to not require an exact match in the comparison
if (quotient == answer)
but to consider a deviation of up to 0.01 to still be considered a match. You can do this for example by changing the expression to this:
if ( std::abs( quotient - answer ) < 0.01 )
Note that you will have to #include <cmath> in order to use std::round and std::abs.

How to calculate a/b in C++? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I am trying to get value for a/b, but I always get '0' for a/b.
Why do I get a/b=0 ?
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout << "Give a and b" << endl;
cin >> a >> b;
double q=a/b;
cout << "a/b=" << q;
return 0;
}
You are using integer arithmetic, so the division result will be an integer. A floating point value that is between 0 and 1 will get truncated to 0 when interpretted as an integer, before it is assigned to your q variable. So either:
change your a and b variables to double instead of int
typecast a and/or b to double during the division.
Either way, you will then be performing floating-point division instead of integer division, so the result will be a floating-point value.
Can do double q=double(a)/b; instead.

c++: what does this insertion expression do and how does it work when passing as a function argument?

What's the meaning of binary(number >> 1), and how does it work in the following code? Could somebody explain it to me in detail? Thank you!
#include <iostream.h>
void binary(int);
int main() {
int number = 3;
cout << number << endl;
binary(number);
}
void binary(int number) {
if(number <= 1) {
cout << number;
return;
}
int remainder = number%2;
binary(number >> 1); //How does this work exactly?
cout << remainder;
}
The << and >> operators are bit-shift operators; they change the value based off of the binary representation of the number; an example will clarify:
001010 (10)
If we do << 1 (left shift 1 bit), then we get:
010100 (20)
If you noticed, the above is equivalent to multiplying by two; in fact, shifting left n bits is equivalent to multiplying by 2 to the nth power!
If we do >> 1 (right shift 1 bit) to the original, we get this:
000101 (5)
Again, if you look carefully, you'll notice the above is equivalent to dividing by 2! In fact, the right shift operator is the inverse operator of the left shift operator, so right shifting n bits is equivalent to dividing by 2 to the nth power!
Also, void main() is just plain wrong, so don't use it. <iostream.h> should be replaced by <iostream> as the former was used prior to standard ISO C++.

C++ Simple Farenheit Conversion

I am trying to create a simple program that takes a value Celsius entered by the user, and converts it to Fahrenheit.
#include <iostream>
using namespace std;
int main()
{
float celsius, farenheit;
cout << "Enter a temperature in farenheit ";
cin >> farenheit;
celsius = (farenheit - 32) * (5 / 9);
cout << celsius;
return 0;
}
It always prints a value of 0, why is this?
This is because you multiply by 5 / 9, which is zero due to integer division.
If you change parentheses like this
celsius = ((farenheit - 32) * 5) / 9;
you would get the correct result.
Since you parenthesized (5 / 9), the compiler computed that value before performing multiplication. Since (5 / 9) is an integer expression, the compiler performs an integer division, meaning that the fractional part is discarded. As the result, all proper fractions become zeros.
Another way to fix this problem is to force C++ to use floating point division by making one or both numbers a floating point constant (specifically, a constant of type double) by appending .0 to them, for example:
celsius = (farenheit - 32) * (5.0 / 9);

C++ What does the percentage sign mean?

I got this c++ macro and wonder what they mean by code%2 (the percentage sign) ?
#define SHUFFLE_STATEMENT_2(code, A, B)
switch (code%2)
{
case 0 : A; B; break;
case 1 : B; A; break;
}
It is for taking a modulus.
Basically, it is an integer representation of the remainder.
So, if you divide by 2 you will have either 0 or 1 as a remainder.
This is a nice way to loop through numbers and if you want the even rows to be one color and the odd rows to be another, modulus 2 works well for an arbitrary number of rows.
In case somebody happens to care: % really returns the remainder, not the modulus. As long as the numbers are positive, there's no difference.
For negative numbers there can be a difference though. For example, -3/2 can give two possible answers: -1 with a remainder of -1, or -2 with a remainder of 1. At least as it's normally used in modular arithmetic, the modulus is always positive, so the first result does not correspond to a modulus.
C89/90 and C++98/03 allow either answer though, as long as / and % produce answers that work together so you can reproduce the input (i.e. -1x2+-1->-3, -2x2+1=-3).
For newer versions of the standards (C99, C11 and C++11) there's no longer any choice: integer division must round toward 0. For example -3/2 must give -1 with a remainder of -1. -3/2 giving -2 with a remainder of 1 is no longer allowed.
It means the remainder of a division. In your case, divide by 2 and the remainder will be either 0 or 1.
It means modulo. Usually (x % 2) discriminates odd and even numbers.
Thats the modulo. It returns whats left after division:
10/3 will give 3. - 1 is left.
10%3 gives this 1.
Modulo returns the remainder that is left after division. It is helpful when you're tasked with determining even / odd / prime numbers as an example:
Here's an example of using it to find prime numbers:
int main(void)
{
int isPrime=1;
int n;
cout << "Enter n: ";
cin >> n;
for (int i=1; i<=n; i++)
{
for (int j=2; j <= sqrt(static_cast<double>(i)); j++)
{
if(!(i%j))
{
isPrime=0;
break;
}
}
if (isPrime)
cout << i << " is prime" << endl;
isPrime=1;
}
return 0;
}
It's the remainder of division.
So like how 5 divided by 2 has a remainder of 1 because 2 goes into 5 2 times but that only equals four, and you got that little extra on the end
5 % 2 == 1
Note that division value isn't being calculated anywhere, so if you wanted both the whole total integer value of the division and it's remainder
int answer = 5 / 2;
int remainder = 5 % 2;
cout << "5 divided by 2 is " << answer << " with remainder " << remainder;
"5 divided by 2 is 2 with remainder 1"