LESSON 3 Notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Pseudocode

CHAPTER THREE
Structured Programming, Pseudocode, • Pseudocode is structured english that is used as
an alternative method to flowcharts for planning
syntax, semantics, data types, structured programs.
expressions, statements, variable, input-
• Pseudo-code instructions are written in English,
output, control structures
• they can be easily understood and reviewed by
users.
• The only syntax rules to be concerned with
involve the LOOP and SELECTION structures.
• They must be used as CAPITALISED words.

Pseudocode: Example C Programming

• Write a pseudocode that would solve the area of a • In order to have a basic understanding of
parallelogram shown below programming methodology, we shall use C
6 cm
programming language.
• C is a general-purpose, procedural, imperative
4 cm

3 cm
computer programming language developed in 1972
by Dennis M. Ritchie at the Bell Telephone
Laboratories to develop the UNIX operating system.
C is the most widely used computer language.
•4 C is a powerful system programming language,
Lecturesand
on
Numerical
C++ is an excellent general purpose programming
Methods

language with modern bells and whistles.

1
Tokens in C Tokens in C
• Keywords • String Literals
• These are reserved words of the C language. For • A sequence of characters enclosed in double
example int, float, if, else, for, quotes as “…”. For example “13” is a string literal
while etc. and not number 13. ‘a’ and “a” are different.
• Identifiers • Operators
• An Identifier is a sequence of letters and digits, but • Arithmetic operators like +, -, *, / ,% etc.
must start with a letter. Underscore ( _ ) is treated • Logical operators like ||, &&, ! etc. and so on.
as a letter. Identifiers are case sensitive. Identifiers
are used to name variables, functions etc. • White Spaces
• Valid: Root, _getchar, __sin, x1, x2, • Spaces, new lines, tabs, comments ( A sequence of
x3, x_1, If characters enclosed in /* and */ ) etc. These are
• Invalid: 324, short, price$, My Name
Lectures on
used to separate the adjacent identifiers,Lectures
kewords on
5 Numerical 6 and constants. Numerical
• Constants Methods Methods

• Constants like 13, ‘a’, 1.3e-5 etc.

Basic Data Types


Basic Data Types
• Integral Types
• Integers are stored in various sizes. They can be
• Integral Types
signed or unsigned. • char Stored as 8 bits. Unsigned 0 to 255.
Signed -128 to 127.
• Example
Suppose an integer is represented by a byte (8 • short int Stored as 16 bits. Unsigned 0 to
bits). Leftmost bit is sign bit. If the sign bit is 0, the 65535.
Signed -32768 to 32767.
number is treated as positive.
Bit pattern 01001011 = 75 (decimal). • int Same as either short or long int.
The largest positive number is 01111111 = 27 – 1 =
• long int Stored as 32 bits. Unsigned 0 to
127.
4294967295.
Negative numbers are stored as two’s complement Signed -2147483648 to 2147483647
Lectures on Lectures on
7 or as one’s complement. Numerical 8 Numerical
Methods Methods
-75 = 10110100 (one’s complement).
-75 = 10110101 (two’s complement).

2
Constants
Basic Data Types • Numerical Constants
• Constants like 12, 253 are stored as int type.
• Floating Point Numbers No decimal point.
• Floating point numbers are rational numbers. • 12L or 12l are stored as long int.
Always signed numbers.
• 12U or 12u are stored as unsigned int.
• float Approximate precision of 6 decimal digits .
• 12UL or 12ul are stored as unsigned long int.
• Typically stored in 4 bytes with 24 bits of signed
mantissa and 8 bits of signed exponent. • Numbers with a decimal point (12.34) are stored as
• double Approximate precision of 14 decimal double.
digits. • Numbers with exponent (12e-3 = 12 x 10-3 ) are
• Typically stored in 8 bytes with 56 bits of signed stored as double.
mantissa and 8 bits of signed exponent. • 12.34f or 1.234e1f are stored as float.
Lectures on • These are not valid constants: Lectures on
9 • One should check the file limits.h to what Numerical
is 10 Numerical
implemented on a particular machine. Methods 25,000 7.1e 4 $200 2.3e-3.4 etc.
Methods

