UniMAP Sem II-10/11EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm (Part 2)‏

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

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.
Chapter 2: Introduction to C++.
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.
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.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
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
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Lecture3.
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 II-10/11EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm (Part 2)‏

EKT120: Computer Programming2 Outline Pseudo code & flowchart Sample programming question Sample C program Identifiers and reserved words Program comments Pre-processor directives Data types and type declarations Operators Formatted input and output Program debugging UniMAP Sem II-10/11

EKT120: 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 UniMAP Sem II-10/11

Recall..Pseudo code and Flowchart Try develop the pseudo code and flowchart for the problem given in the previous slide. EKT120: Computer Programming4 UniMAP Sem II-10/11

EKT120: Computer Programming5 Sample C Program /*Program name : program1.c Programmer : Yasmin This program calculates the area of triangle*/ #include int main(void)‏ { double dBase, dHeight, dArea; printf(“Enter base length : “); scanf(“%f”, &dBase); printf(“Enter height length : “); scanf(“%f”, &dHeight); dArea=0.5 * dBase * dHeight; printf(“\nArea of the triangle is : %5.2f\n”, dArea); 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 UniMAP Sem II-10/11

EKT120: Computer Programming6 Variables & Reserved 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 Reserved words cannot be variables/identifiers Reserved words already assigned to a pre-defined meaning e.g.: delete, int, main, include, double, for, if, etc. UniMAP Sem II-10/11

EKT120: Computer Programming7 UniMAP Sem II-10/11  An identifier for the data in the program  Hold the data in your program  Is a location (or set of locations) in memory where a value can be stored  A quantity that can change during program execution Variables & Reserved Words

Constants A constant is a named or unnamed value, which does not change during the program execution. Example: const double dPi= ; Const int iDegrees=360; Const char cQuit=‘q’; Unnamed constant are often called literals Eg: and 360 EKT120: Computer Programming8 UniMAP Sem II-10/11

EKT120: Computer Programming9 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 II-10/11

EKT120: Computer Programming10 Preprocessor Directives An instruction to pre-processor Standard library header:, E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processing UniMAP Sem II-10/11

EKT120: Computer Programming11 Data Types UniMAP Sem II-10/11  Data types determine the following:  Type of data stored  Number of bytes it occupies in memory  Range of data  Operations that can be performed on the data  Modifiers alter the meaning of the base type to more precisely fit a specific need  C supports the following modifiers along with data types:  short, long, signed, unsigned

EKT120: Computer Programming12 Data Types & Memory Allocation UniMAP Sem II-10/11 TypeBitsBytesRange Char or Signed Char to +127 Unsigned Char810 to +255 Int or Signed int324-2,147,483,648 to +2,147,483,647 Unsigned int3240 to +4,294,967,295 Short int or Signed short int162-32,768 to + +32,767 Unsigned short int1620 to +65,535 Long int or signed long int324-2,147,483,648 to +2,147,483,647 Unsigned long int3240 to +4,294,967,295 Float e-38 to 3.4 e+38 Double6481.7e-308 to 1.7e+308 Long Double6481.7e-308 to 1.7e+308

EKT120: Computer Programming13 Variables Naming Conventions UniMAP Sem II-10/11  Variable names should use Hungarian notation  Start with an appropriate prefix that indicates the data type  After the prefix, the name of variable should have ore or more words  The first letter of each word should be in upper case  The rest of the letter should be in lower case.  The name of variable should clearly convey the purpose of the variable

EKT120: Computer Programming14 Naming Variables According to Standards UniMAP Sem II-10/11 PrefixData TypeExample iint and unsigned intiTotalScore ffloatfAverageScore ddoubledHeight llong and unsigned longlFactorial csigned char and unsigned charcProductCode aiArray of integeraiStudentId afArray of floatafWeight adArray of doubleadAmount alArray of long integeralSample acArray of charactersacMaterial

EKT120: Computer Programming15 Data Types Declaration float fIncome; float fNet_income; double dBase, dHeight, dArea; int iIndex =0, iCount =0; char cCh=‘a’, cCh2; const float fEpf = 0.1, fTax = 0.05; float income, net_income; Declare and initialize Named constant declared and initialized UniMAP Sem II-10/11

EKT120: Computer Programming16 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 II-10/11

EKT120: Computer Programming17 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 II-10/11

EKT120: Computer Programming18 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 II-10/11

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

EKT120: Computer Programming20 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 II-10/11

EKT120: Computer Programming21 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 II-10/11

EKT120: Computer Programming22 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 II-10/11

EKT120: Computer Programming23 Truth Table for && (logical AND) Operator true false true falsetruefalse exp1 && exp2exp2exp1 UniMAP Sem II-10/11

EKT120: Computer Programming24 Truth Table for || (logical OR) Operator true falsetrue false exp1 || exp2exp2exp1 UniMAP Sem II-10/11

EKT120: Computer Programming25 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 II-10/11

EKT120: Computer Programming26 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 Sem II-10/11

Comparison of Prefix and Postfix Increments EKT120: Computer Programming UniMAP Sem II-10/11

EKT120: Computer Programming28 Unary Operators (Example)‏ Increment/decrement { ++, -- } prefix:value incr/decr before used in expression postfix:value incr/decr after used in expression 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 II-10/11

EKT120: Computer Programming29 Operator Precedence last= seventh|| sixth&& fifth== != fourth = > third+ - (binary operators) second* / % first! + - (unary operators) PrecedenceOperators UniMAP Sem II-10/11

Formatted Output with “printf” #include void main (void) { int iMonth; float fExpense, fIncome; iMonth = 12; fExpense = 111.1; fIncome = ; printf (“Month=%2d, Expense=$%9.2f\n”,iMonth,fExpense); } EKT120: Computer Programming30 Declaring variable (fMonth) to be integer Declaring variables (fExpense and fIncome) 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 II-10/11

Formatted Output with printf- cont printf (“Month= %2d, Expense=$ %9.2f \n”,iMonth, fExpense); %2d refer to variable iMonth value. %9.2f refer to variable fExpense value. The output of printf function will be displayed as UniMAP Sem II-09/10EKT120: Computer Programming31

EKT120: Computer Programming32 Formatted input with scanf UniMAP Sem II-10/11

EKT120: Computer Programming33 Formatted input with scanf- cont UniMAP Sem II-10/11

EKT120: Computer Programming34 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 II-10/11

EKT120: Computer Programming35 Program debugging-syntax error snapshot UniMAP Sem II-10/11

EKT120: Computer Programming36 Program debugging-run time error snapshot UniMAP Sem II-10/11

EKT120: Computer Programming37 Program debugging-logic error snapshot UniMAP Sem II-10/11

EKT120: Computer Programming38 End Week 1 – Session 2 Q & A! UniMAP Sem II-10/11