Algorithms 1 Beginning Concepts
Algorithms 1 Beginning Concepts
3
Variables and Constants
• Named memory locations where some value is stored.
– e.g. length, width, height
• value of a constant is initially set and cannot be varied.
– e.g. pi = 3.14159…
• value stored in a variable may be changed during
execution of the program.
4
Data Types
• integer
– in C, short int and long
– in Pascal integer,
• real or floating point
– in C, float and double
– in Pascal Float,
• character
– in C, char
– in Pascal, Char
• string, often treated as a list of characters
– in C, char[] or char *
5
Data Types
• logical
• in C, 0 represents false, true is any non zero number
• in Pascal, Bool has 2 values True and False
• composite data types, defined by programmer
• in C use struct
• in Pascal use record
6
Data Types
7
8
Functions
• functions, usually come with the language in a ‘library’
• a function usually performs a well defined specific task
– e.g. square root function
– sqrt 4.0 gives 2.0
• are used as parts of instructions in a program
• programmer uses them as part of his solution, leader to
shorter programs
• an application domain has its own functions
9
10
11
Operators
12
Operator Precedence or Hierarchy
13
Expressions and Equations
14
Example
15
Exercises
16
Exercises
17
Exercises
18