Introduction to Programming II W4260. Lecture 2

Similar documents
Chapter 4: Basic C Operators

Operators and Expressions:

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

W4260: Modeling the Universe. Lecture 4

Expressions. Eric McCreath

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

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

Learning to Program with Haiku

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Lecture 3 Operators MIT AITI

Add Subtract Multiply Divide

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

CS 320: Concepts of Programming Languages

Logical and Bitwise Expressions

Here n is a variable name. The value of that variable is 176.

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Lecture 5: Making Decisions

Operators & Expressions

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Chapter 2, Part III Arithmetic Operators and Decision Making

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

CSE 115. Introduction to Computer Science I

Introduction to Programming

ENGR 101 Engineering Design Workshop

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to 8086 Assembly

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

MATH 353 Engineering mathematics III

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

CIS 110: Introduction to Computer Programming

bool bool - either true or false

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Basic stuff -- assignments, arithmetic and functions

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

An Introduction to MATLAB

Fundamental of Programming (C)

L-System Fractal Generator: Language Reference Manual

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

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

ANSI C Programming Simple Programs

Announcements. Homework 0 out! Coming soon: 1. remote access 2. programming instructions 3. lab 1 (graded!) this week

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

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

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Decision Making -Branching. Class Incharge: S. Sasirekha

Basic Assignment and Arithmetic Operators

Functions with Parameters and Return Values

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Fundamentals of Programming

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Operators in C. Staff Incharge: S.Sasirekha

JAVA OPERATORS GENERAL

REVIEW. The C++ Programming Language. CS 151 Review #2

Expressions and operators

Chapter 1. Numeric Artifacts. 1.1 Introduction

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

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Functions. Systems Programming Concepts

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

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.

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introduction to Engineering gii

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Computer Programming CS F111

Programming for Engineers in Python. Autumn

Operators. Lecture 3 COP 3014 Spring January 16, 2018

n Group of statements that are executed repeatedly while some condition remains true

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Lecture 3 Tao Wang 1

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

CSC-140 Assignment 5

Variables and Operators 2/20/01 Lecture #

Functions. Arash Rafiey. September 26, 2017

Lecture 2 Algorithms with numbers

Learning the Language - V

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Chapter 17. Fundamental Concepts Expressed in JavaScript

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

SECTION II: LANGUAGE BASICS

CONTROL AND HIGHER ORDER FUNCTIONS 1

Transcription:

Introduction to Programming II W4260 Lecture 2

Overview Storing Data Basic types Arrays Controlling the flow of execution Loops (for, while) Ifthenelse Operators Arithmetic, relational, logical Functions (sub-program) Built-in functions Defining functions

Variables Name e.g. x, y, index, NSync, Nigela6 Types Integer (-2147483648 to +21747483647) Float (6 digits,10-38 to 10 +38 ) Double (15 digits, 10-308 to 10 +308 ) Boolean (true or false) Character or string

More on data storage: Arrays Arrays are collections of numbers all numbers within an array have the same type (e.g. integer) primes 0 0 0 0 main() { int primes[4]; primes[0] = 1; primes[1] = 2; primes[2] = 3; primes[3] = 5; } C program PYTHON program from numpy import * primes = zeros(4, int) primes[0] = 1 primes[1] = 2 primes[2] = 3 primes[3] = 5

More on data storage: Arrays Array contents can be modified primes 1 2 3 5 0 1 2 3 Array index main() { int primes[4]; primes[0] = 1; primes[1] = 2; primes[2] = 3; primes[3] = 5; } C program PYTHON program from numpy import * primes = zeros(4, int) primes[0] = 1 primes[1] = 2 primes[2] = 3 primes[3] = 5

Why use arrays? Store a list of measurements Can then compute mean, etc Vectors and matrices 2 1 5 1 =? C program main() { double vector[3]; double matrix[3][3]; 1 3 5 11 2 2 9 3 PYTHON program from numpy import * vector = zeros(3) matrix = zeros([3,3]) } matrix[0][0] = 1.0; matrix[0][1] = 5.0; matrix[0,0] = 1.0 matrix[0,1] = 5.0

