Download presentation
Presentation is loading. Please wait.
Published byMiles Tovey Modified over 9 years ago
1
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Fa05: Timing about right B Smith: Fa05: Timing about right B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming. B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming.
2
2 Administrivia Lab 1 should be submitted by end of lab today See Blackboard for next few labs Lab 2 due Friday, Sept 2 Lab 3 due Friday, Sept 9
3
3 Overview Define basic data type Evaluate Arithmetic Expressions using C
4
4 Basic data types in C Integer: 2, -50, +2, -2 Floating point: 0.331, -5978.55, 1.0, +1. Double precision: 6 – 7 digits precision Character: ‘W’, ‘w’, ‘+’, ‘&’ Exponential Notation 5.123e-7
5
5 Integers (2, -50, +2, -2, etc.) A positive or negative number with no fractional part or (no decimal point.) From the American Heritage dictionary: A member of the set of positive whole numbers {1, 2, 3,... }, negative whole numbers {-1, -2, -3,... }, and zero {0} Can be “signed” or “unsigned” 5622 ok, but not 20215613210254666154? There is a limit to the size of allowed integer types B Smith: Show table (list) as an example: If we have only 6 spots available, you can have signed: -3,-2,-1,0,1,2 or unsigned: 0,1,2,4,5,6 B Smith: Show table (list) as an example: If we have only 6 spots available, you can have signed: -3,-2,-1,0,1,2 or unsigned: 0,1,2,4,5,6 B Smith: not covered Fa 05 B Smith: not covered Fa 05
6
6 Arithmetic Expressions take arithmetic (numerical) values and return an arithmetic (numerical) value Are composed using the following operators: + (unary plus) - (unary minus) + (addition) - (subtraction) * (multiplication) / (division or quotient) % (modulus or remainder) L01a.c B Smith: This is a great opportunity to start using a debugger in class to show variable changes. This would be an alternative to using printf to trace through a program! B Smith: This is a great opportunity to start using a debugger in class to show variable changes. This would be an alternative to using printf to trace through a program!
7
7 Unary operators Called unary because they require one operand. Examples i = +1;/* + used as a unary operator */ j = -i;/* - used as a unary operator */ The unary operator ‘+’ does nothing just emphasis that a numeric constant is positive. The unary operator ‘–’ produces the negative of its operand.
8
8 Precedence in Expressions Defines the order in which an expression is evaluated As in algebra we need rules regarding what gets done first Write down your answers to the following:
9
9 Precedence in Expressions 1 + 2 * 3 - 4 / 5 = P stands for parentheses, E for Exponents, M for multiplication D for division, A for addition, and S for subtraction. P.E.M.D.A.S. 1 + (2 * 3) - (4 / 5)
10
10 More on precedence *, /, % are at the same level of precedence +, - are at the same level of precedence For operators at the same “level,” left-to-right ordering is applied. 2 + 3 – 1 = (2 + 3) – 1 = 4 2 – 3 + 1 = (2 – 3) + 1 = 0 2 * 3 / 4 = (2 * 3) / 4 = 6 / 4 2 / 3 * 4 = (2 / 3) * 4 = 0 / 4 B Smith: Provide an example for next slide. Your turn... B Smith: Provide an example for next slide. Your turn...
11
11 Precedence in Expressions – Example (cont) 6.2 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) B Smith: This entire sequence can be covered on 1 slide using a tablet B Smith: This entire sequence can be covered on 1 slide using a tablet
12
12 Precedence in Expressions – Example (cont) 6.2 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
13
13 Precedence in Expressions – Example (cont) Integer division results in integer quotient 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
14
14 Precedence in Expressions – Example (cont) = 0 7 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
15
15 Precedence in Expressions – Example (cont) 7 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
16
16 int -s and float -s float is a “communicable” type Example: 1 + 2 * 3 - 4.0 / 5 = 1 + (2 * 3) - (4.0 / 5) = 1 + 6 - 0.8 = 6.2 B Smith: Redundant since done on board with tablet. Consider writing out line 1 and finishing live B Smith: Redundant since done on board with tablet. Consider writing out line 1 and finishing live
17
17 int -s and float -s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0
18
18 int -s and float -s – Example 2 (cont) (1 + 2.0) * (3 - 4) / 5 = ((1 + 2.0) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6
19
19 int -s and float -s – Example 3 (1 + 2.0) * ((3 - 4) / 5) = (1 + 2.0) * (-1 / 5) = 3.0 * 0 = 0.0
20
20 Floating Point and Exponential Notation (e.g. 2.555e4, 3.1e-3, 7e0) Generally written in exponential format i.e., 3000 would be 3e3 and 1/1000 or 0.001 would be 1e-3 the precision of the floating-point representation is determined by the number of digits the computer will allow for the decimal part of the mantissa i.e. in 2.555e4, 2.555 is the mantissa Whereas the the range of the floating-point representation is based on the number of digits the computer allows for the exponent i.e., in 2.555e4, 4 is the exponent
21
21 Summary Defined basic data type Evaluated Arithmetic Expressions using C Read chapter 2 of your book and review lectures notes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.