Professional Documents
Culture Documents
CHAPTER 1
INTRODUCTION TO COMPUTER
PROGRAMMING
(Part 2)
Lecturer: Mrs Nurul Izni Rusli
nurulizni@unimap.edu.my
UNICITI S2 BK5 Level 1
Outline
Pseudo code & flowchart
Sample programming question
Sample C program
Program debugging
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
Use algorithm
Convert to actual codes
begin
int main(void)
{
double base, height, area;
Variables
declaration
end
6
return 0 (int) to
OS
Comments
directives
int main ()
{
double r, area;
printf("Enter radius : ");
scanf("%lf", &r);
area=3.14*r*r;
printf("\nArea of the circle is : %lf\n", area);
return 0;
}
Program Comments
Starts with /* and terminates with */ OR
Character // starts a line comment, if several lines,
Preprocessor Directives
An instruction to pre-processor
Standard library header: <stdio.h>,<math.h>
E.g.
o #include <stdio.h>
for std input/output
o #include <stdlib.h>
conversion number-text vise-versa, memory
Size
(bytes)
double
bool
char
int
float
11
Description
float net_income;
double base, height, area; Declare and initialize
int index =0, count =0;
char ch=a, ch2;
const float epf = 0.1, tax = 0.05;
Named constant declared and initialized
12
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
13
Arithmetic Operators
Used to execute mathematical equations
The result is usually assigned to a data storage
14
Algebraic
Expression
C Expression
f+7
f+7
pc
p-c
Multipication
bm
b*m
Division
x/y
Addition
Subtraction
Remainder
(Modulus)
15
Arithmetic
Operator
r mod s
x/y
r%s
z=x%y
= 20 % 3
= 2 (remainder)
16
17
18
example:
i. b=8, c=10,
if ((b > 10) && (c<10))
ii. while ((b==8) || (c > 10))
iii. if ((kod == 1) && (salary > 2213))
19
20
exp1
exp2
false
false
false
false
true
false
true
false
false
true
true
true
21
exp1
exp2
exp1 || exp2
false
false
false
false
true
true
true
false
true
true
true
true
count -=1;
count--;
--count;
22
Unary Operators
Obviously operating on ONE operand
Commonly used unary operators
Increment/decrement { ++ , -- }
Arithmetic Negation { - }
Logical Negation { ! }
postfix
23
24
Unary Operators
Example
Increment/decrement { ++ , -- }
prefix:value incr/decr before used in expression
postfix:value incr/decr after used in expression
val=5;
Output:
val=5;
Output:
val=5;
Output:
val=5;
Output:
Logical Negation { ! }
bool isDinnerTime = true;
bool isLunchTime = !isDinnerTime;
25
Operator Precedence
26
Operators
Precedence
! + - (unary operators)
first
* / %
second
+ - (binary operators)
third
fourth
== !=
fifth
&&
sixth
||
seventh
last
27
28
29
30
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
31
32
33
PLT101 2013/2014
34
3
5
Pop Quiz
Q1: Debug the following program:
#include <stdio.h>
int main()
{
int A, B, ∑
printf ("input first integer \n");
scanf ("%d", &A)
printf ("input second integer \n");
scanf ("%d", &B)
sum = A + B;
printf ("Sum is %s\t", A+B);
return 0;
}
36
37
38
//discard &
//missing semicolon
// missing semicolon
//capital S
//%d for decimal(int)
39