More on Numerical Computation CS2301 System Programming for






















- Slides: 22
More on Numerical Computation CS-2301, System Programming for Non -majors (Slides include materials from The C Programming Language, 2 nd ed. , by Kernighan and Ritchie and from C: How to Program, 5 th ed. , by Deitel and Deitel) CS-2301 B-term 2008 More on Numerical Computation 1
Reading Assignment • Chapter 2 of Kernighan & Ritchie CS-2301 B-term 2008 More on Numerical Computation 2
Digression on scanf() & printf() • Prints a string in which each conversion specifier is replaced with value of corresponding argument • printf(“string in double quotes”, arg 1, arg 2, arg 3, …); • Conversion specifier: – • Begins with ‘%’ character • Describes how to print one argument • ith conversion specifier in string says how to print argi • Resulting string printed on stdout • Returns number of characters printed CS-2301 B-term 2008 More on Numerical Computation 3
printf() (continued) • %d, %i — decimal number • %u — unsigned decimal number • %c — character • %s — string • %f — floating point number • e. g. , 3. 14159 • %e, E — floating point number • e. g. , 2. 9979 e+8 or 2. 9979 E+8 • %% — a single ‘%’ character • See textbook for full list (pp 154, 244) CS-2301 B-term 2008 More on Numerical Computation 4
printf() Optional Controls • %6. 4 d • ^ – minimum field width • Padded on left • ^ – precision of number • %. 12 s • ^ – width of string • %-6. 4 d • ^ – indicates left justify • Padded on right CS-2301 B-term 2008 More on Numerical Computation 5
printf() Examples int j = 24; float two. Pi = 2 * pi; printf(“j=%d, k=%fn”, j, two. Pi ); • Output j=24, k=6. 28319 printf(“%4 d %4 d %6 d”, 1, 100, 1000); • Output 1 10 1000. . . CS-2301 B-term 2008 More on Numerical Computation 6
scanf() • Reads input, decomposes into individual variables • Opposite of printf() • scanf(“string in double quotes”, &arg 1, &arg 2, &arg 3, …); • Arguments must be locations – use‘&’ • Converts input string according to scan string, stores in variables • Returns number of matched and stored items – Or EOF if end of file is encountered • Stops at end of string or when no match is found CS-2301 B-term 2008 More on Numerical Computation 7
scanf() Examples int i; double x; char c; scanf(“%d%f%c”, &i, &x, &c); • Looks first for an integer • Skips white space • Looks next for a floating point • Skips white space • Looks next for a single character • Does not skip white space; returns the first character it finds CS-2301 B-term 2008 More on Numerical Computation 8
scanf() Formats • %d — any decimal number • %u — an unsigned integer • %c — character • White space not skipped • %e, f, g — floating point number • %s — string • Defer to later in the course • %% — matches a single ‘%’ character • Any other character • Matches that character CS-2301 B-term 2008 More on Numerical Computation 9
May specify “h” or “l” scanf() Formats indicating short or long • %d — any decimal number integer, float vs. double • %u — an unsigned integer • %c — character • White space not skipped • %e, f, g — floating point number • %s — string • Defer to later in the course • %% — matches a single ‘%’ character • Any other character • Matches that character CS-2301 B-term 2008 More on Numerical Computation 10
Questions? CS-2301 B-term 2008 More on Numerical Computation 11
More Numerical Operators • Relational operators – <, <=, >, >=, ==, != – Return 0 if false, 1 if true • Let int a = 3; • Then a a < 3 returns 0 >= 3 returns 1 == 3 returns 1 != 3 returns 0 CS-2301 B-term 2008 More on Numerical Computation 12
Rel More Numerical Operators ati • not onal o s are pecial perato just in C rs ar ope Relational operators rato like an. They e rs in y ot – <, <=, >, >=, ==, != exp her ress ions – Return 0 if false, 1 if true • Let int a = 3; • Then a a < 3 returns 0 >= 3 returns 1 == 3 returns 1 != 3 returns 0 CS-2301 B-term 2008 More on Numerical Computation 13
Precedence of Relational Operators • Comparisons: less than arithmetic operators • Equality & inequality: less than comparisons • See p. 53 • Examples • i < lim -1 means i < (lim – 1) • X + (y >= 3) returns the value X (when y < 3) and the value X+1 (when y >= 3) CS-2301 B-term 2008 More on Numerical Computation 14
Increment & Decrement Operators • ++x, --x, x++, x-- • Increments or decrements x by 1 • ++x – increments x, returns new value of x • x++ – increments x, returns old value of x • Used in many situations, especially for loops • --x – decrements x, returns new value of x • x-- – decrements x, returns old value of x • High precedence than *, /, % – Associates right to left CS-2301 B-term 2008 More on Numerical Computation 15
Bitwise Operations • • & | ^ << – bitwise AND – bitwise OR (inclusive) – bitwise exclusive OR – left shift • Same as multiplying by 2 (i. e. , fills low-order bits with zeros) • >> – right shift • Machine dependent fill on left • ~ – one’s complement • May only be applied to integral types • i. e. , int, short, long, char • signed or unsigned CS-2301 B-term 2008 More on Numerical Computation 16
Conditional Expressions • expr 1 ? expr 2 : expr 3 – Evaluate expr 1. – If result is true, return expr 2 – Otherwise return expr 3 • Example, – z = (a < b) ? a : b – Assigns z the value of a if a < b, or b otherwise • See tricky code at end of § 2. 11 – For use in Homework #1 CS-2301 B-term 2008 More on Numerical Computation 17
Assignment Operator (yet again) • location += expression means – Add expression to the value at location and assign the result back into location • Similarly for -=, *=, /=, %=, ^=. |=, <<=, >>= • E. g. , • x *= a is the same as x = x * a • y /= b is the same as y = y * b • z <<= 3 is the same as z = z << 3 CS-2301 B-term 2008 More on Numerical Computation 18
Type Conversion • May be automatic or explicit • See § 2. 7 • Automatic, for signed operands: – • • If either is long double, convert other to long double Else if either is double, convert other to double Else if either is float, convert other to float Otherwise, convert char and short to int and then if either is long, convert other to long • I. e. , “promote” numerical types from lower to higher CS-2301 B-term 2008 More on Numerical Computation 19
Type Conversion (continued) • Automatic type conversion of unsigned integer values is • Tricky and • Machine dependent CS-2301 B-term 2008 More on Numerical Computation 20
Explicit Type Conversion • Definition – cast • A unary operator applied to an expression to explicitly force the value to a particular type • Represented as (type) expression • High precedence, equal to unary operators • Associates right-to-left • Example (int) sqrt(2*pi) • Converts the square root of 2π to an integer CS-2301 B-term 2008 More on Numerical Computation 21
Questions? Read or review Chapter 2 CS-2301 B-term 2008 More on Numerical Computation 22