Calculation in C++: B-C && (B-A) /C - Which Answer is Right?

In summary, the conversation discusses a confusion regarding a calculation problem in C++. The question involves the use of variables A, B, and C in an expression using arithmetic and logical operators. One person believes the answer is 0 due to the first part being negative, while their friend argues that -1 doesn't necessarily mean false. However, a further explanation is given, showing that the actual result is false (0) due to the order of operations and implicit conversion of ints to bools in C++. The conversation ends with the suggestion to test the expression in a C++ compiler to confirm the answer.
  • #1
ccky
15
0
I am doing my homework regarding calculation in c++.
I am confused by the below question.
Int A=2 B=3 C=4
B-C && (B-A) /C
I think the answer is 0 because the first part is negative.
But,my friends say the answer is 1 because -1 not mean false.
I want to ask which answer is right?
Thanks.
 
Technology news on Phys.org
  • #2
Your friend is right - -1 doesn't mean false. 0 and only 0 means false.
 
  • Like
Likes 1 person
  • #3
You and your friend are both incorrect. You could easily check this by running it through a C++ compiler.

In C++, there is implicit conversion from ints to bools. && has pretty low precedence, so first the following expressions are evaluated: B - C is 3 - 4 = -1, and (B - A) / C is 1 / 4 = 0 (in integer division).
So the resulting expression is -1 && 0 which is equivalent to true && false, hence the result you get is false (0).
 
Last edited:
  • Like
Likes 1 person
  • #4
Ah, I referred just to the small part of the post, I have not actually bothered to evaluate whole expression :shy:
 
  • #5


I would first clarify the question and make sure all variables are defined and the desired outcome is clear. In this case, it seems like the calculation is asking for the result of the expression (B-C && (B-A)/C) where A=2, B=3, and C=4.

In C++, the logical operator && represents "and" and will return a boolean value of true or false. The expression (B-C) evaluates to -1, which is considered true in C++. The second part of the expression (B-A)/C evaluates to 0.25, which is also considered true in C++. Therefore, the overall expression will evaluate to true (1).

However, it is important to note that the answer will vary depending on the programming language and its specific rules for evaluating logical expressions. It is also possible that the question is asking for a different outcome, in which case the answer may be different. It is always important to fully understand the question and its context before providing an answer.
 

1. What is the purpose of the calculation in C++?

The purpose of the calculation in C++ is to perform mathematical operations and solve equations within a C++ program. This allows for complex calculations to be completed efficiently and accurately.

2. What does the "B-C && (B-A) /C" part of the calculation mean?

The "B-C" portion of the calculation represents subtracting the value of variable C from variable B. The "&&" operator is a logical AND operation, meaning both conditions on either side of the operator must be true for the overall expression to be true. The "(B-A) /C" portion represents first subtracting the value of variable A from variable B, and then dividing that result by the value of variable C.

3. How do I know which answer is right?

In order to determine which answer is right, you will need to know the specific values of variables B, A, and C. Then, you can plug those values into the calculation and solve for the result. It is important to use proper order of operations when solving the equation to ensure an accurate answer.

4. Can I use other mathematical operators in C++ calculations?

Yes, there are several mathematical operators available in C++ for performing calculations, including addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators can be combined and used in various ways to solve equations and perform calculations.

5. How does C++ handle errors in calculations?

C++ has a built-in error handling mechanism that will throw an error if a calculation results in an overflow, underflow, or division by zero. It is important for programmers to anticipate and handle these errors in their code to prevent unexpected results or crashes.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
769
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
2
Views
369
Back
Top