You are on page 1of 33

UniMAP Sem II-09/10 EKT120: Computer Programming 1

Week 1 Introduction
to Computer and Algorithm
(Part 2)


UniMAP Sem II-09/10 EKT120: Computer Programming 2
Outline
Pseudo code & flowchart
Sample programming question
Sample C program
Identifiers and reserved words
Program comments
Pre-processor directives
Data types and type declarations
Operators
Formatted input and output
Program debugging

UniMAP Sem II-09/10 EKT120: Computer Programming 3
Sample Programming
Question
Write a program that calculates area of
triangle. Your program should read the
base length and the height length from
user. Given the formula to calculate the
area of triangle: 0.5 x (base) x (height).
Steps:
Analyze the problem
Use algorithm
Convert to actual codes

Recall..Pseudo code and
Flowchart
Try develop the pseudo code and flowchart for the
problem given in the previous slide.

UniMAP Sem II-09/10 EKT120: Computer Programming 4
UniMAP Sem II-09/10 EKT120: Computer Programming 5
Sample C Program
//Program name : program1.c
//Programmer : Yasmin
//This program calculates the area of triangle
#include <stdio.h>

int main(void)
{
double base, height, area;

printf(Enter base length : );
scanf(%f, &base);
printf(Enter height length : );
scanf(%f, &height);

area=0.5 * base * height;
printf(\nArea of the triangle is : %5.2f\n, area);
return 0;
}
The term void indicates we receive
nothing from OS and return an
integer to OS
Variables
declaration
begin
end
return 0 (int) to
OS
body

Comments

Preprocessor directives
UniMAP Sem II-09/10 EKT120: Computer Programming 6
Variables & Reserved Words
Identifiers/Variables
labels for program elements
case sensitive
can consist of capital letters[A..Z], small letters[a..z],
digit[0..9], and underscore character _
First character MUST be a letter or an underscore
No blanks
Reserved words cannot be variables/identifiers
Reserved words
already assigned to a pre-defined meaning
e.g.: delete, int, main, include, double, for, if, etc.
UniMAP Sem II-09/10 EKT120: Computer Programming 7
Program Comments
Starts with /* and terminates with */
OR
Character // starts a line comment, if
several lines, each line must begin with
//
Comments cannot be nested /* /* */*/
UniMAP Sem II-09/10 EKT120: Computer Programming 8
Preprocessor Directives
An instruction to pre-processor
Standard library header: <stdio.h>,<math.h>
E.g. #include <stdio.h>
for std input/output
#include <stdlib.h>
Conversion number-text vise-versa, memory
allocation, random numbers
#include <string.h>
string processing

UniMAP Sem II-09/10 EKT120: Computer Programming 9
Data Types & Memory
Allocation
1
Boolean representation of logic states. Can only be
assigned true (1) or false (0).
bool
8
A more precise version of float. Has larger dynamic
range and better representation of decimal points.
double
4 Floating-point number. Set of real numbers. float
4
Integer quantity. Can be represented in signed or
unsigned form (with the unsigned keyword).
int
1
A single character. Internally stored as a coded
integer value (refer to ASCII table).
char
Size
(bytes)
Description Data Type
UniMAP Sem II-09/10 EKT120: Computer Programming 10
Data Types Declaration
float income;
float net_income;
double base, height, area;
int index =0, count =0;
char ch=a, ch2;
const float epf = 0.1, tax = 0.05;
float income, net_income;
Declare and initialize
Named constant declared and initialized
UniMAP Sem II-09/10 EKT120: Computer Programming 11
Types of Operators
Types of operators are:
Arithmetic operators
(+ , - , * , / , %)
Relational operators
(> , < , == , >= , <=, !=)
Logical operators (&& , ||)
Compound assignment operators
(+=, -=, *=, /=, %=)
Binary operators: needs two operands
Unary operators: single operand
Bitwise operators: executes on bit level

UniMAP Sem II-09/10 EKT120: Computer Programming 12
Arithmetic Operators
Used to execute mathematical
equations
The result is usually assigned to a data
storage (instance/variable) using
assignment operator ( = )
E.g.
sum = marks1 + marks2;
UniMAP Sem II-09/10 EKT120: Computer Programming 13
Arithmetic Operators
r % s r mod s %
Remainder
(Modulus)
x / y x / y /
Division
b * m bm *
Multipication
p - c p c -
Subtraction
f + 7 f + 7 +
Addition
C Expression Algebraic
Expression
Arithmetic
Operator
C Operation
UniMAP Sem II-09/10 EKT120: Computer Programming 14
Exercise on Arithmetic Operators
Given x = 20, y = 3
z = x % y
= 20 % 3
= 2 (remainder)

