Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Similar presentations


Presentation on theme: "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"— Presentation transcript:

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

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

3 Administrative Stuff Hw2 is out Part I is Due on Monday Sep 15 @ 10 am  Paper submission before the start of class. Part II is Due on Monday Sep 15 @ 8pm.  Electronic submission on WebCT

4 Quick review of the last lecture

5 Variables A variable is a name for a location in memory A variable must be declared by specifying the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name © 2004 Pearson Addison-Wesley. All rights reserved

6 Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type The expression on the right is evaluated and the result is stored in the variable on the left © 2004 Pearson Addison-Wesley. All rights reserved

7 Assignment Through a Function y = f(x); Q = sin(30); The assignment operator is still the = sign

8 Assignment Through scanf() int variable; scanf(“%d”, &variable); 30 There is not assignment operator in this case

9 Some Primitive Data Types char short int long float double long double

10 float and double analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

11 float and double analogy float double [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

12 Numeric Primitive Data The difference between the various numeric primitive types is their size, and therefore the values they can store: Type char short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 10 18 +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 10 18 © 2004 Pearson Addison-Wesley. All rights reserved

13 Computer Memory Main memory is divided into many memory locations (or cells) 9278 9279 9280 9281 9282 9283 9284 9285 9286 Each memory cell has a numeric address, which uniquely identifies it

14 Storing Information 9278 9279 9280 9281 9282 9283 9284 9285 9286 Large values are stored in consecutive memory locations10011010 Each memory cell stores a set number of bits (usually 8 bits, or one byte)

15 Storing a char 9278 9279 9280 9281 9282 9283 9284 9285 9286 char (8 bits = 1 byte)

16 Storing a short 9278 9279 9280 9281 9282 9283 9284 9285 9286 short (16 bits = 2 bytes)

17 Storing an int 9278 9279 9280 9281 9282 9283 9284 9285 9286 int (32 bits = 4 bytes)

18 Storing a long 9278 9279 9280 9281 9282 9283 9284 9285 9286 long (64 bits = 8 bytes)

19 Storing a float 9278 9279 9280 9281 9282 9283 9284 9285 9286 float (32 bits = 4 bytes)

20 Storing a double 9278 9279 9280 9281 9282 9283 9284 9285 9286 double (64 bits = 8 bytes)

21 The sizeof() operator #include int main(int argc, char *argv[]) { printf("Size of char = %d \n", sizeof(char)); printf("Size of short = %d \n", sizeof(short)); printf("Size of int = %d \n", sizeof(int)); printf("Size of long = %d \n", sizeof(long)); printf("Size of long long = %d \n", sizeof(long long)); printf("Size of float = %d \n", sizeof(float)); printf("Size of double = %d \n", sizeof(double)); printf("Size of long double = %d \n", sizeof(long double)); system("PAUSE"); return 0; }

22 Binary Numbers Once information is digitized, it is represented and stored in memory using the binary number system A single binary digit (0 or 1) is called a bit Devices that store and move information are cheaper and more reliable if they have to represent only two states A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) Permutations of bits are used to store values

23 Bit Permutations 1 bit 0101 2 bits 00 01 10 11 3 bits 000 001 010 011 100 101 110 111 4 bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Each additional bit doubles the number of possible permutations © 2004 Pearson Addison-Wesley. All rights reserved

24 Bit Permutations Each permutation can represent a particular item There are 2 N permutations of N bits Therefore, N bits are needed to represent 2 N unique items 2 1 = 2 items 2 2 = 4 items 2 3 = 8 items 2 4 = 16 items 2 5 = 32 items 1 bit ? 2 bits ? 3 bits ? 4 bits ? 5 bits ? How many items can be represented by © 2004 Pearson Addison-Wesley. All rights reserved

25 Relationship Between a Byte and a Bit [Figure 1.5in the textbook]

26 What is the value of this binary number? 0 0 1 0 1 1 0 0 0*2 7 + 0*2 6 + 1*2 5 + 0*2 4 + 1*2 3 + 1*2 2 + 0*2 1 + 0*2 0 0*128 + 0*64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 32+ 8 + 4 = 44 (in decimal)

27 What is the maximum number that can be stored in one byte (8 bits)?

28 1 1 1 1 1 1 1 1 1 1 1 1 1*2 7 + 1*2 6 + 1*2 5 + 1*2 4 + 1*2 3 + 1*2 2 + 1*2 1 + 1*2 0 1*128 + 1*64 + 1*32 + 1*16 + 1*8 + 1*4 + 1*2 + 1*1 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 (in decimal) Another way is: 1*2 8 – 1 = 256 – 1 = 255

29 What would happen if we try to add 1 to the largest number that can be stored in one byte (8 bits)? 1 1 1 1 1 1 1 1 + 1 ------------------------------- 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

30 Analogy with car odometers

31 [http://www.hyperocity.com/volvo240/images/Volvo/odometerrepair/speedo999999.jpg]

32 Signed integers are more complicated

33 The story with floats is more complicated IEEE 754-1985 Standard [http://en.wikipedia.org/wiki/IEEE_754]

34 In the example shown above, the sign is zero so s is +1, the exponent is 124 so e is −3, and the significand m is 1.01 (in binary, which is 1.25 in decimal). The represented number is therefore +1.25 × 2 −3, which is +0.15625. [http://en.wikipedia.org/wiki/IEEE_754]

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

36 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string literal." "123 Main Street" "X" © 2004 Pearson Addison-Wesley. All rights reserved

37 Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters © 2004 Pearson Addison-Wesley. All rights reserved

38 Characters The ASCII character set is older and smaller than Unicode, but is still quite popular The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab,... © 2004 Pearson Addison-Wesley. All rights reserved

39 ASCII Table

40 Extended ASCII Codes

41 The Unicode Character Code http://www.unicode.org/charts/

42 Escape Sequences What if we wanted to print a the quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string printf ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ( \ ) printf ("I said \"Hello\" to you."); © 2004 Pearson Addison-Wesley. All rights reserved

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

44 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);

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

46 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

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

48 Evaluating Expressions

49 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

50 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

51 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

52 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

53 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

54 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

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

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

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

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

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

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

61 Questions?

62 THE END


Download ppt "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"

Similar presentations


Ads by Google