UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
-
Upload
lenard-perry -
Category
Documents
-
view
235 -
download
4
Transcript of UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
UniMAP Sem1-07/08
EKT120: Computer Programming
1
Week2
UniMAP Sem1-07/08
EKT120: Computer Programming
2
Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Operators Formatted input and output Program debugging
UniMAP Sem1-07/08
EKT120: Computer Programming
3
Sample Programming Question
• Write a program that calculate nett income .Your program should read income from user. Given tax rate is 10% and epf rate is 5% from income.
• Steps: Analyze the problem Use algorithm Convert to actual codes
UniMAP Sem1-07/08
EKT120: Computer Programming
4
Sample C Program//Program name : program1.c//Programmer : Yasmin//This program reads income and calculate nett income //after epf and tax deduction#include <stdio.h>
int main(void){
float income, net_income;const float epf=0.1, tax=0.05;
printf(“Enter income : “);scanf(“%f”, &income);net_income=income-(epf*income) – (tax*income);printf(“\nNett income : %5.2f”, net_income);return 0;
}
The terms void indicates we receive nothing from OS and
return an integer to OSVariable
&constant
declaration
begin
end
Return 0(int) to OS
body
Comments
Preprocessor directives
UniMAP Sem1-07/08
EKT120: Computer Programming
5
Identifiers & Reserve Words Identifiers
labels for program elements case sensitive can consists 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 Reserve words cannot be identifiers
Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if etc.
UniMAP Sem1-07/08
EKT120: Computer Programming
6
Program comments
Starts with /* and terminate with */ OR
Character // start a line comment, if several lines, each line must begin with //
Comments cannot be nested /* /* */*/
UniMAP Sem1-07/08
EKT120: Computer Programming
7
Preprocessor directives An instruction to pre-processor Standard library header (p154,Deitel) 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 Sem1-07/08
EKT120: Computer Programming
8
Data Types & Mem. Alloc.
Data Type
DescriptionSize (byte
s)
charA single character. Internally stored as a coded integer value (refer to ASCII table).
1
intInteger quantity. Can be represented in signed or unsigned form (with the unsigned keyword).
4
float Floating-point number. Set of real numbers. 4
doubleA more precise version of float. Has larger dynamic range and better representation of decimal points.
8
boolBoolean representation of logic states. Can only be assigned true (1) or false (0).
1
UniMAP Sem1-07/08
EKT120: Computer Programming
9
Type declarations
float income;float net_income;
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 Sem1-07/08
EKT120: Computer Programming
10
Types of operators Types of operators are:
Arithmetic operators (+ , - , * , / , %)
Relational operators (> , < , == , >= , <=, !=)
Logical operators (&& , ||) Compound assignment operator
(+=, -=, *=, /=, %=) Binary operators: needs two operands Unary operators: single operand Bitwise operators: executes on bit level
UniMAP Sem1-07/08
EKT120: Computer Programming
11
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 Sem1-07/08
EKT120: Computer Programming
12
Arithmetic OperatorsC Operation Arithmetic
OperatorAlgebraic expression
C expression
Addition + f + 7 f + 7
Subtraction - p – c p - c
Multipication * bm b * m
Division / x / y x / y
Remainder(Modulus)
% r mod s r % s
UniMAP Sem1-07/08
EKT120: Computer Programming
13
Exercise on arithmetic operators
Given x = 20, y = 3 z = x % y = 20 % 3
= 2 (remainder)
UniMAP Sem1-07/08
EKT120: Computer Programming
14
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 Sem1-07/08
EKT120: Computer Programming
15
More on relational operators Relational operators use mathematical
comparison (operation) on two data, but gives logical outpute.g1 let say b = 8, if (b > 10)e.g2 while (b != 10)e.g3 if(kod == 1) print(“Pegawai”);
Reminder:Don’t confuse == (relational op.) with = (assignment op.)
UniMAP Sem1-07/08
EKT120: Computer Programming
16
More on logical operators
Logical operators are manipulation of logice.g1 let say b=8, c=10,
if ((b > 10) && (c<10))e.g2 while ((b==8) ||(c > 10))e.g3 if ((kod == 1) && (salary > 2213))
UniMAP Sem1-07/08
EKT120: Computer Programming
17
Truth table for &&(logical AND) operator
exp1 exp2 exp1 && exp2
false false false
false true false
true false false
true true true
UniMAP Sem1-07/08
EKT120: Computer Programming
18
Truth table for ||(logical OR) operator
exp1 exp2 exp1 || exp2
false false false
false true true
true false true
true true true
UniMAP Sem1-07/08
EKT120: Computer Programming
19
Compund assignment operator To calculate value from expression and
store it in variable, we use assignment operator (=)
Compound assignment operator combine 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 Sem1-07/08
EKT120: Computer Programming
20
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
UniMAP Sem1-07/08
EKT120: Computer Programming
21
Unary Operators (Eg.) 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:
4val=5;
printf(“%d”, val++);
Output:
5
val=5;
printf(“%d”, val--);
Output:
5
UniMAP Sem1-07/08
EKT120: Computer Programming
22
Operator PrecedenceOperators Precedence
! + - first
* / % second
+ - third
< <= >= > fourth
== != fifth
&& sixth
|| seventh
= last
UniMAP Sem1-07/08
EKT120: Computer Programming
23
Formatted Output with printf
UniMAP Sem1-07/08
EKT120: Computer Programming
24
Formatted Output with printf-cont
UniMAP Sem1-07/08
EKT120: Computer Programming
25
Formatted input with scanf
UniMAP Sem1-07/08
EKT120: Computer Programming
26
Formatted input with scanf-cont
UniMAP Sem1-07/08
EKT120: Computer Programming
27
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 Sem1-07/08
EKT120: Computer Programming
28
Program debugging-syntax error snapshot
UniMAP Sem1-07/08
EKT120: Computer Programming
29
Program debugging-run time error snapshot
UniMAP Sem1-07/08
EKT120: Computer Programming
30
Program debugging-logic error snapshot
UniMAP Sem1-07/08
EKT120: Computer Programming
31
End Week 1 – Session 2
Q & A!