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.ece.iastate.edu/~alexs/classes/2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2 Variables and Assignment CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

3 Administrative Stuff Labs TA Office hours WebCT page is up How to submit homework 1 Hw2 is out

4 Labs Section E: Tuesdays, 12:10 - 2:00pm (Coover, room 2018) Section F: Wednesdays, 2:10 - 4:00pm (Coover, room 2018) Section G: Tuesdays, 2:10 - 4:00pm (Coover, room 2018) Section J: Wednesdays, 4:10 - 6:00pm (Coover, room 2018) Section L: Tuesdays, 10:00 - 12:00am (Coover, room 2018)

5 WebCT page We’ll use it to submit homeworks The official class web page is still:  http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/ Anonymous course feedback

6 Go to WebCT -> Discussions It is truly anonymous It helps me improve this class. Instantaneous feedback is more helpful than feedback at the end of the semester.

7 How to submit HW 1 (demo) http://www.ece.iastate.edu/~alexs/classes/ 2009_Fall_185/HomeworkSubmission/

8 HW2

9 Quick review of last 2 lectures

10 General Form of a C Program Figure 2.7. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

11 Our First Program int main(int argc, char* argv[]) {}{} #include // comments about the program printf(“Hello World! \n”); return 0;

12 The same program in the Java language public class MyProgram {}{} // comments about the class public static void main (String[] args) {}{} // comments about the method System.out.println(“Hello World”);

13 Programming Languages Each type of CPU executes only a particular machine language A program must be translated into machine language before it can be executed A compiler is a software tool which translates source code into a specific target language Often, that target language is the machine language for a particular CPU type

14 Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program The semantics of a program statement define what that statement means (its purpose or role in a program) A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do

15 Program Development The mechanics of developing a program include several activities  writing the program in a specific programming language  translating the program into a form that the computer can execute  investigating and fixing various types of errors that can occur Software tools can be used to help with all parts of this process

16 Entering, Translating, and Running a High-Level Language Program [Figure 1.12 in the textbook]

17 Basic Program Development errors Edit and save program Compile program Execute program and evaluate results

18 Errors (a.k.a., bugs) A program can have three types of errors The compiler will find syntax errors and other basic problems (compile-time errors)  If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)

19 The first bug! Read more at: http://www.history.navy.mil/photos/pers-us/uspers-h/g-hoppr.htm

20 Chapter 2

21 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

22 Rules for valid variable names The name can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Variable names cannot begin with a digit C is case sensitive - Total, total, and TOTAL are different identifiers By convention, programmers use different case styles for different types of names/identifiers, such as  title case for variable names - Lincoln  upper case for constants - MAXIMUM

23 Variable Initialization A variable can be given an initial value in the declaration When a variable is referenced in a program, its current value is used int sum = 0; int base = 32, max = 149;

24 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

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

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

27 Constants A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence As the name implies, it is constant, not variable The compiler will issue an error if you try to change the value of a constant In C, we use the const modifier to declare a constant const int MIN_HEIGHT = 69;

28 Constants Constants are useful for three important reasons First, they give meaning to otherwise unclear literal values  For example, MAX_LOAD means more than the literal 250 Second, they facilitate program maintenance  If a constant is used in multiple places, its value need only be updated in one place Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers

29 #define primitive Constants can also be defined using the primitives of the C preprocessor #define KMS_PER_MILE 1.609

30 Miles-to-Kilometers Conversion Program Figure 2.1. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

31 Memory State: (a) Before and (b) After Execution of the Program Figure 2.2. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

32 Effect of kms = KMS_PER_MILE * miles; Figure 2.3. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

33 [Figure 1.4 in the textbook]

34 Memory Analogy

35

36

37 Some Primitive Data Types int float double

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

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

40 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

41 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

42 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

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

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

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

46 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

47 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

48 Analogy with car odometers

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

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

51 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]

52 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"

53 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.");

54 Escape Sequences Some C escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

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

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

57 Evaluating Expressions

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

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

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

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

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

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

64 Questions?

65 THE END

66 TA Office hours The office hours are posted on the class web page. The TAs get paid to help you with this class. Make them earn their salaries. You can see any TA you like (not only the ones that lead your lab section). Different TAs have different strengths.


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

Similar presentations


Ads by Google