Download presentation
Presentation is loading. Please wait.
Published byDiane McCarthy Modified over 9 years ago
1
UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm
2
UniMAP Sem I-09/10EKT120: Computer Programming2 Outline Pseudo code & flowchart Sample programming question Sample C program Identifiers and reserve words Program comments Pre-processor directives Data types and type declarations Operators Formatted input and output Program debugging
3
UniMAP Sem I-09/10EKT120: Computer Programming3 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
4
Recall..Pseudo code and Flowchart Try develop the pseudo code and flowchart for the problem given in the previous slide. UniMAP Sem I-09/10EKT120: Computer Programming4
5
UniMAP Sem I-09/10EKT120: Computer Programming5 Sample C Program //Program name : program1.c //Programmer : Yasmin //This program calculates the area of triangle #include 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
6
“printf” In C language, “printf” command is used to display any message or output to the screen. The format of printf is: printf(“The text to be displayed”); The text that you want to display must be within the double quote “the text”.
7
Can send parameter to the printf function. It enables to display the dynamic value after done the data processing. Example : calculating mathematical problem and want to display the answer to the screen. To calculate area of triangle using the equation: area = ½*(base * height) : printf(“The area of triangle is = %f”, area); area is the variable that contains the answer value, and it is passed to the printf function. the symbol of % must be used to tell the printf function where to print the answer value.
8
“scanf” “scanf” is used to accept the user input from the keyboard. The command of scanf is as below: scanf (“%f”,&base); %f is the format of data type that will be entered. For example if the variable “base” is defined as float the format “f” is used The “%f” must be in the double quote. The symbol “&” must be used with scanf command. This is to tell the compiler the address of variable “base”, thus the keyed in data will be located to the address of “base” in the computer memory
9
UniMAP Sem I-09/10EKT120: Computer Programming9 Variables & Reserve 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 Reserve words cannot be variables/identifiers Reserve words already assigned to a pre-defined meaning e.g.: delete, int, main, include, double, for, if, etc.
10
UniMAP Sem I-09/10EKT120: Computer Programming10 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 /* /* */*/
11
UniMAP Sem I-09/10EKT120: Computer Programming11 Preprocessor Directives An instruction to pre-processor Standard library header (p154,Deitel) E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processing
12
UniMAP Sem I-09/10EKT120: Computer Programming12 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 4Floating-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) DescriptionData Type
13
UniMAP Sem I-09/10EKT120: Computer Programming13 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
14
UniMAP Sem I-09/10EKT120: Computer Programming14 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
15
UniMAP Sem I-09/10EKT120: Computer Programming15 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;
16
UniMAP Sem I-09/10EKT120: Computer Programming16 Arithmetic Operators r % s r mod s % Remainder (Modulus) x / y / Division b * m bm * Multipication p - c p – c - Subtraction f + 7 + Addition C ExpressionAlgebraic Expression Arithmetic Operator C Operation
17
UniMAP Sem I-09/10EKT120: Computer Programming17 Exercise on Arithmetic Operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)
18
UniMAP Sem I-09/10EKT120: Computer Programming18 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
19
UniMAP Sem I-09/10EKT120: Computer Programming19 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)
20
UniMAP Sem I-09/10EKT120: Computer Programming20 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))
21
UniMAP Sem I-09/10EKT120: Computer Programming21 Truth Table for && (logical AND) Operator true false true falsetruefalse exp1 && exp2exp2exp1
22
UniMAP Sem I-09/10EKT120: Computer Programming22 Truth Table for || (logical OR) Operator true falsetrue false exp1 || exp2exp2exp1
23
UniMAP Sem I-09/10EKT120: Computer Programming23 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;
24
UniMAP Sem I-09/10EKT120: Computer Programming24 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
25
Comparison of Prefix and Postfix Increments
26
UniMAP Sem I-09/10EKT120: Computer Programming26 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
27
UniMAP Sem I-09/10EKT120: Computer Programming27 Operator Precedence last= seventh|| sixth&& fifth== != fourth = > third+ - second* / % first! + - PrecedenceOperators
28
Formatted Output with “printf” #include 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 I-09/10EKT120: Computer Programming28 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
29
UniMAP Sem I-09/10EKT120: Computer Programming29 Formatted Output with printf- cont
30
UniMAP Sem I-09/10EKT120: Computer Programming30 Formatted input with scanf
31
UniMAP Sem I-09/10EKT120: Computer Programming31 Formatted input with scanf- cont
32
UniMAP Sem I-09/10EKT120: Computer Programming32 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
33
UniMAP Sem I-09/10EKT120: Computer Programming33 Program debugging-syntax error snapshot
34
UniMAP Sem I-09/10EKT120: Computer Programming34 Program debugging-run time error snapshot
35
UniMAP Sem I-09/10EKT120: Computer Programming35 Program debugging-logic error snapshot
36
UniMAP Sem I-09/10EKT120: Computer Programming36 End Week 1 – Session 2 Q & A!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.