UniMAP Sem II-09/10 EKT120: Computer Programming 15
Relational and Logical Operators
Previously, relational operator:
>, < >=, <=, == , !=
Previously, logical operator:
&&, ||
Used to control the flow of a program
Usually used as conditions in loops and
branches
UniMAP Sem II-09/10 EKT120: Computer Programming 16
More on relational operators
Relational operators use mathematical
comparison (operation) on two data, but give
logical output
e.g.1 let say b = 8, if (b > 10)
e.g.2 while (b != 10)
e.g.3 if (mark == 60) print (Pass);
Reminder:
DO NOT confuse == (relational operator)
with = (assignment operator)

UniMAP Sem II-09/10 EKT120: Computer Programming 17
More on logical operators
Logical operators are manipulation of
logic. For example:
i. b=8, c=10,
if ((b > 10) && (c<10))
ii. while ((b==8) || (c > 10))
iii. if ((kod == 1) && (salary > 2213))

UniMAP Sem II-09/10 EKT120: Computer Programming 18
Truth Table for &&
(logical AND) Operator
true true true
false false true
false true false
false false false
exp1 && exp2 exp2 exp1
UniMAP Sem II-09/10 EKT120: Computer Programming 19
Truth Table for ||
(logical OR) Operator
true true true
true false true
true true false
false false false
exp1 || exp2 exp2 exp1
UniMAP Sem II-09/10 EKT120: Computer Programming 20
Compound Assignment Operators
To calculate value from expression and store
it in variable, we use assignment operator (=)
Compound assignment operator combines
binary operator with assignment operator
E.g. val +=one; is equivalent to val = val + one;
E.g. count = count -1; is equivalent to
count -=1;
count--;
--count;
UniMAP Sem II-09/10 EKT120: Computer Programming 21
Unary Operators
Obviously operating on ONE operand
Commonly used unary operators
Increment/decrement { ++ , -- }
Arithmetic Negation { - }
Logical Negation { ! }
Usually using prefix notation
Increment/decrement can be both a
prefix and postfix
Comparison of Prefix and Postfix Increments
UniMAP Sem II-09/10 EKT120: Computer Programming
UniMAP Sem II-09/10 EKT120: Computer Programming 23
Unary Operators (Example)
Increment/decrement { ++ , -- }
prefix:value incr/decr before used in expression
postfix:value incr/decr after used in expression




Logical Negation { ! }
bool isDinnerTime = true;
bool isLunchTime = !isDinnerTime;
val=5;
printf (%d, ++val);
Output:
6
val=5;
printf (%d, --val);
Output:
4
val=5;
printf (%d, val++);
Output:
5
val=5;
printf (%d, val--);
Output:
5
UniMAP Sem II-09/10 EKT120: Computer Programming 24
Operator Precedence
last =
seventh ||
sixth &&
fifth == !=
fourth < <= >= >
third + - (binary operators)
second * / %
first ! + - (unary operators)
Precedence Operators
Formatted Output with printf
#include <stdio.h>
void main (void)
{
int month;
float expense, income;
month = 12;
expense = 111.1;
income = 1000.0
printf (Month=%2d, Expense=$%9.2f\n,month,expense);
}




UniMAP Sem II-09/10 EKT120: Computer Programming 25
Declaring variable (month) to be integer
Declaring variables (expense and
income) to be real
Assignment statements store numerical values
in the memory cells for the declared variables
Correspondence between variable names and
%...in string literal
, separates string literal from variable names
UniMAP Sem II-09/10 EKT120: Computer Programming 26
Formatted Output with printf-
cont
UniMAP Sem II-09/10 EKT120: Computer Programming 27
Formatted input with scanf
UniMAP Sem II-09/10 EKT120: Computer Programming 28
Formatted input with scanf-
cont
UniMAP Sem II-09/10 EKT120: Computer Programming 29
Program debugging
Syntax error
Mistakes caused by violating grammar of C
C compiler can easily diagnose during compilation
Run-time error
Called semantic error or smart error
Violation of rules during program execution
C compiler cannot recognize during compilation
Logic error
Most difficult error to recognize and correct
Program compiled and executed successfully but
answer wrong

UniMAP Sem II-09/10 EKT120: Computer Programming 30
Program debugging-syntax
error snapshot
UniMAP Sem II-09/10 EKT120: Computer Programming 31
Program debugging-run time
error snapshot
UniMAP Sem II-09/10 EKT120: Computer Programming 32
Program debugging-logic error
snapshot
UniMAP Sem II-09/10 EKT120: Computer Programming 33
End Week 1 Session 2
Q & A!

You might also like