Week 2 Introduction to Computer and Algorithm (Part 2)

Similar documents
Unit 3. Operators. School of Science and Technology INTRODUCTION

A flow chart is a graphical or symbolic representation of a process.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Differentiate Between Keywords and Identifiers

Programming in C++ 5. Integral data types

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

Operators in C. Staff Incharge: S.Sasirekha

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

UNIT- 3 Introduction to C++

Fundamentals of Programming

Chapter 3: Operators, Expressions and Type Conversion

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Computers Programming Course 6. Iulian Năstac

Operators and Expressions:

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Chapter 2: Overview of C. Problem Solving & Program Design in C

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Fundamental of Programming (C)

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Variables and Operators 2/20/01 Lecture #

Full file at C How to Program, 6/e Multiple Choice Test Bank

Objectives. In this chapter, you will:

Operators & Expressions

Operators in java Operator operands.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Chapter 2 Working with Data Types and Operators

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming for Engineers Introduction to C

Data Types and Variables in C language

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

LECTURE 02 INTRODUCTION TO C++

Data types, variables, constants

Lecture 3. More About C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C++ Programming: From Problem Analysis to Program Design, Third Edition

Basics of Programming

Basic Operations jgrasp debugger Writing Programs & Checkstyle

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

3. Types of Algorithmic and Program Instructions

Chapter 2: Introduction to C++

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

3. Java - Language Constructs I

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Chapter 2: Basic Elements of C++

Computer System and programming in C

DEPARTMENT OF MATHS, MJ COLLEGE

JAVA OPERATORS GENERAL

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

ANSI C Programming Simple Programs

UNIT 3 OPERATORS. [Marks- 12]

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Programming for Engineers Iteration

These are reserved words of the C language. For example int, float, if, else, for, while etc.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CS102: Variables and Expressions

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Arithmetic Operators. Portability: Printing Numbers

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Chapter 2: Overview of C++

2. Numbers In, Numbers Out

Chapter 4: Basic C Operators

Chapter 1 & 2 Introduction to C Language

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computers Programming Course 5. Iulian Năstac

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Outline. Data and Operations. Data Types. Integral Types

Fundamentals of Programming Session 4

CHAPTER 3 BASIC INSTRUCTION OF C++

Expressions and Precedence. Last updated 12/10/18

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Operators. Java operators are classified into three categories:

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Arithmetic Expressions in C

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

A First Program - Greeting.cpp

Programming and Data Structures

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

The C++ Language. Arizona State University 1

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Full file at

Operators And Expressions

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 2: Using Data

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Reserved Words and Identifiers

Lecture 3 Tao Wang 1

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Transcription:

Week 2 Introduction to Computer and Algorithm (Part 2) UniMAP Sem II-11/12 DKT121 1

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 2

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 3

Recall..Pseudo code and Flowchart Try develop the pseudo code and flowchart for the problem given in the previous slide. 4

Sample C Program begin Variables declaration end /*Program name : program1.c Programmer : Yasmin This program calculates the area of triangle*/ #include <stdio.h> 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; return 0 (int) to OS Preprocessor directives Comments The term void indicates we receive nothing from OS and return an integer to OS body 5

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

Variables & Reserved Words 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 7

Constants A constant is a named or unnamed value, which does not change during the program execution. Example: const double dpi=3.141592 Const int idegrees=360; Const char cquit= q ; Unnamed constant are often called literals Eg: 3.141592 and 360 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: <stdio.h>,<math.h> 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 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 11

Data Types & Memory Allocation Type Bits Bytes Range Char or Signed Char 8 1-128 to +127 Unsigned Char 8 1 0 to +255 Int or Signed int 32 4-2,147,483,648 to +2,147,483,647 Unsigned int 32 4 0 to +4,294,967,295 Short int or Signed short int 16 2-32,768 to + +32,767 Unsigned short int 16 2 0 to +65,535 Long int or signed long int 32 4-2,147,483,648 to +2,147,483,647 Unsigned long int 32 4 0 to +4,294,967,295 Float 32 4 3.4 e-38 to 3.4 e+38 Double 64 8 1.7e-308 to 1.7e+308 Long Double 64 8 1.7e-308 to 1.7e+308 12

Variables Naming Conventions 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 13

Naming Variables According to Standards Prefix Data Type Example i int and unsigned int itotalscore f float faveragescore d double dheight l long and unsigned long lfactorial c signed char and unsigned char cproductcode ai Array of integer aistudentid af Array of float afweight ad Array of double adamount al Array of long integer alsample ac Array of characters acmaterial 14

Data Types Declaration float fincome; float fnet_income; double dbase, dheight, darea; int iindex =0, icount =0; char cch= a, cch2; float income, net_income; Declare and initialize const float fepf = 0.1, ftax = 0.05; Named constant declared and initialized 15

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 16

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

Arithmetic Operators C Operation Arithmetic Operator Algebraic 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 18

Exercise on Arithmetic Operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder) 19

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 20

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

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

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

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

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

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 26

Comparison of Prefix and Postfix Increments 27

Unary Operators (Example) Increment/decrement { ++, -- } prefix:value incr/decr before used in expression postfix:value incr/decr after used in expression val=5; Output: val=5; Output: printf ( %d, ++val); 6 printf ( %d, --val); 4 val=5; Output: val=5; Output: printf ( %d, val++); 5 printf ( %d, val--); 5 28

Operator Precedence Operators! + - (unary operators) * / % + - (binary operators) < <= >= > ==!= && = Precedence first second third fourth fifth sixth seventh last 29

Formatted Output with printf #include <stdio.h> void main (void) { } int imonth; float fexpense, fincome; imonth = 12; fexpense = 111.1; fincome = 1000.0; Declaring variable (imonth) to be integer Declaring variables (fexpense and fincome) to be real Assignment statements store numerical values in the memory cells for the declared variables, separates string literal from variable names printf ( Month=%2d, Expense=$%9.2f\n,iMonth,fExpense); Correspondence between variable names and %...in string literal 30

Formatted Output with printf 31

Formatted Input with scanf 32

Formatted Input with scanf 33

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 34

Program Debugging-syntax error snapshot 35

Program Debugging-run time error snapshot 36

Program Debugging-logic error snapshot 37

End Week 2 Session 2 Q & A! 38