Lecture 2
Lecture 2
LECTURE TWO
1. Problem Definition
2. Problem Analysis
3. Algorithm design and representation
(Pseudocode or flowchart)
4. Coding and debugging
In order to understand the basic steps in solving a
problem on a computer, let us define a single
problem that we will solve step-by-step as we
discuss the problem solving methodologies in detail.
Problem Definition
Problem Analysis
After the problem has been adequately defined,
the simplest and yet the most efficient
and effective approach to solve the problem must
be formulated. Usually, this step involves
breaking up the problem into smaller and simpler
sub problems.
Example 1;
Determine the number of times a name occurs in a list
Input to the program: list of names, name to look for
Terminal Symbol
Represents the beginning, the end,
or a point of interruption or delay in
a program.
Connector Symbol
Represents any entry from, or exit
to, another part of the flowchart.
Also serves as an off-page
connector.
Flowline Symbol Represents the sequence of available
information and executable operations.
The lines connect other symbols, and
the arrowheads are mandatory only
for right-to-left and bottom-to -top
flow.
Annotation Symbol
Represents the addition of descriptive
information, comments, or explanatory
notes as clarification. The vertical line and
the broken line may be placed on the left,
as shown, or on the
Coding and Debugging
After constructing the algorithm, it is now possible
to create the source code. Using the
algorithm as basis, the source code can now be
written using the chosen programming
language.
Most of the time, after the programmer has
written the program, the program isn't 100%
working right away. The programmer has to add
some fixes to the program in case of errors (also
called bugs) that occurs in the program. This
process of is called debugging.
TYPES OF ERRORS
There are two types of errors. The first one is compile-time
error, and the other is runtime error.
Compile-Time Errors occur if there is a syntax error
in the code. The compiler will detect the error and the
program won't even compile. At this point, the programmer
is unable to form an executable that a user can run until the
error is fixed.
Forgetting a semi-colon at the end of a statement or
misspelling a certain command, for example, is a compile-
time error. It's something the compiler can detect as an error.
Compilers aren't perfect and so can't catch all errors at
compile time.
This is especially true for logic errors such as infinite loops.
This type of error is called runtime error.
Number Systems and Conversions
Numbers can be represented in a variety of
ways. The representation depends on what is
called the BASE.
The following are the four most common
representations.
1. DECIMAL
We normally represent numbers in their decimal
form. Numbers in decimal form are in base 10.
This means that the only digits that appear are
0-9.
Here are examples of numbers written in
decimal form:
12610 (normally written as just 126)
2. BINARY
Numbers in binary form are in base 2. This means that the
only legal digits are 0 and 1.
We need to write the subscript 2 to indicate that the
number is a binary number. Here are examples of
numbers written in binary form: (1) 11111102 (2)
10112
3. OCTAL
Numbers in octal form are in base 8. This means that the
only legal digits are 0-7. We
need to write the subscript 8 to indicate that the number is
an octal number. Here are
examples of numbers written in octal form:
(1) 1768 (2) 138
4. HEXADECIMAL
Numbers in hexadecimal form are in base 16.
This means that the only legal digits are 0-9
and the letters A-F (or a-f, lowercase or
uppercase does not matter).
We need to write the subscript 16 to indicate
that the number is a hexadecimal number.
Here are examples of numbers written in
hexadecimal form:7E16, B16
Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal Equivalent: 0 1 2 3 4 5 6 7 8 9 10 11 12
13 14 15
Conversions
1. Decimal to Binary / Binary to Decimal
Example 1:
12610 = ? 2
QUOTIENT REMAINDER
126 / 2 = 63 0
63 / 2 = 31 1
THIS WAY
WRITE IT
31 / 2 = 15 1
15 / 2 = 7 1
7/2= 3 1
3/2= 1 1
1/2= 1
So, writing the remainders from the bottom up,
we get the binary number 11111102
For Example:
11111102 = ? 10
Position 6543210
Binary
Digits 1111110
0
0x2=0
1
1x2=2
2
1 x 23= 4
1 x 2= 8
4
1 x 2= 16
5
1 x 2 = 32
6
1 x 2 = 64
TOTAL: 126
Decimal to Octal (or Hexadecimal)/Octal (or
Hexadecimal) to Decimal
Example2. (Octal):
12610 = ? 8
QUOTIENT REMAINDER
126 / 8 = 15 6
15 / 8 = 1 7
1/8= 1
For Example (Hexadecimal):
12610 = ? 16
THIS WAY
WRITE IT
126 / 16 = 7 14 (equal to hex
digit E)
7 / 16 = 7
7E16
Converting octal or hexadecimal numbers to
decimal is also the same as converting binary
numbers to decimal.
Binary to Octal / Octal to Binary
To convert from binary numbers to octal, we
partition the binary number into groups of 3
digits (from right to left), and pad it with zeros
if the number of digits is not divisible by 3.
We then convert each partition into its
corresponding octal digit. The following is a
table showing the binary representation of
each octal digit.
Octal Digit Binary Representation
Octa digit Binary representation
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
For Example:
11111102 = ? 8
001 111 110
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
For Example:
11111102 = ? 16
0111 1110
7 E
Equivalent Hexadecimal
number
QUESTIONS
EASY ONES
ONLY
Exercises
Writing Algorithms
Given the following set of tasks, create an algorithm to
accomplish the tasks.
You may write your algorithms using pseudocode or you
can use flowcharts.
1. Producing
2. Logging into your laboratory's computer
3. Getting the average of three numbers
Number Conversions
Convert the following numbers:
1. 198010 to binary, hexadecimal and octal
2. 10010011012 to decimal, hexadecimal and octal
3. 768 to binary, hexadecimal and decimal
4. 43F16 to binary, decimal and octal
INTRODUCTION TO C
• C is a general purpose, structured programming
Language.
• It consist of terms that resembles algebraic
expressions, augmented by certain English
keywords such as if ,else, for do while etc.
• C also contains certain additional features that
allows it to be used at lower level, thus bridging the
gap between the machine language and the High
level language.
• C is highly flexible, thus making it suitable for
system programming as well as application
programming.
29
29
HISTORY OF C
30
30
More on C Language
Many programming languages have been fully or
partially written in C including C++, Java, C#, Perl,
Python, PHP and many more.
34
34
C Characters
short
Long
Unsigned
Signed
CONSTANTS
There are basically four(4) types of constants in C
The integer constants -Numeric constants
Floating point -Numeric constants
Character constants and the string constant
Enumerated types are created using the enum
keyword. They are used to assign integer values to the
words which can be used later in the program
X={}
first element X[1], second element X[2]
There are many ways the array can be represented by the one
dimensional may be ok for our level now.
Declarations
Declarations basically associates groups of the variables with specific
data types. (allocate memory)
Makes use of variables , data types .
All variables must be declared before they can appear or used in
executed statements .
The data type decides the type and range of values that a variable can
store.
STATEMENTS (compound)
The individual statements may themselves be expression statement,
compound statements or even control statements.
The compound statement provides a capability for embedding
statements within other statement.
Unlike the expression statements, a compound statement does not end
with a semi colon.
Examples
{
pi=3.142;
circum=2.*pi* rad;
area=pi*rad*rad;
}
statements
STATEMENTS
CONTROL STATEMENT
This statement basically the most important of all statements as it help in
creating special program features such as logical test, loops and branches .
The for loop, while loop, if-else ,, etc are used to form the control statement.
SYMBOLIC CONSTANTS
Symbolic constant is a name that substitutes for a sequence of characters.
The characters may represent a numeric constant , a character constant or a
string constant.
A symbolic constant allows a name to appear in place of a numeric constant
or character.
Symbolic constant are usually defined at the beginning of a program.
Syntax : #define name text
#define taxrate 0.23
#define passrate 100
QUIZ
OPERATORS AND EXPRESSIONS
Arithmetic Operators
There are FIVE arithmetic Operators in C
Arithmetic Operators
Operands acted upon by arithmetic operators must represent NUMERIC
values.
Operands can be of any data type : float integer, characters etc.
% : requires that both operand must be integers and the second must be
nonzero
/: the second operator must not be zero.
Examples: a=20 , b =8
a+b
a-b
a*b
a%b
a+b*2
(a+b)*2 note the precedence between + and *(higher precedence)
OPERATORS AND EXPRESSIONS
Arithmetic Operators
Examples: a=10, b =-4 but if a=-10 , b=4 modulus will be -2
a+b
a-b
a*b
a%b =2
a/b =-2
a= ((a/b)*b) + (a%b) ?
OPERATORS AND EXPRESSIONS
Arithmetic Operators
Some rules applies to when performing operation on unsigned
operators
SUMMARY OF THE RULES
int (char, short or long)+float=float
float+double=double
Float+long double=long double
double+long double=long double
int+double=double
Int+long int=long int
short int +int = int
OPERATORS AND EXPRESSIONS
Arithmetic Operators
The value of an expression can be converted to a different data type
if desired . To do so the expression must be preceded by the name of
the desired data type enclosed in parentheses.
Example
If i= 7 (integer) , and f= 8.5 (float)
(i+f)%4 /* invalid because the i+f is a float , thus modulus may not
be possible */
Arithmetic Operators
Precedence : the order of evaluation. The operation with a higher
precedence is carried out first before those of lower precedence . But
this precedence can be altered through the use of parentheses.
UNARY OPERATORS
Unary operators are operators that act upon a single operand to
produce a new value.
Unary operators usually precede their single operand, though some
unary operators are written after their operands
OPERATORS AND EXPRESSIONS
UNARY Operators
Unary Minus : (-) formed when a numerical constant , variable or
expression is preceded by a minus sign.
Note : unary minus is completely different from subtraction.
Example
-743
-root1
-(x+y)
-3*(x+y)
UNARY Operators
if i=5 , so the expression ++1 , which is equivalent to
i=i+1 = 6
Similarly -- 1 , is equivalent to
i=i-1= 4.
e.g
a=20, b=8;
c=a++ +b;
c=--a+b;
Sizeof : the sizeof is also a unary operator, it returns the size of its
operand in byte. It is usually used in form of a cast .
Logical Operators
This operators represents condition that are either true or false.
The resulting expression will be of type integer since true is represented
by the integer value 1 and false by integer 0
OPERATORS AND EXPRESSIONS
Logical Operators
Lets see some Logical operations
These Logical operators act upon operands that are themselves logical
expressions. The overall resultant is to combine the individual logic
expressions into more complex conditions that are either true or false.
The result of a logical AND operation will be TRUE if both operands
are true .
While the result of a logical OR operation will be TRUE if any of the
operands is true.
i=7 , f=5.5
(f<11)||(i>100)
(f>11)&&(i>100)
OPERATORS AND EXPRESSIONS
Another logical operator is the logical Negation (Logical NOT ). We can see that it is a unary
operator.
Identifier =expression
Conditional Operators (? : )
When an expression make use of these operators it is known as
Conditional expression
Conditional Operators (? : )
example
(i<0) ? 0 : 100