Presentation is loading. Please wait.

Presentation is loading. Please wait.

CprE 185: Intro to Problem Solving (using C)

Similar presentations


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

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

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 14 @ 3pm
Paper submission before the start of class. Part II is Due on Friday Sep 8pm. Electronic submission on WebCT

4 Administrative Stuff No class next Monday (Labor Day)

5 Quick review of the last lecture

6 Multiple variables can be created in one declaration
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 data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration © 2004 Pearson Addison-Wesley. All rights reserved

7 Assignment An assignment statement changes the value of a variable
The assignment operator is the = sign total = 55; The expression on the right is evaluated and the result is stored in the variable on the left 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 © 2004 Pearson Addison-Wesley. All rights reserved

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

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

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

11 float and double analogy
[image fom

12 float and double analogy
[image fom

13 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 Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 © 2004 Pearson Addison-Wesley. All rights reserved

14 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

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

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

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

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

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

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

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

22 The sizeof() operator #include <stdio.h>
#include <stdlib.h> 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; }

23 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

24 Bit Permutations 1 bit 1 2 bits 00 01 10 11 3 bits 000 001 010 011 100
1 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

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

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

27 What is the value of this binary number?
0* *26 + 1* *24 + 1*23 + 1*22 + 0*21 + 0*20 0* *64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 = 44 (in decimal)

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

29 What is the maximum number that can be stored in one byte (8 bits)?
1* *26 + 1* *24 + 1*23 + 1*22 + 1*21 + 1*20 1* *64 + 1*32 + 1*16 + 1*8 + 1*4 + 1*2 + 1*1 = 255 (in decimal) Another way is: 1*28 – 1 = 256 – 1 = 255

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

31 Analogy with car odometers

32 Analogy with car odometers
[

33 Signed integers are more complicated

34 The story with floats is more complicated IEEE 754-1985 Standard
[

35 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 × 2−3, which is [

36 On-line IEEE 754 Converters

37 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

38 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

39 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

40 ASCII Table

41 Extended ASCII Codes

42 The Unicode Character Code

43 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

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

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

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

47 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

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

49 Evaluating Expressions

50 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: Addition Subtraction Multiplication Division Remainder + - * / % If either or both operands used by an arithmetic operator are floating point, then the result is a floating point © 2004 Pearson Addison-Wesley. All rights reserved

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

52 result = total + count / max - offset;
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

53 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 © 2004 Pearson Addison-Wesley. All rights reserved

54 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 + / - d b c a + (b – c) / d © 2004 Pearson Addison-Wesley. All rights reserved

55 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 answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side © 2004 Pearson Addison-Wesley. All rights reserved

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

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

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

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

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

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

62 Questions?

63 THE END


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

Similar presentations


Ads by Google