Part B: Flow of control 1. Loops 2. Branches (ifthenelse)

Flow of control Top to bottom (within a program) unless: 1. Loops for loops while loops 2. Branches Ifthenelse 3. Functions?

1. Loops

1(a). While loops allows for repeated actions while (condition is true) {do this} while condition is true: do this main() { float x, y; } C program x=0.1; while (x < 2.0) { y = x*x; printf( %f %f\n, x, y); x = x + 0.1; } PYTHON program x=0.1 while x < 2.0: y = x*x print x, y x = x + 0.1

1(a). While loops allows for repeated actions main() { float x, y; } Repeated statements surrounded by braces {} C program x=0.1; while (x < 2.0) { y = x*x; printf( %f %f\n, x, y); x = x + 0.1; } Repeated statements indented PYTHON program x=0.1 while x < 2.0: y = x*x print x, y x = x + 0.1

1(b). for loops short-hand way to loop for a certain number of times main() { int i; C program PYTHON program for i in range(0,5): print %d bottles of beer % i } for (i=0; i < 5; i++) { printf( %d bottles of beer\n,i); }

1(b). for loops short-hand way to loop for a certain number of times main() { int i; C program PYTHON program for i in range(0,5): print %d bottles of beer % i } for (i=0; i < 5; i++) { printf( %d bottles of beer\n,i); } Equivalent to [0,1,2,3,4]

Looping in π program #include <math.h> 1 1 1 π = 2 3 1 + +... 2 3 3 3 5 3 7 3 C program from math import * Python program main() { double sum, pi; int i; sum = 1.0; for (i = 1; i < 30; i = i + 1) sum = sum+1.0/((i*2+1)*pow(-3.0,i)); sum = 1.0 for i in range(1,30): sum = sum+1.0/((i*2+1)*(-3.0)**i) pi = 2.0*sqrt(3.0)*sum print "%.20f\n" % pi pi = 2.0*sqrt(3.0)*sum; } printf("%.20f\n", 2.0*sqrt(3.0)*sum);

2. Branching (if statement) allows for different actions depending on input if (condition is true) {do this} if condition is true: do this C program main() { if (x < 2.0) { printf( x is less than 2!\n ); } } PYTHON program if x < 2.0: print x is less than 2!

2. Branching (ifelse statement) allows for different actions depending on input if (condition is true) {do this} else {do that} C program main() { if (x < 2.0) { printf( x is less than 2!\n ); } else { printf( x is >= 2!\n ); } } if condition is true: do this else: do that PYTHON program if x < 2.0: print x is less than 2! else: print x is >= 2!

Can combine ( nest ) loops main() { int i; } C program for (i=0; i < 5; i++) { if (i == 1) printf( 1 bottle of beer\n ); else printf( %d bottles of beer\n, i); } PYTHON program for i in range(0,5): if (i == 1): print 1 bottle of beer else: print %d bottles of beer % i

Can combine ( nest ) loops main() { int i; } C program for (i=0; i < 5; i++) { if (i == 1) printf( 1 bottle of beer\n ); else printf( %d bottles of beer\n, i); } Dropped braces because only 1 statement PYTHON program for i in range(0,5): if (i == 1): print 1 bottle of beer else: print %d bottles of beer % i Note: relational operator for equals is = =, not =

Summary Various kinds of variables (int, float, ) Float/double have limited precision Arrays are useful ways to group numbers Loops While, for Ifthenelse

What is an operator?

What is an operator?

What is an operator? Mathematical: L[f(x)] = g(x) L might be d/dx L[sin(x)] = cos(x) Computational: datum datum datum (unary) operator or (binary) operator datum datum

The 3 Types of Operators (1) number number arithmetic operator number e.g. 1+3 4 (binary/unary) (2) number number relational operator boolean e.g. 1<3 true (binary only) (3) boolean boolean logical operator (binary/unary) boolean e.g. true OR false true

