School of Engineering and Applied Science
Department of Computer Science
CSci 49 -- Introduction to C Computing
http://www.seas.gwu.edu/~csci49/spring06
Prof. Michael B. Feldman
mfeldman@gwu.edu

Lab Exercise #2
for lab meeting Wednesday, Feb. 1, 2006

Objectives: This exercise will help you explore integer and float arithmetic, and operator precedence. 

Part I. Integer and float arithmetic.

Given these variable declarations:

int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
float fResult, val1 = 17.0, val2 = 12.78;

For each of these assignment statements, what will the result be in iResult or fResult? First compute the results manually and fill in the "predicted results" column of the table. Then, use the program programs49/Arithmetic.c as a way to test your predictions and fill in the "actual results" column. Use the fourth column for any comments you may have. Make sure you understand why each actual result is what it is!
 
Assignment Statement Predicted Result Actual Result Explanatory Comments 
iResult = num1 / num4;      
fResult = num1 / num4;      
iResult = num3 / num4;      
fResult = num3 / num4;      
fResult = val1 / num4;      
fResult = val1 / val2;      
iResult = num1 / num2;      
fResult = (float) num1 / num2;      
fResult = num1 / (float) num2;      
fResult = (float) (num1 / num2);      
iResult = (int) (val1 / num4);      
fResult = (int) (val1 / num4);      
fResult = (int) ((float) num1 / num2);      
iResult = num3 % num4;      
iResult = num 2 % num3;      
iResult = num3 % num2;      
iResult = num2 % num4;      

 

Part II: Operator precedence.

Now do the same with these assignment statements, given the declarations:

int iResult, a = 10, b = 8, c = 6, d = 5, e =2;

and the program programs49/Precedence.c.
Assignment Statement Predicted Result Actual Result Explanatory Comments
iResult = a - b - c - d;      
iResult = a - b + c - d;      
iResult = a + b / c / d;      
iResult = a + b / c * d;      
iResult = a / b * c * d;      
iResult = a % b / c * d;      
iResult = a % b % c % d;      
iResult = a - (b - c) - d;      
iResult = (a - (b - c)) - d;      
iResult = a - ((b - c) - d);      
iResult = a % (b % c) * d * e;      
iResult = a + (b - c) * d - e;      
iResult = (a + b) * c + d * e;      
iResult = (a + b) * (c / d) % e;      

(end of lab)