Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Lectures on Numerical Methods1 An Example zCompute Squares çPrints a table of squares as shown below /* Compute Squares.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
CPS120: Introduction to Computer Science Lecture 8.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
WEEK 4 ET2640 Logic and Numerical Methods 1ET2640.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Introduction to ‘c’ language
Chapter 2: Basic Elements of C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Fundamental Data Types
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Numerical Data Types.
Chapter 2: Basic Elements of Java
Lectures on Numerical Methods
Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers.
Fundamental Data Types
Lexical Elements & Operators
Chapter 2 Primitive Data Types and Operations
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Programming Fundamental-1
Presentation transcript:

Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers çAn Identifier is a sequence of letters and digits, but must start with a letter. Underscore ( _ ) is treated as a letter. Identifiers are case sensitive. Identifiers are used to name variables, functions etc.  Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If  Invalid: 324, short, price$, My Name zConstants çConstants like 13, ‘a’, 1.3e-5 etc.

Lectures on Numerical Methods2 Tokens in C zString Literals çA sequence of characters enclosed in double quotes as “…”. For example “13” is a string literal and not number 13. ‘a’ and “a” are different. zOperators  Arithmetic operators like +, -, *, /,% etc.  Logical operators like ||, &&, ! etc. and so on. zWhite Spaces çSpaces, new lines, tabs, comments ( A sequence of characters enclosed in /* and */ ) etc. These are used to separate the adjacent identifiers, kewords and constants.

Lectures on Numerical Methods3 Basic Data Types  Integral Types çIntegers are stored in various sizes. They can be signed or unsigned. çExample Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign bit is 0, the number is treated as positive. Bit pattern = 75 (decimal). The largest positive number is = 2 7 – 1 = 127. Negative numbers are stored as two’s complement or as one’s complement. -75 = (one’s complement). -75 = (two’s complement).

Lectures on Numerical Methods4 Basic Data Types zIntegral Types  char Stored as 8 bits. Unsigned 0 to 255. Signed -128 to 127.  short int Stored as 16 bits. Unsigned 0 to Signed to  int Same as either short or long int.  long int Stored as 32 bits. Unsigned 0 to Signed to

Lectures on Numerical Methods5 Basic Data Types zFloating Point Numbers çFloating point numbers are rational numbers. Always signed numbers.  float Approximate precision of 6 decimal digits. Typically stored in 4 bytes with 24 bits of signed mantissa and 8 bits of signed exponent.  double Approximate precision of 14 decimal digits. Typically stored in 8 bytes with 56 bits of signed mantissa and 8 bits of signed exponent. çOne should check the file limits.h to what is implemented on a particular machine.

Lectures on Numerical Methods6 Constants zNumerical Constants  Constants like 12, 253 are stored as int type. No decimal point.  12L or 12l are stored as long int.  12U or 12u are stored as unsigned int.  12UL or 12ul are stored as unsigned long int. çNumbers with a decimal point (12.34) are stored as double. çNumbers with exponent (12e-3 = 12 x ) are stored as double.  12.34f or 1.234e1f are stored as float. çThese are not valid constants: 25,0007.1e 4$2002.3e-3.4 etc.

Lectures on Numerical Methods7 Constants zCharacter and string constants  ‘c’, a single character in single quotes are stored as char. Some special character are represented as two characters in single quotes. ‘\n’ = newline, ‘\t’ = tab, ‘\\’ = backlash, ‘\”’ = double quotes. Char constants also can be written in terms of their ASCII code. ‘\060’ = ‘0’ (Decimal code is 48).  A sequence of characters enclosed in double quotes is called a string constant or string literal. For example “Charu” “A” “3/9” “x = 5”

Lectures on Numerical Methods8 Variables zNaming a Variable çMust be a valid identifier. çMust not be a keyword çNames are case sensitive. çVariables are identified by only first 32 characters. çLibrary commonly uses names beginning with _. çNaming Styles: Uppercase style and Underscore style çlowerLimitlower_limit çincomeTaxincome_tax

Lectures on Numerical Methods9 Declarations zDeclaring a Variable çEach variable used must be declared. çA form of a declaration statement is data-type var1, var2,…; çDeclaration announces the data type of a variable and allocates appropriate memory location. No initial value (like 0 for integers) should be assumed. çIt is possible to assign an initial value to a variable in the declaration itself. data-type var = expression; çExamples int sum = 0; char newLine = ‘\n’; float epsilon = 1.0e-6;

Lectures on Numerical Methods10 Global and Local Variables zGlobal Variables çThese variables are declared outside all functions. çLife time of a global variable is the entire execution period of the program. çCan be accessed by any function defined below the declaration, in a file. /* Compute Area and Perimeter of a circle */ #include float pi = ; /* Global */ main() { floatrad;/* Local */ printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); printf( “Area = %f\n”, area ); } /* Compute Area and Perimeter of a circle */ #include float pi = ; /* Global */ main() { floatrad;/* Local */ printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); printf( “Area = %f\n”, area ); }