1. Arithmetic operators

1. Arithmetic operators number number arithmetic operator number e.g. 3+x y-3*x (2.3/y+5)*x 2.0**x + addition - subtraction * multiplication / division ** exponential % modulo (remainder) ( ) grouping

1. Arithmetic operators float float arithmetic operator float 2.0/4.0 0.5 Two arguments of the same type produce a result with that type

1. Arithmetic operators float float arithmetic operator float 2.0/4.0 0.5 float integer arithmetic operator float 2.0/4 0.5 Two arguments of different types make the result the higher precision type

1. Arithmetic operators Warning: integer arithmetic can cause unexpected results integer integer arithmetic operator integer 2/4 0

1. Arithmetic operators: Example PYTHON program x=0.1 While x < 2.0: y = x*x print x, y x = x + 0.1 binary operation: multiply in: x, x out: x*x binary operation: add in: x, 0.1 out: x+0.1

2. Relational operators

2. Relational operators

2. Relational operators number number relational operator boolean (true or false) e.g. 3 < 4 x!= (4+x*y) index == i*2 < less than <= less than or equal > greater than >= greater than or equal to == equal to!= not equal to

2. Relational operators: Example if n == 3: x = 4/3*pi*r*r*r if n == 2: x = pi*r*r if n == 1: x = 2*pi*r if n > 3: print n is too large equal-to operator greater-than operator

2. Relational operators: warning Note: difference between == and = Relational operator: equal-to if n == 3: Variable assignment n = 3 n = 3 if n == 3: x = 4/3*pi*r*r*r This changes the value of n This doesn t Warning: confusing the two can cause difficult to debug errors.

3. Logical operators

3. Logical operators

3. Logical operators boolean boolean logical operator boolean (true or false) C && AND OR Python and AND or OR e.g. x < 3 && y>4 (i-4) == 1 x!= 2 e.g. x < 3 and y>4 (i-4) == 1 or x!= 2

3. Relational operators: Example if n == 3: x = 4/3*pi*r*r*r if n == 2: x = pi*r*r if n == 1: x = 2*pi*r if n > 3: print n is too large What if n = 0?

3. Relational operators: Example if n == 3: x = 4/3*pi*r*r*r if n == 2: x = pi*r*r if n == 1: x = 2*pi*r if n < 1 or n > 3: print n is out of range

B. Functions Math: define f(x) = x 2 f(3) = 9 argument(s) function value (return value) define g(x,y) = (x 2 + y 2 ) 1/2! g(3,4) = 5

Functions are sub-programs Take any number of arguments Any types Returns one value Any type result = func(arg1,arg2,)

1. Built-in functions built-in to language (library function) many math functions, string manipulations C program #include <math.h> main() { double x, y; x = 3.0; y = sqrt(x); } from math import * x = 3.0 y = sqrt(x) PYTHON program Imports the standard library of math functions

2. Writing your own functions 1. Define function 2. Invoke it from main program C program #include <math.h> float radius(double a, double b) { return sqrt(a*a + b*b); } main() { double x, y; x = 3; y = radius(x, 4.5); } PYTHON program from math import * def radius(a, b): return sqrt(a*a + b*b) x = 3.0 y = radius(x, 4.5)

2. Writing your own functions 1. Define function 2. Invoke it from main program C program #include <math.h> float radius(double a, double b) { return sqrt(a*a + b*b); } main() { double x, y; x = 3; y = radius(x, 4.5); } PYTHON program from math import * def radius(a, b): return sqrt(a*a + b*b) x = 3.0 y = radius(x, 4.5)

A prime number algorithm Step 1: List out the numbers from 1 to n Step 2: Strike out all numbers divisible by 2 (except 2) Strike out all numbers divisible by 3 (except 3) and so on for 7,11,13 Step 3: List out the remaining numbers, they are prime. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

