Evaluate C Programming Questions: 1-2/3+4-5, 1-2/3+4.0-5 etc.

Click For Summary

Discussion Overview

The discussion focuses on evaluating various C programming expressions involving arithmetic operations and character arithmetic. Participants explore the differences in results based on integer and floating-point arithmetic, as well as the order of operations in C.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that in expression (i), all numbers are integers, leading to integer division where 2/3 results in 0.
  • Others argue that in expression (ii), the presence of 4.0 introduces floating-point arithmetic, resulting in a final answer of 0.0 instead of 0.
  • There is uncertainty expressed regarding expression (iii), with some participants suggesting that it involves floating-point division due to the presence of 2/3.0.
  • For expressions (iv) and (v), participants discuss the application of the order of operations (PEMDAS) and how integer division affects the results, particularly in (v) where 3/2 is evaluated as 1.
  • One participant explains that character arithmetic in expression (vi) involves converting characters to their ASCII values.
  • Some participants express confusion about the results of expressions (v) and (iii), seeking clarification on the order of operations and the impact of integer versus floating-point division.

Areas of Agreement / Disagreement

Participants generally agree on the application of PEMDAS but have differing interpretations of how integer and floating-point arithmetic affect the results, particularly in expressions (iii) and (v). The discussion remains unresolved regarding the exact outcomes of these expressions.

Contextual Notes

Limitations include potential misunderstandings about the significance of floating-point versus integer division and the handling of significant digits in C programming.

Who May Find This Useful

Students learning C programming, particularly those struggling with arithmetic operations and order of operations in expressions.

Maybe_Memorie
Messages
346
Reaction score
0

Homework Statement



Evaluate the following expressions

(i) 1 - 2/3 + 4 - 5

(ii) 1 - 2/3 + 4.0 - 5

(iii) 1 - 2/3.0 + 4 - 5

(iv) 1 + 4*3/2

(v) 1 + 3/2*4

(vi) 'h' - 'e' + 'l' - 'p'

The Attempt at a Solution



The answers for each question respectively are
0
0.0
-.666667
7
5
-1

I am completely lost here. I have no idea what the differences between the first 3 are or the 4th and 5th. There is nothing about this in my notes.

Any advice is greatly appreciated.
 
Physics news on Phys.org
Maybe_Memorie said:

Homework Statement



Evaluate the following expressions

(i) 1 - 2/3 + 4 - 5

(ii) 1 - 2/3 + 4.0 - 5

(iii) 1 - 2/3.0 + 4 - 5

(iv) 1 + 4*3/2

(v) 1 + 3/2*4

(vi) 'h' - 'e' + 'l' - 'p'

The Attempt at a Solution



The answers for each question respectively are
0
0.0
-.666667
7
5
-1

i) All of the numbers in the expression are integers. Since this is so, we confine the result of each calculation to be an integer. 2/3 = 0 by chopping off the decimal.

ii) Since a 4.0 is introduced, you are now working with a float that has two significant figures. After combining the terms, you'll end up with 0.0 instead of 0.

iii) This one I'm not quite sure on. You're performing a division on a float, so after dividing out, you'll be left with a float. I'm not quite sure how many decimal places you keep though.

iv,v) PEMDAS. Perform multiplication/division from left to right. Then addition. Since we're working with all integers again, you should see the answers.

vi) Convert each character to its appropriate ASCII code.
 
Last edited:
Thanks, I'm still not quite sure on (v) and (iii)
 
On (v), although I agree the order of operations is PEMDAS, I would think the division in this case is happening before the multiplication because 3/2 = 1 (integer division), so it would yield 1+1*4 = 5.
 
Hi Maybe_Memorie! :smile:

Have you moved on to C programming?
Maybe_Memorie said:
Thanks, I'm still not quite sure on (v) and (iii)
Maybe_Memorie said:
(iii) 1 - 2/3.0 + 4 - 5

First C evaluates multiplications and divisions from left to right.
In this case there is only 1 division.

If the arguments of a division are both integers, the result is a truncated integer division.
If either argument (or both) have a point in them (that is, they are "floating point"), the result is floating point.
In this case 2/3.0 = 0.666666666666667
(Usually you will always have 15 significant digits, so gb7nash's remark is off.)

Then C evaluates all additions and subtractions from left to right.

So:
1 - 2/3.0 + 4 - 5
= 1 - 0.666666666666667 + 4 - 5
= 0.333333333333333 + 4 - 5
= 4.33333333333333 - 5
= -0.66666666666667
Maybe_Memorie said:
(v) 1 + 3/2*4

In this case we're looking at integer division, and we evaluate mul together with div left to right.
1 + 3/2*4
= 1 + 1*4
= 1 + 4

And then additions and subtractions left to right:
1 + 4 = 5
 
Last edited:
timthereaper said:
On (v), although I agree the order of operations is PEMDAS, I would think the division in this case is happening before the multiplication because 3/2 = 1 (integer division), so it would yield 1+1*4 = 5.

It does. In PEMDAS, you do multiplication/division left to right. The division sign occurs before the multiplication sign like you said, so you would end up getting 1+1*4 = 5

I like Serena said:
(Usually you will always have 15 significant digits, so gb7nash's remark is off.)

I learned something new today.
 
Hey Maybe_Memorie! :smile:

Are you satisfied with the answers?
(I always like to feel a thread was finished satisfactorily before moving on to new problems. :wink:)
 
Thank you very much! :)

(Sorry for taking so long to reply, I don't always have internet access :redface: )
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K