Constants
• Character and string constants Variables
• ‘c’ , a single character in single quotes are stored
as char.
Some special character are represented as two • Naming a Variable
characters in single quotes.
‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’
• Must be a valid identifier.
= double quotes. • Must not be a keyword
Char constants also can be written in terms of their • Names are case sensitive.
ASCII code.
‘\060’ = ‘0’ (Decimal code is 48). • Variables are identified by only first 32 characters.
• A sequence of characters enclosed in double • Library commonly uses names beginning with _.
quotes is called a string constant or string literal. For • Naming Styles: Uppercase style and Underscore
example style
“Charu”
“A” Lectures on • lowerLimitlower_limit Lectures on
11 Numerical 12 Numerical
“3/9” Methods • incomeTax income_tax Methods
“x = 5”

3
Declarations Global and Local Variables
/* Compute Area and Perimeter of a circle */

#include <stdio.h>

• Declaring a Variable
• Global Variables
float pi = 3.14159; /* Global */

• Each variable used must be declared. main() {

• These variables are float rad; /* Local */


• A form of a declaration statement is declared outside all
data-type var1, var2,…; functions. printf( “Enter the radius “ );

scanf(“%f” , &rad);

• Declaration announces the data type of a variable and allocates • Life time of a global
appropriate memory location. No initial value (like 0 for integers) should be variable is the if ( rad > 0.0 ) {

assumed. entire execution float area = pi * rad * rad;


float peri = 2 * pi * rad;
period of the
• It is possible to assign an initial value to a variable in the declaration itself. program. printf( “Area = %f\n” , area );

data-type var = expression; • Can be accessed by printf( “Peri = %f\n” , peri );

}
any function defined
• Examples else
below the printf( “Negative radius\n”);
int sum = 0; declaration, in a
file. printf( “Area = %f\n” , area );
char newLine = ‘\n’; Lectures on }
Lectures on
13 Numerical 14 Numerical
float epsilon = 1.0e-6; Methods Methods

Global and Local Variables /* Compute Area and Perimeter of a circle */ Operators
#include <stdio.h>
float pi = 3.14159; /* Global */

• Arithmetic Operators
• Local Variables
main() {
float rad; /* Local */
• +, - , *, / and the modulus operator %.
• These variables are
declared inside some
functions.
printf( “Enter the radius “ );
scanf(“%f” , &rad);
• + and – have the same precedence and associate left to
• Life time of a local right.
variable is the entire
3 – 5 + 7 = ( 3 – 5 ) + 7  3 – ( 5 + 7 )
if ( rad > 0.0 ) {
execution period of float area = pi * rad * rad;
the function in which
float peri = 2 * pi * rad;
it is defined. 3 + 7 – 5 + 2 = ( ( 3 + 7 ) – 5 ) + 2
• Cannot be accessed by
any other function.
printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri );
• *, /, % have the same precedence and associate left to
• In general variables } right.
declared inside a
block are accessible
else
• The +, - group has lower precendence than the *, / %
only in that block. printf( “Negative radius\n”);
group.
printf( “Area = %f\n” , Lectures
area ); on Lectures on
15 } Numerical 16 Numerical
Methods Methods

4
Operators Operators
• Arithmetic Operators
• % is a modulus operator. x % y results in the
3 – 5 * 7 / 8 + 6 / 2 remainder when x is divided by y and is zero when x
3 – 35 / 8 + 6 / 2 is divisible by y.

3 – 4.375 + 6 / 2 • Cannot be applied to float or double variables.


3 – 4.375 + 3 • Example
-1.375 + 3 if ( num % 2 == 0 )
printf(“%d is an even number\n”, num)’;
1.625
else
printf(“%d is an odd number\n”, Lectures
num);on
18 Numerical
Methods

Type Conversions Cont..

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

Lectures on
19 Numerical
Methods

