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

AI Thread Summary
The discussion focuses on evaluating various C programming expressions involving integers and floating-point numbers. Key points include the distinction between integer and floating-point division, which affects the results of expressions like 1 - 2/3 + 4 - 5, yielding different outputs based on whether floats are involved. The order of operations, following PEMDAS, is emphasized, particularly in expressions like 1 + 3/2*4, where integer division alters the outcome. Participants clarify that when both operands are integers, the result is truncated, while introducing a float changes the result to a decimal. Overall, the thread highlights the importance of understanding data types and operations in C programming for accurate evaluations.
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

Back
Top