Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Computation Review and Continuation CS-2301, B-Term 20091 Numerical Computation in C Review and Continuation CS-2301, System Programming for.

Similar presentations


Presentation on theme: "Numerical Computation Review and Continuation CS-2301, B-Term 20091 Numerical Computation in C Review and Continuation CS-2301, System Programming for."— Presentation transcript:

1 Numerical Computation Review and Continuation CS-2301, B-Term 20091 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)

2 Numerical Computation Review and Continuation CS-2301, B-Term 20092 Simple Program Example #include const double pi = 3.14159265358979323846; 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.

3 Numerical Computation Review and Continuation CS-2301, B-Term 20093 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

4 Numerical Computation Review and Continuation CS-2301, B-Term 20094 Simple Program Example #include const double pi = 3.14159265358979323846; 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

5 Numerical Computation Review and Continuation CS-2301, B-Term 20095 Simple Program Example #include const double pi = 3.14159265358979323846; 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

6 Numerical Computation Review and Continuation CS-2301, B-Term 20096 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

7 Numerical Computation Review and Continuation CS-2301, B-Term 20097 Simple Program Example #include const double pi = 3.14159265358979323846; 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

8 Numerical Computation Review and Continuation CS-2301, B-Term 20098 Simple Program Example #include const double pi = 3.14159265358979323846; 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

9 Numerical Computation Review and Continuation CS-2301, B-Term 20099 More about Numerical Computation More on Declarations and Definitions Assignment statements Expressions Operator Precedence

10 Numerical Computation Review and Continuation CS-2301, B-Term 200910 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

11 Numerical Computation Review and Continuation CS-2301, B-Term 200911 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

12 Numerical Computation Review and Continuation CS-2301, B-Term 200912 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

13 Numerical Computation Review and Continuation CS-2301, B-Term 200913 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

14 Numerical Computation Review and Continuation CS-2301, B-Term 200914 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?

15 Numerical Computation Review and Continuation CS-2301, B-Term 200915 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

16 Numerical Computation Review and Continuation CS-2301, B-Term 200916 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

17 Numerical Computation Review and Continuation CS-2301, B-Term 200917 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.

18 Numerical Computation Review and Continuation CS-2301, B-Term 200918 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

19 Numerical Computation Review and Continuation CS-2301, B-Term 200919 Questions?

20 Numerical Computation Review and Continuation CS-2301, B-Term 200920 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)

21 Numerical Computation Review and Continuation CS-2301, B-Term 200921 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

22 Numerical Computation Review and Continuation CS-2301, B-Term 200922 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

23 Numerical Computation Review and Continuation CS-2301, B-Term 200923 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

24 Numerical Computation Review and Continuation CS-2301, B-Term 200924 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

25 Numerical Computation Review and Continuation CS-2301, B-Term 200925 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;

26 Numerical Computation Review and Continuation CS-2301, B-Term 200926 Questions?

27 Numerical Computation Review and Continuation CS-2301, B-Term 200927 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)

28 Numerical Computation Review and Continuation CS-2301, B-Term 200928 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)

29 Numerical Computation Review and Continuation CS-2301, B-Term 200929 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

30 Numerical Computation Review and Continuation CS-2301, B-Term 200930 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. 197-198 More later '/', and '%' are undefined if divisor is zero

31 Numerical Computation Review and Continuation CS-2301, B-Term 200931 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

32 Numerical Computation Review and Continuation CS-2301, B-Term 200932 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

33 Numerical Computation Review and Continuation CS-2301, B-Term 200933 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

34 Numerical Computation Review and Continuation CS-2301, B-Term 200934 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

35 Numerical Computation Review and Continuation CS-2301, B-Term 200935 Questions?

36 Numerical Computation Review and Continuation CS-2301, B-Term 200936 Specifying Symbolic Constants in C Two ways –Textual substitution –Declaration of const data object

37 Numerical Computation Review and Continuation CS-2301, B-Term 200937 Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI 3.14159265358979323846 #define LOWER 0 #define UPPER 300 #define STEP 20 It is traditional in C for textual substitution names to be all UPPER CASE

38 Numerical Computation Review and Continuation CS-2301, B-Term 200938 Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI 3.14159265358979323846 #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

39 Numerical Computation Review and Continuation CS-2301, B-Term 200939 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

40 Numerical Computation Review and Continuation CS-2301, B-Term 200940 Constant Declaration const double pi = 3.14159265358979323846; const double c = 2.99792458e+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

41 Numerical Computation Review and Continuation CS-2301, B-Term 200941 Questions?

42 Numerical Computation Review and Continuation CS-2301, B-Term 200942 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?

43 Numerical Computation Review and Continuation CS-2301, B-Term 200943 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

44 Numerical Computation Review and Continuation CS-2301, B-Term 200944 Exercise – Do the same for this expression Representation as a C expression Order of operations

45 Numerical Computation Review and Continuation CS-2301, B-Term 200945 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!

46 Numerical Computation Review and Continuation CS-2301, B-Term 200946 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 */

47 Numerical Computation Review and Continuation CS-2301, B-Term 200947 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

48 Numerical Computation Review and Continuation CS-2301, B-Term 200948 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

49 Numerical Computation Review and Continuation CS-2301, B-Term 200949 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

50 Numerical Computation Review and Continuation CS-2301, B-Term 200950 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

51 Numerical Computation Review and Continuation CS-2301, B-Term 200951 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

52 Numerical Computation Review and Continuation CS-2301, B-Term 200952 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token Place on operator stack 1 values&locations / operators

53 Numerical Computation Review and Continuation CS-2301, B-Term 200953 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 ()

54 Numerical Computation Review and Continuation CS-2301, B-Term 200954 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

55 Numerical Computation Review and Continuation CS-2301, B-Term 200955 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token –Higher precedence than ‘ ( ’ Place ‘ / ’ on operator stack 1 values&locations / operators ()1 /

56 Numerical Computation Review and Continuation CS-2301, B-Term 200956 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover identifier token v1 Place on value- location stack 1 values&locations / operators ()1 /v1

57 Numerical Computation Review and Continuation CS-2301, B-Term 200957 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

58 Numerical Computation Review and Continuation CS-2301, B-Term 200958 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Stacks now look like this 1 values&locations / operators ()1/v1 +

59 Numerical Computation Review and Continuation CS-2301, B-Term 200959 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another value 1 –Place on stack 1 values&locations / operators ()1/v1 +1

60 Numerical Computation Review and Continuation CS-2301, B-Term 200960 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 /

61 Numerical Computation Review and Continuation CS-2301, B-Term 200961 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

62 Numerical Computation Review and Continuation CS-2301, B-Term 200962 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ ) ’ token –Finish all operations back to ‘ ()’ 1 values&locations / operators ()1/v1 +1 /v2

63 Numerical Computation Review and Continuation CS-2301, B-Term 200963 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

64 Numerical Computation Review and Continuation CS-2301, B-Term 200964 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

65 Numerical Computation Review and Continuation CS-2301, B-Term 200965 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Delete ‘ () ’ 1 values&locations / operators ()sum

66 Numerical Computation Review and Continuation CS-2301, B-Term 200966 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

67 Numerical Computation Review and Continuation CS-2301, B-Term 200967 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Final result is value of the expression 1/sum values&locationsoperators

68 Numerical Computation Review and Continuation CS-2301, B-Term 200968 Questions?


Download ppt "Numerical Computation Review and Continuation CS-2301, B-Term 20091 Numerical Computation in C Review and Continuation CS-2301, System Programming for."

Similar presentations


Ads by Google