5
Automatic Type Casting Explicit Type Casting
1. char and short operands are converted to int • The general form of a type casting operator is
2. Lower data types are converted to the higher data • (type-name) expression
types and result is of higher type.
• It is generally a good practice to use explicit casts than to
3. The conversions between unsigned and signed types Hierarchy rely on automatic type conversions.
may not yield intuitive results. Double • Example
4. Example float C = (float)9 / 5 * ( f – 32 )
float f; double d; long l;
long • float to int conversion causes truncation of fractional
int i; short s;
Int part
d + f f will be converted to double
Short and • double to float conversion causes rounding of digits
i / s s will be converted to int
char
Lectures on • long int to int causes dropping of the higher
Lecturesorder
on
21 l / i i is converted to long; long result Numerical 22 Numerical
Methods bits. Methods

Precedence and Order of evaluation Precedence and Order of evaluation

Lectures on Lectures on
23 Numerical 24 Numerical
Methods Methods

6
Operators
Operators
• Logical Operators
• Relational Operators • &&, || and ! are the three logical operators.
• <, <=, > >=, ==, != are the relational operators. The • expr1 && expr2 has a value 1 if expr1 and
expression expr2 both are nonzero.
operand1 relational-operator operand2 • expr1 || expr2 has a value 1 if expr1 and
takes a value of 1(int) if the relationship is true and 0(int) if expr2 both are nonzero.
relationship is false. • !expr1 has a value 1 if expr1 is zero else 0.
• Example
• Example
int a = 25, b = 30, c, d;
• if ( marks >= 40 && attendance >= 75 )
c = a < b; grade = ‘P’
d = a > b;
25
Lectures on
Numerical 26
• If ( marks < 40 || attendance Lectures
< 75 on)
Numerical
value of c will be 1 and that of d will be 0 . Methods grade = ‘N’ Methods

Operators
Operators • Increment and Decrement Operators
• The operators ++ and –- are called increment and
• Assignment operators decrement operators.
• The general form of an assignment operator is • a++ and ++a are equivalent to a += 1.
• v op= exp • a-- and --a are equivalent to a -= 1.
• Where v is a variable and op is a binary arithmetic • ++a op b is equivalent to a ++; a op b;
operator. This statement is equivalent to • a++ op b is equivalent to a op b; a++;
• v = v op (exp) • Example
• a = a + b can be written as a += b Let b = 10 then
• a = a * b can be written as a *= b (++b)+b+b = 33

• a = a / b can be written as a /= b b+(++b)+b = 33

• a = a - b can be written as a -= b b+b+(++b) = 31


Lectures on Lectures on
27 Numerical 28 b+b*(++b) = 132 Numerical
Methods Methods

7
Floating Point Arithmetic Floating Point Arithmetic
• Representation • Representation
• All floating point numbers are stored as  0.d1d 2  d p  B
e

• Sk = { x | Bk-1 <= x < Bk }. Number of elements in


• such that d1 is nonzero. B is the base. p is the each Sk is same. In the previous example it is 900.
precision or number of significant digits. e is the
• Gap between seuccessive numbers of Sk is Bk-p.
exponent. All these put together have finite number
of bits (usually 32 or 64 bits ) of storage. • B1-p is called machine epsilon. It is the gap
between 1 and next representable number.
Example
• Underflow and Overflow occur when number cannot
• Assume B = 10 and p = 3. be represented because it is too small or too big.
23.7 = +0.237E2
• Two floating points are added by aligning decimal
23.74 = +0.237E2 points.
37000 = +0.370E5
• Floating point arithmetic is not associative and
37028 = +0.370E5 Lectures on distributive. Lectures on
29 Numerical 30 Numerical
-0.000124 = -0.124E-4 Methods Methods

EXERCISE 3
a) Define the following terms citing example in each case.
i) Syntax (2 mark)
ii) Semantics (2 mark)
iii) Variable declaration (2 mark)
iv) Data type (2 mark)
v) Identifier (2 mark)
b) Rewrite the following mathematical statement in their equivalent
C statements:
i) c = 5 (f-32)
6 [1 Mark]
ii) T=3x2 + y
2 2
( x +100)(x ) [3 Marks]

You might also like