Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 1 – Introduction to Computer and Algorithm (Part 2)‏

Similar presentations


Presentation on theme: "Session 1 – Introduction to Computer and Algorithm (Part 2)‏"— Presentation transcript:

1 Session 1 – Introduction to Computer and Algorithm (Part 2)‏

2 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 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
Flowchart Try develop the pseudo code and flowchart for the problem given in the previous slide. Pseudo code Begin Input variables; Area, base, height Calculate Area = 0.5 x base x height Print result of Area End

5 Preprocessor directives
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; } Comments Preprocessor directives begin The term void indicates we receive nothing from OS and return an integer to OS Variables declaration body return 0 (int) to OS end

6 C vs Assembly

7 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.

8 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 /* /* */*/

9 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

10 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 Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword). int A single character. Internally stored as a coded integer value (refer to ASCII table). char Size (bytes)‏ Description Data Type

11

12 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

13 Types of Operators Types of operators are:
Arithmetic operators (+ , - , * , / , %)‏ Relational operators (> , < , == , >= , <=, !=)‏; f=1 not same with f==1 Logical operators (&& , ||)‏ Compound assignment operators (+=, -=, *=, /=, %=)‏; a=a+1 same with a=+1 Binary operators: needs two operands ; A!=B Unary operators: single operand ; a+1 Bitwise operators: executes on bit level

14 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;

15 Arithmetic Operators r % s r mod s % x / y / b * m bm * p - c p – c -
Remainder (Modulus)‏ x / y / Division b * m bm * Multipication p - c p – c - Subtraction f + 7 + Addition C Expression Algebraic Expression Arithmetic Operator C Operation

16 Exercise on Arithmetic Operators
Given x = 20, y = 3 z = x % y; not same with z = x/y; = 20 % = 20/3 = 2 (remainder) = 6(division value)

17 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

18 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)‏

19 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))‏

20 Truth Table for && (logical AND) Operator
true false exp1 && exp2 exp2 exp1

21 Truth Table for || (logical OR) Operator
true false exp1 || exp2 exp2 exp1

22 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;

23 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

24 Comparison of Prefix and Postfix Increments

25 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

26 Operator Precedence last = seventh || sixth && fifth == != fourth
== != fourth < <= >= > third + - second * / % first ! + - Precedence Operators

27 Formatted Output with “printf”
#include <stdio.h> void main (void) { int month; float expense, income; month = 12; expense = 111.1; income = printf (“Month=%2d, Expense=$%9.2f\n”,month,expense); } 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 ‘,’ separates string literal from variable names Correspondence between variable names and %...in string literal

28 Formatted Output with printf-cont

29 Formatted input with scanf

30 Formatted input with scanf-cont

31 Program debugging Syntax error Run-time error Logic 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

32 Program debugging-syntax error snapshot

33 Program debugging-run time error snapshot

34 Program debugging-logic error snapshot

35 End of Session 2 Q & A!


Download ppt "Session 1 – Introduction to Computer and Algorithm (Part 2)‏"

Similar presentations


Ads by Google