BIL 104E Introduction to Scientific and Engineering Computing

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Data Types, Expressions and Functions (part I)
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ME 142 Engineering Computation I Excel Functions.
ME 142 Engineering Computation I Excel Functions.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Recap……Last Time [Variables, Data Types and Constants]
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Variables, Operators, and Expressions
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
Expressions.
Chapter 7: Expressions and Assignment Statements
ARITHMETIC IN C Operators.
Lecture 3 Java Operators.
ECE 1304 Introduction to Electrical and Computer Engineering
Expressions.
Operators and Expressions
Chapter 3 Assignment and Interactive Input.
TMF1414 Introduction to Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Arithmetic & other operations
Fundamental of Java Programming Basics of Java Programming
Introduction to Programming
Type Conversion, Constants, and the String Object
Copyright © 2012 Pearson Education, Inc.
Lecture 2B Expressions Richard Gesick
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Primitive Types and Expressions
OPERATORS AND EXPRESSIONS IN C++
OPERATORS in C Programming
Operator King Saud University
Data Types and Expressions
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

BIL 104E Introduction to Scientific and Engineering Computing Lecture 3

Symbolic Constants Defined with a preprocessor directive that assigns an identifier to a constant. The directive can appear anywhere in the program; the compiler will replace each occurrence of the directive identifier with the constant value in all statements that follow the directive. Only one symbolic constant can be defined in a directive; if several symbolic constants are desired, several separate directives are required. Preprocessor directives which include the #define statement; do not end with a semicolon. Example: #define PI 3.141593 /*Note “;” is not used */ … area=PI*radius*radius; 17.06.2018 Lecture 3

Assignment Statements General Form: identifier=expression; /*The equal sign should be read as “is assigned the value of” */ Example: sum=10.5; /* Expression is a constant */ rate=state_tax; /* Expression is another variable */ sum= a+b; /* Expression is result of an operation*/ Multiple assignments are also allowed in C. x=y=z=0; 17.06.2018 Lecture 3

Assignment Statements If a value is assigned to a variable that has a different data type, then a conversion must occur during the execution of the statement. Sometimes this may cause loss of data. Example: int a; float b; a=12.8; /* Information loss: a will be 12*/ b=6; /* No information loss: b will be 6.0 */ 17.06.2018 Lecture 3

Arithmetic Operators Symbol Meaning + Addition - Subtraction * Multiplication / Division % Remainder (or modulus) 17.06.2018 Lecture 3

Arithmetic Operators Examples: area_square=side*side; area_triangle=0.5*base*height; x=x+1; /* not valid in algebra but valid in C */ /* x is assigned the value of x plus 1 */ /* value stored in x is incremented by 1 */ The modulus operator is useful in determining if an integer is a multiple of another number. Thus, if x%2 is equal to 0 then x is even and if x%5 is equal to 0 then x is a multiple of 5. Precedence: a*b + b/c*d is equivalent to (a*b) + ((b/c)*d) 17.06.2018 Lecture 3

Precedence Precedence Operator Associativity 1 paranthesis: ( ) innermost first 2 uniary operators: + - (type) right to left 3 binary operators: * / % left to right 4 binary operators: + - 5 assignment operators: = += -= *= /= %= 17.06.2018 Lecture 3

Arithmetic Operations The result of a binary operation with values of same type is another value of the same type. For example: If a and b are double then a/b is also double. An integer division can sometimes produce unexpected results because any decimal portion of the integer is dropped; the result is a truncated result, not a rounded result. Thus, 5/3 is equal to 1, and 3/6 is equal to 0. An operation between values with different types is a mixed operation. Before the operation is performed, the value with the lover type is converted to the higher type, thus the operation is performed with values of the same type. 17.06.2018 Lecture 3

Cast Operator Cast operator allows specifying a type change temporarily in the value before the next computation. Example (Without cast operator): int sum=18, count=5; float average; … average=sum/count; /*average is 3.0, not 3.6 */ /* the result of the integer division is going to be */ /* a truncated result, thus there is information loss. */ 17.06.2018 Lecture 3

Cast Operator Example (With cast operator): int sum=18, count=5; float average; … average=(float)sum/count; /*average is 3.6 */ /* by the cast operator sum is converted to float */ /* before the division is performed. The division is */ /* then a mixed operation between a float value and */ /* an integer, so the value of the count is also converted */ /* to float value before the division. The result is a float */ /* value and stored in average without loss of information. */ 17.06.2018 Lecture 3