primes: The program C program main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; for (j=2; j < m; j++) if (numbers[j]!= 0) for (i=2*j; i < n; i=i+j) numbers[i] = 0; PYTHON program numbers = range(0,1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i for j in range(2, m): if numbers[j]!= 0: for i in range(2*j,n,j): numbers[i] = 0 } for (i=1; i < n; i++) if (numbers[i]!= 0) printf("%d\n", numbers[i]); for i in range(1,n): if numbers[i]!= 0: print numbers[i]

1. Storage of data variables store data different types (integer, real, text,) n 1000 C program main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; PYTHON program numbers = range(0,1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i;

1. Storage of data arrays numbers C program main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; PYTHON program numbers = range(0, 1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i;

2. (Arithmetic) Operations elementary maths (+, -, multiply, divide) other operations (library functions) n 1000 Sqrt(1000)+1 m 32 C program main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; PYTHON program numbers = range(0,1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i;

3. Flow of Control: Loops perform (nearly) the same operation many times i=1 number[i] = i add 1 to i (until i=n) numbers 1 2 3 4 5 6 7 main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; C program numbers = range(0,1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i; PYTHON program

More loops Step 2: strike out all numbers divisible by 2 (j=2) strike out all numbers divisible by 3 (j=3) strike out all numbers divisible by 4 (j=4) strike out all numbers divisible by 5 (j=5) C program for (j=2; j < m; j++) if (numbers[j]!= 0) for (i=2*j; i < n; i=i+j) numbers[i] = 0; PYTHON program for j in range(2, m): if numbers[j]!= 0: for i in range(2*j,n,j): numbers[i] = 0

More loops Step 2: strike out all numbers divisible by 2 (excluding 2 itself) 4,6,8,10 0 0 numbers 1 2 3 0 5 0 7 C program for (j=2; j < m; j++) if (numbers[j]!= 0) for (i=2*j; i < n; i=i+j) numbers[i] = 0; PYTHON program for j in range(2, m): if numbers[j]!= 0: for i in range(2*j,n,j): numbers[i] = 0

4. Flow of Control: If statement Step 2: strike out all numbers divisible by 2 (j=2) strike out all numbers divisible by 3 (j=3) strike out all numbers divisible by 4 (j=4) strike out all numbers divisible by 5 (j=5) unnecessary numbers 1 2 3 0 5 0 7 C program for (j=2; j < m; j++) if (numbers[j]!= 0) for (i=2*j; i < n; i=i+j) numbers[i] = 0; PYTHON program for j in range(2, m): if numbers[j]!= 0: for i in range(2*j,n,j): numbers[i] = 0

4b. An alternative: functions Can define a function (strike_out) which actually does the striking out of one multiple (e.g. all the 2 s) C program strike_out(int numbers[], int n, int j) { for (i=2*j; i < n; i = i + j) numbers[i] = 0; } for (j=2; j < m; j++) if (numbers[j]!= 0) strike_out(numbers, n, j); def strike_out(numbers, n, j): for i in range(2*j,n,j): numbers[i] = 0; PYTHON program for j in range(2, m): if numbers[j]!= 0: strike_out(numbers, n, j)

5. Output (Step 3) C program main() { int numbers[1000], i, j, m, n; n = 1000; m = sqrt(n)+1; for (i=1; i < n; i++) numbers[i] = i; for (j=2; j < m; j++) if (numbers[j]!= 0) for (i=2*j; i < n; i=i+j) numbers[i] = 0; PYTHON program numbers = range(0,1000) n = 1000 m = sqrt(n)+1 for i in range(1,n): numbers[i] = i for j in range(2, m): if numbers[j]!= 0: for i in range(2*j,n,j): numbers[i] = 0 } for (i=1; i < n; i++) if (numbers[i]!= 0) printf("%d\n", numbers[i]); for i in range(1,n): if numbers[i]!= 0: print numbers[i]

Summary Operators arithmetic, relational, logical Functions Built-in Define yourself Do not even have to return a value