Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)
Numerical Computation Review and Continuation CS-2301, B-Term Simple Program Example #include const double pi = ; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main Purpose: To calculate the length of one side of a polygon given the number of sides and the radius of the circumscribing circle.
Numerical Computation Review and Continuation CS-2301, B-Term Things We Have Seen So Far #include Compiler Directive: instructs the compiler to copy program text from some file into your program Header File: ends in.h — the file that is included Purpose – to declare functions, variables, and other stuff provided elsewhere (e.g., by other.c files!). Declaration and Definition Introduces a new identifier and gives meaning to it Specifies what type, how initialized, etc. Definition: specifies storage or code associated with the identifier
Numerical Computation Review and Continuation CS-2301, B-Term Simple Program Example #include const double pi = ; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main Header Files
Numerical Computation Review and Continuation CS-2301, B-Term Simple Program Example #include const double pi = ; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main Declarations and Definitions: Introduces new names Sets aside storage space for data or code for function
Numerical Computation Review and Continuation CS-2301, B-Term Things We Have Seen So Far (continued) Comments: Anything between '/*' and '*/' ; may span lines Anything after '//' and end of line Equivalent to White Space White Space A blank, tab, new line, vertical tab, form feed, or comment Separates identifiers and constants from each other and from reserved words Reserved Words Identifiers with special meaning to C programs; may not be used for any other purpose
Numerical Computation Review and Continuation CS-2301, B-Term Simple Program Example #include const double pi = ; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main A Comment A reserved word Another Comment
Numerical Computation Review and Continuation CS-2301, B-Term Simple Program Example #include const double pi = ; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main Functions used in our program but declared in header files
Numerical Computation Review and Continuation CS-2301, B-Term More about Numerical Computation More on Declarations and Definitions Assignment statements Expressions Operator Precedence
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold an unsigned integer. Name of that location is i
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate two memory locations, each big enough to hold an integer. Names of those locations are j and k Cannot count on them being in contiguous locations
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold a short integer. –i.e., short int m = 0 Name of that location is m Initialize the value of that location to 0 –Note: none of the previous integers have been initialized
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold a long integer. Name of that location is n Initialize the value of that location to the value stored in m
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = k; … What if we had tried to initialize it to k instead? What would value of n be?
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Same as previous, but for floating point data
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Same as previous, but for floating point data Note the decimalized notation for the initialization
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Any numerical constant with a decimal point is a floating point number! 2.0 is a floating point constant 2 is an integer constant This makes a difference in some programs.
Numerical Computation Review and Continuation CS-2301, B-Term Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Scientific notation for initializations – i.e., –9.3 10 6 –2.1 10 -8
Numerical Computation Review and Continuation CS-2301, B-Term Questions?
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Note: computer scientists often refer to the location as the l-value (i.e., left value; see p.197)
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Assign the value 3 to the location i
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Assign the value from location i to location j
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Apply the sin function to the value at location x and store the result in location f
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Evaluate the expression (see below) and store the result in location g
Numerical Computation Review and Continuation CS-2301, B-Term Note A declaration of a variable with an initial value is equivalent to an assignment to that variable. E.g., float b = 3.5; is the same as float b; b = 3.5;
Numerical Computation Review and Continuation CS-2301, B-Term Questions?
Numerical Computation Review and Continuation CS-2301, B-Term Definition — Expression A sequence of operands and operators that, when evaluated, produce a result value Always scanned left-to-right by compiler –However, precedence of operators may define a different order of evaluation (see below)
Numerical Computation Review and Continuation CS-2301, B-Term Arithmetic Operators Unary – ‘ + ’ and ‘ – ’ Indicates sign of numerical value Additive – ‘ + ’ and ‘ – ’ Adds or subtracts two numbers, returns sum or difference Multiplicative – ‘ * ’, ‘ / ’, and ‘ % ’ ‘ * ’ – multiplies to numbers together, returns product ‘ / ’ – divides first number by second, returns quotient ‘ % ’ – integer division, returns remainder … E.g., -3.2, +5 -x, +i -sin(x)
Numerical Computation Review and Continuation CS-2301, B-Term Arithmetic Expressions a*x + b c*c + 2*c*d + d*d c*c*c + 3*c*c*d + 3*c*d*d + d*d*d 1/(1/v1 + 1/v2) (minutes1 + minutes2) % 60
Numerical Computation Review and Continuation CS-2301, B-Term Arithmetic Expressions (continued) Arithmetic expressions always return a value of the same type as their operands Type conversion rules apply if operands are of different or mixed types See §A.2, pp More later '/', and '%' are undefined if divisor is zero
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Operator (again) ‘ = ’ — assigns value of the expression on the right to memory location defined by left y = a*x + b; i = j + 1; z = y = a*x + b; Note: assignment is just another operator in an expression Value of the assignment expression is the value assigned
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Expression ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; Evaluate this expression first
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Expression (continued) ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; 33 Assign the result here
Numerical Computation Review and Continuation CS-2301, B-Term Assignment Expression (continued) ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; 34 Assign that result here
Numerical Computation Review and Continuation CS-2301, B-Term Questions?
Numerical Computation Review and Continuation CS-2301, B-Term Specifying Symbolic Constants in C Two ways –Textual substitution –Declaration of const data object
Numerical Computation Review and Continuation CS-2301, B-Term Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI #define LOWER 0 #define UPPER 300 #define STEP 20 It is traditional in C for textual substitution names to be all UPPER CASE
Numerical Computation Review and Continuation CS-2301, B-Term Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI #define LOWER 0 #define UPPER 300 #define STEP 20 When a textual substitution constant is used in a program, the compiler simply substitutes the replacement text on the fly
Numerical Computation Review and Continuation CS-2301, B-Term Constant – Textual Substitution (continued) #define is a Compiler Directive –Instructs compiler to re-write the code by substituting the definition for the defined word –No meaning is attached to the substitution
Numerical Computation Review and Continuation CS-2301, B-Term Constant Declaration const double pi = ; const double c = e+8; /* speed of light in meters/sec */ Defines a value of the declared type with the declared name I.e., creates storage to hold this value Must be initialized May never be used on left side of an assignment
Numerical Computation Review and Continuation CS-2301, B-Term Questions?
Numerical Computation Review and Continuation CS-2301, B-Term Introduction to Operator Precedence Suppose you encounter the following expressions in a math, physics, or engineering problem:– How do you represent them in C and what order should the operators be evaluated?
Numerical Computation Review and Continuation CS-2301, B-Term Arithmetic Expressions with Multiple Operators pow(x,3) + 3*pow(x,2)*y + 3*x*pow(y,2) + pow(y,3) 1 st 2 nd 3 rd 4 th 5 th 6 th 8 th 7 th 9 th 10 th 11 th Value of expression
Numerical Computation Review and Continuation CS-2301, B-Term Exercise – Do the same for this expression Representation as a C expression Order of operations
Numerical Computation Review and Continuation CS-2301, B-Term Definitions Operator Precedence –The relative order in which operators in an expression in C are executed Operator Associativity –When two operators are of same precedence, whether the left or right operator is applied first See Table 2-1, p. 53 A very important topic. Easy to get tripped up. Source of lots of errors!
Numerical Computation Review and Continuation CS-2301, B-Term Operator Precedence Table A portion of the table is:– ( )/* highest – function call and parenthesization */ + -/* unary, right to left*/ * / %/* two operands, left to right */ + -/* two operands, l-to-r*/ =/* assignment, right to left */,/* lowest – sequence of expressions */
Numerical Computation Review and Continuation CS-2301, B-Term Operator Precedence When scanning an expression … A pending operator is not applied until it is of higher precedence than the next operator or It has the same precedence and associativity is left-to-right
Numerical Computation Review and Continuation CS-2301, B-Term Arithmetic Expressions with Multiple Operators pow(x,3) + 3*pow(x,2)*y + 3*x*pow(y,2) + pow(y,3) Highest – evaluate within () before anything else Low – look ahead to see if next operator is higher Medium – look ahead to see if next operator is lower
Numerical Computation Review and Continuation CS-2301, B-Term Warning Operator precedence does not specify order of evaluation of functions E.g., –x = f(arg1) + g(arg2); –f and g may be evaluated in either order! –arg1 and arg2 may be evaluated in either order Exceptions:– –&&, ||, ?:, and ',' Important if f and g have side effects
Numerical Computation Review and Continuation CS-2301, B-Term How does the compiler implement precedence? Answer:– using a data structure called a stack Definition:– Stack A linear data structure comprising a sequence of items, such that the last item added is the first item removed
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Detect constant token representing value 1 Place value 1 on value-location stack 1 values&locationsoperators
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token Place on operator stack 1 values&locations / operators
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ ( ’ token –Start of a “sub expression” –Highest precedence of all Place matching ‘ ) ’ on operator stack 1 values&locations / operators ()
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover constant token 1 (again) Place on top of value- location stack 1 values&locations / operators ()1
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token –Higher precedence than ‘ ( ’ Place ‘ / ’ on operator stack 1 values&locations / operators ()1 /
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover identifier token v1 Place on value- location stack 1 values&locations / operators ()1 /v1
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover token ‘ + ’ –Note that ‘ + ’ has lower precedence than ‘ / ’ –Cannot place it above ‘ / ’ Emit code to calculate 1/v1 –Consumes ‘ / ’ operator and top two values from stacks Place result on value- location stack Place on operator stack 1 values&locations / operators ()1 /v1
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Stacks now look like this 1 values&locations / operators ()1/v1 +
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another value 1 –Place on stack 1 values&locations / operators ()1/v1 +1
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another ‘ / ’ operator –Higher precedence than top of operator stack Place on operator stack 1 values&locations / operators ()1/v1 +1 /
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another identifier v2 Place on value- location stack 1 values&locations / operators ()1/v1 +1 /v2
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ ) ’ token –Finish all operations back to ‘ ()’ 1 values&locations / operators ()1/v1 +1 /v2
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Emit code to do ‘ / ’ operation –Consumes top two values on the stack Places result on value location stack 1 values&locations / operators ()1/v1 +1 /v2
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Emit code for ‘ + ’ operation –Places result (i.e., sum ) on value location stack 1 values&locations / operators ()1/v1 +1/v2
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Delete ‘ () ’ 1 values&locations / operators ()sum
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover end of expression Emit code for ‘ / ’ operation Place result on value location stack 1 values&locations / operators sum
Numerical Computation Review and Continuation CS-2301, B-Term Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Final result is value of the expression 1/sum values&locationsoperators
Numerical Computation Review and Continuation CS-2301, B-Term Questions?