Break long expressions into several statements f=(x*x*x-2*x*x+x-6.3)/(x*x+0.05005*x-3.14); Statement can be broken into two lines: f=(x*x*x-2*x*x+x-6.3)/ (x*x+0.05005*x-3.14); Or, numerator and denominator can be computed separately: numerator=x*x*x-2*x*x+x-6.3; denominator=x*x+0.05005*x-3.14; f= numerator/denominator; 17.06.2018 Lecture 3

Increment and Decrement Operators Increment operator (++) : y++; is equal to y = y + 1; Decrement operator (--) : y--; is equal to y = y – 1; Preincrementation and predecrementation: The identifier is modified and the new value is used in evaluating the rest of the expression. w = ++x – y; is equivalent to x = x +1; w = x – y; Postincrementation and postdecrementation: The old value of the identifier is used in evaluating the rest of the expression and its value is modified. w = x++ - y; is equivalent to w = x – y; x = x + 1; 17.06.2018 Lecture 3

Abbreviated Assignment Operators x = x + 3; is equivalent to x +=3; d = d / a; is equivalent to d /= a; a = b += c + d; is equivalent to a = (b += (c + d)); a = (b += (c + d)); is equivalent to a = (b = b + (c + d)); or a = (b += (c + d)); is equivalent to b = b + (c + d); a = b; 17.06.2018 Lecture 3

Conditional Expressions A condition is an expression that can be evaluated to be true or false and is composed of expressions combined with relational operators; a condition can also include logical operators. The relational operators can be used to compare two expressions. Relational Operator Interpretation < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to 17.06.2018 Lecture 3

Relational Operators Examples: a<b x+y >= 10.5 fabs(denominator) < 0.0001 A true condition is assigned to a value of 1; a false condition is assigned to a value of zero. So the following statement is valid: d = b>c; //if b>c then d=1, else d=0 Because a condition is given a value, it is valid to use a value in place of a condition 17.06.2018 Lecture 3

Logical Operators Logical operators can be used to compare conditions and generate new conditions. Example: a<b && b<c (The relational operators have higher precedence than the logical operator.) Logical Operator Symbol not ! and && or || A B A&&B A||B !A !B False True 17.06.2018 Lecture 3

Precedence A condition can contain several logical operators. The hierarchy from highest to the lowest is !, &&, ||, but parenthesis can be used to change the hierarchy. Precedence Operator Associativity 1 ( ) innermost first 2 + - ++ -- (type) ! right to left (unary) 3 * / % left to right 4 + - 5 < <= > >= 6 == != 7 && 8 !! 9 = += -= *= /= %= right to left 17.06.2018 Lecture 3

Mathematical Functions #include <math.h> preprocessor directive should be used in programs referencing the mathematical functions which are available in Standard C library. General notes about functions: A function reference returns a single value. The parentheses following the function name contain the inputs to the function, which are called parameters or arguments. A function may contain no arguments, one argument, or many arguments, depending on its definition. The arguments should be listed in the correct order as in the function definition. 17.06.2018 Lecture 3

Elementary Math Functions fabs(x) Computes the absolute value of x. sqrt(x) Computes the square root of x, where x≥0. pow(x,y) Computes xy. Errors occur if x=0 and y≤0, or if x<0 and y is not an integer. ceil(x) Rounds x to the nearest integer toward ∞. floor(x) Rounds x to the nearest integer toward -∞. exp(x) Computes ex. log(x) Computes ln(x). Errors if x≤0. log10(x) Computes log10(x). Errors if x≤0. 17.06.2018 Lecture 3

Trigonometric Functions Trigonometric functions take arguments in radians. To convert radians to degrees, or degrees to radians the following conversions can be used: #define PI 3.141593 … angle_deg = angle_rad*(180/PI); angle_rad = angle_deg*( PI/180); sin(x) Computes the sine of x. cos(x) Computes the cosine of x. tan(x) Computes the tangent of x. asin(x) Computes the arcsine of x where -1≤x≤1. acos(x) Computes the arccosine of x where -1≤x≤1. atan(x) Computes the arctangent of x. atan2(y,x) Computes the arctangent of y/x. Returns an angle in any quadrant, depending on the signs of x and y. 17.06.2018 Lecture 3