Lectures on Numerical Methods11 Global and Local Variables zLocal Variables çThese variables are declared inside some functions. çLife time of a local variable is the entire execution period of the function in which it is defined. çCannot be accessed by any other function. çIn general variables declared inside a block are accessible only in that block. /* Compute Area and Perimeter of a circle */ #include float pi = ; /* Global */ main() { floatrad;/* Local */ printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); printf( “Area = %f\n”, area ); } /* Compute Area and Perimeter of a circle */ #include float pi = ; /* Global */ main() { floatrad;/* Local */ printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); printf( “Area = %f\n”, area ); }

Lectures on Numerical Methods12 Operators zArithmetic Operators  +, -, *, / and the modulus operator %. ç+ and – have the same precedence and associate left to right. 3 – = ( 3 – 5 ) + 7  3 – ( ) – = ( ( ) – 5 ) + 2  *, /, % have the same precedence and associate left to right.  The +, - group has lower precendence than the *, / % group. 3 – 5 * 7 / / 2 3 – 35 / / 2 3 – / 2 3 –

Lectures on Numerical Methods13 Operators zArithmetic Operators ç% is a modulus operator. x % y results in the remainder when x is divided by y and is zero when x is divisible by y. çCannot be applied to float or double variables. çExample if ( num % 2 == 0 ) printf(“%d is an even number\n”, num)’; else printf(“%d is an odd number\n”, num);

Lectures on Numerical Methods14 Type Conversions çThe operands of a binary operator must have a the same type and the result is also of the same type. ç Integer division: c = (9 / 5)*(f - 32) The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9.0 / 5.0)*(f - 32) çIn case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. c = (9.0 / 5)*(f - 32) çIt is possible to force a conversion of an operand. This is called Explicit Type casting. c = ((float) 9 / 5)*(f - 32)

Lectures on Numerical Methods15 Automatic Type Casting 1.char and short operands are converted to int 2.Lower data types are converted to the higher data types and result is of higher type. 3.The conversions between unsigned and signed types may not yield intuitive results. 4.Example float f; double d; long l; int i; short s; d + f f will be converted to double i / s s will be converted to int l / i i is converted to long ; long result Hierarchy Double float long Int Short and char

Lectures on Numerical Methods16 Explicit Type Casting çThe general form of a type casting operator is ç(type-name) expression çIt is generally a good practice to use explicit casts than to rely on automatic type conversions. çExample C = (float)9 / 5 * ( f – 32 )  float to int conversion causes truncation of fractional part  double to float conversion causes rounding of digits  long int to int causes dropping of the higher order bits.

Lectures on Numerical Methods17 Precedence and Order of evaluation

Lectures on Numerical Methods18 Precedence and Order of evaluation

Lectures on Numerical Methods19 Operators zRelational Operators ç >=, ==, != are the relational operators. The expression operand1 relational-operator operand2 takes a value of 1(int) if the relationship is true and 0(int) if relationship is false. çExample int a = 25, b = 30, c, d; c = a < b; d = a > b; value of c will be 1 and that of d will be 0.

Lectures on Numerical Methods20 Operators zLogical Operators  &&, || and ! are the three logical operators.  expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero.  expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero.  !expr1 has a value 1 if expr1 is zero else 0. çExample çif ( marks >= 40 && attendance >= 75 ) grade = ‘P’ çIf ( marks < 40 || attendance < 75 ) grade = ‘N’

Lectures on Numerical Methods21 Operators zAssignment operators çThe general form of an assignment operator is çv op= exp çWhere v is a variable and op is a binary arithmetic operator. This statement is equivalent to çv = v op (exp)  a = a + b can be written as a += b  a = a * b can be written as a *= b  a = a / b can be written as a /= b  a = a - b can be written as a -= b

Lectures on Numerical Methods22 Operators zIncrement and Decrement Operators  The operators ++ and –- are called increment and decrement operators.  a++ and ++a are equivalent to a += 1.  a-- and --a are equivalent to a -= 1.  ++a op b is equivalent to a ++; a op b;  a++ op b is equivalent to a op b; a++; çExample Let b = 10 then (++b)+b+b = 33 b+(++b)+b = 33 b+b+(++b) = 31 b+b*(++b) = 132

Lectures on Numerical Methods23 Floating Point Arithmetic zRepresentation çAll floating point numbers are stored as çsuch that d 1 is nonzero. B is the base. p is the precision or number of significant digits. e is the exponent. All these put together have finite number of bits (usually 32 or 64 bits ) of storage. çExample çAssume B = 10 and p = 3. ç23.7 = E2 ç23.74 = E2 ç37000 = E5 ç37028 = E5 ç = E-4

Lectures on Numerical Methods24 Floating Point Arithmetic zRepresentation çS k = { x | B k-1 <= x < B k }. Number of elements in each Sk is same. In the previous example it is 900. çGap between seuccessive numbers of Sk is B k-p. çB1-p is called machine epsilon. It is the gap between 1 and next representable number. çUnderflow and Overflow occur when number cannot be represented because it is too small or too big. çTwo floating points are added by aligning decimal points. çFloating point arithmetic is not associative and distributive.