Download presentation

Presentation is loading. Please wait.

Published bySybil Stevens Modified over 5 years ago

1
Instructor: Alexander Stoytchev http://www.ece.iastate.edu/~alex/classes/2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2
Formatting Numbers CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

3
Administrative Stuff HW3 is out Due on Friday Sep 18 @ 8pm Electronic submission on WebCT HW2 is due this Friday @ 8pm

4
Quick review of the last lecture

5
On-line IEEE 754 Converters http://www.h-schmidt.net/FloatApplet/IEEE754.html http://babbage.cs.qc.edu/IEEE-754/Decimal.html

6
Expressions An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point Addition Subtraction Multiplication Division Remainder +-*/%+-*/% © 2004 Pearson Addison-Wesley. All rights reserved

7
Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals 4 0 14 % 3 equals 8 % 12 equals 2 8 © 2004 Pearson Addison-Wesley. All rights reserved

8
Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order © 2004 Pearson Addison-Wesley. All rights reserved

9
Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123 © 2004 Pearson Addison-Wesley. All rights reserved

10
Expression Trees The evaluation of a particular expression can be shown using an expression tree The operators lower in the tree have higher precedence for that expression a + (b – c) / d a + / -d bc © 2004 Pearson Addison-Wesley. All rights reserved

11
Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432 © 2004 Pearson Addison-Wesley. All rights reserved

12
Effect of sum = sum + item; Figure 2.4. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

13
Effect of scanf("%lf", &miles); Figure 2.5. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14
Evaluation Tree for area = PI * radius * radius; Figure 2.8. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

15
Step-by-Step Expression Evaluation Figure 2.9. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

16
Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1); Figure 2.10. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

17
Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y Figure 2.11. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

18
Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1; © 2004 Pearson Addison-Wesley. All rights reserved

19
Increment and Decrement The increment and decrement operators use only one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; © 2004 Pearson Addison-Wesley. All rights reserved

20
Increment and Decrement The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care © 2004 Pearson Addison-Wesley. All rights reserved

21
Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable C provides assignment operators to simplify that process For example, the statement num += count; is equivalent to num = num + count; © 2004 Pearson Addison-Wesley. All rights reserved

22
Assignment Operators There are many assignment operators in C, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y © 2004 Pearson Addison-Wesley. All rights reserved

23
Assignment Operators The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num); © 2004 Pearson Addison-Wesley. All rights reserved

24
Escape Sequences Some C escape sequences: Escape Sequence \b \t \n \r \a \" \' \\ Meaning backspace tab newline carriage return beep double quote single quote backslash © 2004 Pearson Addison-Wesley. All rights reserved

25
printf() function printf(“format string”, variable1, variable2, …); printf(“For int use %d”, myInteger); printff(“For float use %f”, myFloat); printf(“For double use %lf”, myDouble); printf(“For float or double %g”, myF_or_D); printf(“int=%d double %lf”, myInteger, myDouble);

26
scanf() function scanf(“format string”, &variable1, &variable2, …); scanf(“%d”, &myInteger); scanf(“%f”, &myFloat); scanf(“%lf”, &myDouble); scanf(“%d%f”, &myInteger, &myFloat);

27
Common Bugs Using & in a printf function call. printf(“For int use %d”, &myInteger); // wrong Using the wrong string in printf printf(“This is a float %d”, myFloat); // use %f not %d Not using & in a scanf() function call. scanf(“%d”, myInteger); // Wrong Using the wrong string in scanf() scanf(“%d”, &myFloat); // wrong; use %f instead of %d

28
Why do we need to specify the format of the keyboard input before we can read it?

29
printf() arguments %[flags][width][.precision][length]specifier http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

30
Printing octal, hexadecimal and binary

31
Memory Analogy Address 0 Address 1 Address 2 Address 3 Address 4 Address 5 Address 6

32
Memory Analogy (32 bit architecture) Address 0 Address 4 Address 8 Address 12 Address 16 Address 20 Address 24

33
Memory Analogy (32 bit architecture) Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Hexadecimal Address 0x0A Address 0x0D

34
Storing a Double Address 0x08 Address 0x0C

35
Storing 3.14 3.14 in binary IEEE-754 double precision (64 bits) sign exponent mantissa 0 10000000000 1001000111101011100001010001111010111000010100011111 In hexadecimal this is (hint: groups of four): 0100 0000 0000 1001 0001 1110 1011 1000 0101 0001 1110 1011 1000 0101 0001 1111 4 0 0 9 1 E B 8 5 1 E B 8 5 1 F

36
Storing 3.14 So 3.14 in hexadecimal IEEE-754 is 40091EB851EB851F This is 64 bits. On a 32 bit architecture there are 2 ways to store this Small address: 40091EB8 51EB851F Large address: 51EB851F 40091EB8 Big-EndianLittle-Endian

37
Storing 3.14 Address 0x08 Address 0x0C 40 09 1E B8 51 EB 85 1F Address 0x08 Address 0x0C 40 09 1E B8 51 EB 85 1F Big-Endian Little-Endian

38
Storing 3.14 on a Little-Endian Machine (these are the actual bits that are stored) Address 0x08 Address 0x0C 01010001 11101011 10000101 00011111 01000000 00001001 00011110 10111000 Once again, 3.14 in IEEE-754 double precision is: sign exponent mantissa 0 10000000000 1001000111101011100001010001111010111000010100011111

39
They are stored in binary the hexadecimals are just for visualization Address 0x08 Address 0x0C 4 0 0 9 1 E B 8 5 1 E B 8 5 1 F 01010001 11101011 10000101 00011111 01000000 00001001 00011110 10111000

40
Big-Endian (8-bit architecture) (16-bit architecture) http://en.wikipedia.org/wiki/Endianness

41
Little Endian http://en.wikipedia.org/wiki/Endianness (8-bit architecture) (16-bit architecture)

42
Big-Endian/Little-Endian analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

43
Big-Endian/Little-Endian analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

44
Big-Endian/Little-Endian analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

45
What would be printed? (don’t try this at home) double pi = 3.14; printf(“%d”,pi); Result: 1374389535 Why? 3.14 = 40091EB851EB851F (in double format) Stored on a little-endian 32-bit architecture 51EB851F (1374389535 in decimal) 40091EB8 (1074339512 in decimal)

46
What would be printed? (don’t try this at home) double pi = 3.14; printf(“%d %d”, pi); Result: 1374389535 1074339512 Why? 3.14 = 40091EB851EB851F (in double format) Stored on a little-endian 32-bit architecture 51EB851F (1374389535 in decimal) 40091EB8 (1074339512 in decimal) The second %d uses the extra bytes of pi that were not printed by the first %d

47
What would be printed? (don’t try this at home) double a = 2.0; printf(“%d”,a); Result: 0 Why? 2.0 = 40000000 00000000 (in hex IEEE double format) Stored on a little-endian 32-bit architecture 00000000 (0 in decimal) 40000000 (1073741824 in decimal)

48
What would be printed? (an even more advanced example) int a[2]; // defines an int array a[0]=0; a[1]=0; scanf(“%lf”, &a[0]); // read 64 bits into 32 bits // The user enters 3.14 printf(“%d %d”, a[0], a[1]); Result: 1374389535 1074339512 Why? 3.14 = 40091EB851EB851F (in double format) Stored on a little-endian 32-bit architecture 51EB851F (1374389535 in decimal) 40091EB8 (1074339512 in decimal) The double 3.14 requires 64 bits which are stored in the two consecutive 32-bit integers named a[0] and a[1]

49
Questions?

50
THE END

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google