UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Data types and variables
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
2440: 211 Interactive Web Programming Expressions & Operators.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Week 2 Introduction to Computer Programming/ C Programming Language 1 EKT120: Computer Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CISC105 – General Computer Science Class 2 – 6/7/2006.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
UniMAP Sem II-10/11EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm (Part 2)‏
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Week 1 – Introduction to Computer and Algorithm (Part 2)‏
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Basic Elements of C++.
Revision Lecture
By: Syed Shahrukh Haider
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Chapter 2 - Introduction to C Programming
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm

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

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

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

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

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

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.

“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

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.

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

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

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

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

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

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;

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 Addition C ExpressionAlgebraic Expression Arithmetic Operator C Operation

UniMAP Sem I-09/10EKT120: Computer Programming17 Exercise on Arithmetic Operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)‏

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

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

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

UniMAP Sem I-09/10EKT120: Computer Programming21 Truth Table for && (logical AND) Operator true false true falsetruefalse exp1 && exp2exp2exp1

UniMAP Sem I-09/10EKT120: Computer Programming22 Truth Table for || (logical OR) Operator true falsetrue false exp1 || exp2exp2exp1

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;

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

Comparison of Prefix and Postfix Increments

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

UniMAP Sem I-09/10EKT120: Computer Programming27 Operator Precedence last= seventh|| sixth&& fifth== != fourth = > third+ - second* / % first! + - PrecedenceOperators

Formatted Output with “printf” #include void main (void) { int month; float expense, income; month = 12; expense = 111.1; income = 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

UniMAP Sem I-09/10EKT120: Computer Programming29 Formatted Output with printf- cont

UniMAP Sem I-09/10EKT120: Computer Programming30 Formatted input with scanf

UniMAP Sem I-09/10EKT120: Computer Programming31 Formatted input with scanf- cont

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

UniMAP Sem I-09/10EKT120: Computer Programming33 Program debugging-syntax error snapshot

UniMAP Sem I-09/10EKT120: Computer Programming34 Program debugging-run time error snapshot

UniMAP Sem I-09/10EKT120: Computer Programming35 Program debugging-logic error snapshot

UniMAP Sem I-09/10EKT120: Computer Programming36 End Week 1 – Session 2 Q & A!