ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Similar documents
ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Operators in C. Staff Incharge: S.Sasirekha

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 4: Basic C Operators

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

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

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

Operators and Expressions:

Operators & Expressions

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Unit 3. Operators. School of Science and Technology INTRODUCTION

Department of Computer Science

Data Types and Variables in C language

C Programs: Simple Statements and Expressions

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

The Arithmetic Operators

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Expressions and operators

Prepared by: Shraddha Modi

Primitive Data Types: Intro

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

CS Programming I: Primitives and Expressions

Lecture 3 Tao Wang 1

H192 Midterm 1 Review. Tom Zajdel

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

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.

Chapter 3: Operators, Expressions and Type Conversion

Operators. Java operators are classified into three categories:

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Declaration and Memory

Arithmetic and Logic Blocks

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 2. Outline. Simple C++ Programs

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

Full file at

JAVA OPERATORS GENERAL

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS 135 Lab Assignments Week 1

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Only to be used for arranged hours. Order of Operations

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CIS 110: Introduction to Computer Programming

Structures, Operators

Lecture 3. More About C

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

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

CS 320: Concepts of Programming Languages

Data Handing in Python

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

ECEN 468 Advanced Logic Design

LECTURE 3 C++ Basics Part 2

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Chap 6 - Introduction to HDL (b)

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

ECET 264 C Programming Language with Applications

Chapter 2 Working with Data Types and Operators

Chapter 2 Basic Elements of C++

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Operators And Expressions

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

CSCE 110 PROGRAMMING FUNDAMENTALS

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CSc 225 Algorithms and Data Structures I Algorithm Analysis

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CMPT 125: Lecture 3 Data and Expressions

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Lecture 2:- Functions. Introduction

Math Glossary Numbers and Arithmetic

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

CS110: PROGRAMMING LANGUAGE I

by Pearson Education, Inc. All Rights Reserved.

Constants and Variables

Visual C# Instructor s Manual Table of Contents

9-1 GCSE Maths. GCSE Mathematics has a Foundation tier (Grades 1 5) and a Higher tier (Grades 4 9).

ANSI C Programming Simple Programs

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

GCSE Higher Revision List

Chapter 3. Numeric Types, Expressions, and Output

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 1. Types, Expressions, & Variables

Computational Physics

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Goals for This Lecture:

Number System. Introduction. Decimal Numbers

Transcription:

ENGI 1020 - Introduction to Computer Programming M A Y 2 8, 2 0 1 0 R E Z A S H A H I D I

Last class Last class we talked about the following topics: Constants Assignment statements Parameters and calling functions Expressions Mixed precision arithmetic Type casting

Some course administration Assignment 2 will be up tonight! Pair programming details make sure that both students are working on the assignments! Raju will be enforcing this!

A bit more on integer and double precision arithmetic In integer arithmetic, all operands are int types, and the output is always an int type. In double precision arithmetic, all operands are double types, and the output is always a double type. Recall that double precision arithmetic is used even if one of the operands is a double. So for example in the expression 2.5*x, double precision will be used even if x is of type int. See quadratic function example in Lecture Slides.

Rounding vs. Truncation See example in Lecture Slides. Truncation is integer part of x. Rounding can be the integer part of x plus 1. /** truncate ******************************* * * @params: x - any double * * @returns: truncated integer version of x *******************************************/ int truncate (double x){ return x; }

Rounding example /** round ******************************* * * @params: x - any double * * @returns: rounded integer version of x *******************************************/ int round (double x){ return (x+0.5); }

Composite Assignment Operators These are shorthand for assignment operators where the same variable appears on both the left-hand side and the right-hand side. E.g. instead of x=x+5, we would write x+= 5. Similarly holds for the other binary operators, subtraction, multiplication and division. These are assignments and not equations, otherwise they would have no solution! Later we see there are special shorthand notations for x=x+1 and x =x-1.

Order of evaluation in compound expressions Order of evaluation: Expressions in parentheses, (), are always evaluated first, e.g. (5+3)*7. Then the order of operations is as follows: unary -,+ Highest (evaluated first) *, /, % -, + Lowest (evaluated last) See example in Lecture Notes

Library functions Libraries are functions that have already been written and built. So often we do not have to write our own functions, which makes code easier to understand and write. Many common functions already included in libraries. Need to use the #include directive to include the appropriate header file.

Library functions Header files contain the declarations needed to use the libraries. Math library has declarations for: Trigonometric functions e.g. double sin(double x), double tan(double x). Hyperbolic functions. e.g. double atan(double x) The power function which has to be used since C++ has no exponentiation operator: double pow(double x, double exp) The special function double atan2(double y, double x) which returns the arc tangent of y/x, a function which is defined for x = 0.

Library Functions To use any of these functions you would add to the top of your file the line #include <cmath> This is an include directive and the # sign means that this is processed by the precompiler. Recall our previous discussion on this.

Calling functions vs. implementing functions Thus far in the course, we have been implementing functions, e.g. in the assignments. When we use libraries, we are in fact calling functions that have already been implemented. Now we become a client (using a service), instead of providing a service for somebody else. We can call functions inside of one another (see polar/rectangular coordinates example in Lecture notes)

E.g. Dot product and Cross product Dot (scalar) product between two vectors a and b is a b. Cross product between two vectors a and b is a x b. The magnitude of a b is ab cos(theta). The magnitude of axb is ab sin(theta). How would we code functions to compute these magnitudes?

Next class Start control structures, where the flow of the program can change. Up to now, we have had a main function where statements are executed in sequence, possibly with calls to functions. Next, we will look at executing examples where some statements will be executed conditionally. We will also look at how we can repeat certain statements more than once, etc.