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

In summary, all of the expressions evaluate to 0 except for (vi) which evaluates to -1. The first three expressions all result in integers, while the fourth and fifth expressions result in floats. 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.
  • #1
Maybe_Memorie
353
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
  • #2
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:
  • #3
Thanks, I'm still not quite sure on (v) and (iii)
 
  • #4
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.
 
  • #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:
  • #6
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.
 
  • #7
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:)
 
  • #8
Thank you very much! :)

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

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

1. How do you evaluate C programming expressions?

C programming expressions are evaluated using the operator precedence and associativity rules. This means that operators with higher precedence are evaluated first, followed by those with lower precedence. If operators have the same precedence, their evaluation order is determined by their associativity (left to right or right to left).

2. What is the order of evaluation for the expression 1-2/3+4-5?

The order of evaluation for this expression would be: 1-2 = -1, -1/3 = -0.333, -0.333+4 = 3.667, 3.667-5 = -1.333. Therefore, the final result would be -1.333.

3. How does the use of parentheses affect the evaluation of an expression?

Parentheses can be used to change the default order of evaluation in an expression. Any expression within parentheses is evaluated first before any other operations are performed. This allows for more control over the order of operations in an expression.

4. What is the difference between using integers and floating point numbers in an expression?

Integers and floating point numbers are two different data types in C programming. Integers are whole numbers, while floating point numbers (also known as float or double) are numbers with decimals. When performing calculations, integers will only return whole numbers, while floating point numbers will return more precise results that include decimals.

5. What happens if an expression contains both integers and floating point numbers?

In this case, the result of the expression will be a floating point number. This is because C programming follows the promote and return rule, which means that when an integer and a floating point number are used in the same expression, the integer will be converted to a floating point number and the result will also be a floating point number.

Similar threads

Replies
3
Views
1K
Replies
4
Views
651
Replies
23
Views
2K
Replies
3
Views
1K
Replies
1
Views
878
Back
Top