190 likes | 263 Views
B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Fa05: Timing about right. C Fundamentals - I. B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming.
E N D
B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Fa05: Timing about right C Fundamentals - I B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming. Math 130Lecture # 2
Overview • Define basic data type • Evaluate Arithmetic Expressions using C
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
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! 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
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.
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:
P.E.M.D.A.S. Precedence in Expressions 1 + 2 * 3 - 4 / 5 = 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.
B Smith: Provide an example for next slide. Your turn. .. 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
6.2 B Smith: This entire sequence can be covered on 1 slide using a tablet Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
6.2 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
Integer division results in integer quotient Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
7 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) = 0
7 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
B Smith: Redundant since done on board with tablet. Consider writing out line 1 and finishing live 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
int-s and float-s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0
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
int-s and float-s – Example 3 (1 + 2.0) * ((3 - 4) / 5) = (1 + 2.0) * (-1 / 5) = 3.0 * 0 = 0.0
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
Summary • Defined basic data type • Evaluated Arithmetic Expressions using C • Read chapter 2 of your book and review lectures notes