Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

Similar documents
Previous Lecture: Today s Lecture: Announcement: Working with images. Characters and strings. Project 4 due Mon 10/26 at 11pm

Scientific Computing with MATLAB

An Introduction to MATLAB

function [s p] = sumprod (f, g)

Previous Lecture: Today s Lecture: Announcements: Nesting if-statements Logical operators short-circuit. Iteration using for

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

Chapter 2: Basic Elements of C++

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

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

ROCK PAPER SCISSORS Rock Paper Scissors Lab Project Using C or C++

Objectives. In this chapter, you will:

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Outline. User-based knn Algorithm Basics of Matlab Control Structures Scripts and Functions Help

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Fundamentals of Programming CS-110. Lecture 2

Chapter 7: Programming in MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB

Full file at

Lecture 15 MATLAB II: Conditional Statements and Arrays

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Lecture 1. Types, Expressions, & Variables

UNIT 5. String Functions and Random Numbers

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

CS313D: ADVANCED PROGRAMMING LANGUAGE

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Intro Chapter 1 Demo Help Lookfor Doc Quit exit Variable variablename = expression = assignment operator, initializing Incrementing Identifier names

JME Language Reference Manual

VISUAL BASIC 6.0 OVERVIEW

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

Programming for Experimental Research. Flow Control

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Part III Appendices 165

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Programming with Java

2.0 MATLAB Fundamentals

Relational and Logical Statements

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Python Intro GIS Week 1. Jake K. Carr

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Introduction to MATLAB

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Computer Programming : C++

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

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Introduction to MATLAB 7 for Engineers

Relational operators (1)

UNIT- 3 Introduction to C++

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

Programming Paradigms

Text Input and Conditionals

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 3: Programming with MATLAB

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CSc Introduction to Computing

Eng Marine Production Management. Introduction to Matlab

BASIC ELEMENTS OF A COMPUTER PROGRAM

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

QUIZ: What value is stored in a after this

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

Lecture 1: Hello, MATLAB!

Lecturer: Keyvan Dehmamy

Introduction to MATLAB Programming

University of Alberta

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Object Oriented Programming with Java

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Ex: If you use a program to record sales, you will want to remember data:

ECE 102 Engineering Computation

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Chapter 2 Elementary Programming

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. MATLAB Fundamentals

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Schedule. Matlab Exam. Documentation. Program Building Blocks. Dynamics Makeup Exam. Matlab Exam. Semester Summary and Wrap up.

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Introduction to Programming EC-105. Lecture 2

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Statement of integrity: I did not, and will not, violate the rules of academic integrity on this exam.

Transcription:

Today s topics Characters and strings Review of topics for Test 1 Announcements/Reminders: Assignment 1b due tonight 11:59pm Test 1 in class on Thursday

Characters & strings We have used strings already: n= input( Next number: ) fprintf( Answer is %d, ans) A string is made up of individual characters, so a string is a 1-d array of characters CS1112 rocks! is a character array of length 13; it has 7 letters, 4 digits, 1 space, and 1 symbol. C S 1 1 3 2 r o c k s! Can have 2-d array of characters as well C S 1 1 3 2 r o c k s! 2 6 matrix

Matlab types: char, double, uint8, logical a C S 1 b = [3 9] There is not a type string! What we call a string is a 1-d array of chars a is a 1-d array with type char components. We call a a string or char array b is a 1-d array with type double components. double is the default type for numbers in Matlab. We call b a numeric array d = rand >.5 d is a scalar of the type logical. We call d a boolean value Lecture 17 3

Single quotes enclose strings in Matlab Anything enclosed in single quotes is a string (even if it looks like something else) 100 is a character array (string) of length 3 100 is a numeric value pi is a character array of length 2 pi is the built-in constant 3.1416 x is a character (vector of length 1) x may be a variable name in your program Lecture 17 4

Vectors Assignment v= [7 0 5]; Indexing x= v(3); % x is 5 v(1)= 1; % v is [1 0 5] w= v(2:3); % w is [0 5] : notation v= 2:5; % v is [2 3 4 5] Appending v= [7 0 5]; v(4)= 2; % v is [7 0 5 2] Concatenation v= [v [4 6]]; % v is [7 0 5 2 4 6] Strings are vectors Strings Assignment s= hello ; Indexing c= s(2); % c is e s(1)= J ; % s is Jello t= s(2:4); % t is ell : notation s= a : g ; % s is abcdefg Appending s= duck ; s(5)= s ; % s is ducks Concatenation s= [s quack ]; % s is ducks quack Lecture 17 5

Some useful string functions str= Cs 1112 ; length(str) % 7 isletter(str) % [1 1 0 0 0 0 0] isspace(str) % [0 0 1 0 0 0 0] lower(str) % cs 1112 upper(str) % CS 1112 ischar(str) % Is str a char array? True (1) strcmp(str(1:2), cs ) % Compare strings str(1:2) & cs. False (0) strcmp(str(1:3), CS ) % False (0) Lecture 17 6

Example: capitalize 1 st letter Write a function to capitalize the first letter of each word in a string. Assume that the string has lower case letters and blanks only. (OK to use built-in function upper) function [str, ncaps] = caps(str) % Post: Capitalize first letter of each word. % str = partially capitalized string % ncaps = no. of capital letters % Pre: str = string with lower case letters & blanks only look for Look For the spaces The Spaces See caps.m

8

ASCII characters (American Standard Code for Information Interchange) ascii code Character : : : : 65 A 66 B 67 C : : 90 Z : : ascii code Character : : : : 48 0 49 1 50 2 : : 57 9 : : Lecture 17 9

Character vs ASCII code str= Age 19 %a 1-d array of characters code= double(str) %convert chars to ascii values str1= char(code) %convert ascii values to chars Lecture 17 10

Arithmetic and relational ops on characters c - a gives 2 6-5 gives 1 letter1= e ; letter2= f ; letter1-letter2 gives -1 c > a gives true letter1==letter2 gives false A + 2 gives 67 char( A +2) gives C Lecture 17 11

What is in variable g (if it gets created)? d1= Mar 3 ; d2= Mar 9 ; x1= d1(5); x2= d2(5); g= x2-x1; A: the character 6 B: the numeric value 6 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error Lecture 17 12

What is in variable g (if it gets created)? d1= Mar 13 ; d2= Mar 29 ; x1= d1(5:6); x2= d2(5:6); g= x2-x1; A: the string 16 B: the numeric value 16 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error Lecture 17 13

Example: toupper Write a function toupper(cha) to convert character cha to upper case if cha is a lower case letter. Return the converted letter. If cha is not a lower case letter, simply return the character cha. Hint: Think about the distance between a letter and the base letter a (or A ). E.g., a b c d e f g h A B C D E F G H distance = g - a = 6 = G - A Of course, do not use Matlab function upper! Lecture 17 14

function up = toupper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. Lecture 17 15

function up = toupper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; cha is lower case if it is between a and z Lecture 17 16

function up = toupper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) % Find distance of cha from a end Lecture 17 17

function up = toupper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) end % Find distance of cha from a offset= cha - 'a'; % Go same distance from A Lecture 17 18

function up = toupper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha. up= cha; if ( cha >= 'a' && cha <= 'z' ) % Find distance of cha from a offset= cha - 'a'; end % Go same distance from A up= char('a' + offset); Lecture 17 19

20

Review Scrip and Function If statement While loop For loop Array Matrix Plot, hold on, legend 21

The if construct if boolean expression1 statements to execute if expression1 is true elseif boolean expression2 statements to execute if expression1 is false but expression2 is true : else statements to execute if all previous conditions are false end 22

Generating random numbers rand(m,n) gives an m-by-n matrix of random values, each in interval (0,1) Generate a random number in the range (a,b) Generate a random integer in the range [a,b] 23

Generating random numbers rand(m,n) gives an m-by-n matrix of random values, each in interval (0,1) Generate a random number in the range (a,b) Generate a random integer in the range [a,b] + a-1 24

Simulation problem: Ann and Bob take turns flipping an unfair coin twice as likely to be heads than tails In one round, each player flips once Ann gets 1 point if she gets heads; Bob gets 2 points if he gets tails Game ends after the round in which at least one player gets 10 points. Display the final scores. 25

Simulation problem: Ann and Bob take turns flipping an unfair coin twice as likely to be heads than tails In one round, each player flips once Ann gets 1 point if she gets heads; Bob gets 2 points if he gets tails Game ends after the round in which at least one player gets 10 points. Display the final scores. 26

Common loop patterns Do something n times Do something an indefinite number of times for k= 1:1:n end % Do something %Initialize loop variables while ( not stopping signal ) % Do something % Update loop variables end 27

for loop examples for k= 2:0.5:3 k takes on the values 2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values 1,2,3,4 disp(k) Default increment is 1 end for k= 0:-2:-6 k takes on the values 0,-2,-4,-6 disp(k) Increment may be negative end for k= 0:-2:-7 k takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a bound end for k= 5:2:1 The set of values for k is the empty disp(k) set: the loop body won t execute end 28

for k = 4:6 disp(k) k= 9; disp(k) end Not a condition (boolean expression) that checks whether k<=6. It is an expression that specifies values: 4 5 6 29

Built-in functions for creating/manipulating arrays Creation zeros, ones, rand linspace Manipulation length size 30

Built-in functions for creating/manipulating arrays Creation zeros, ones, rand linspace Manipulation length size 31

Example Write a program fragment that calculates the cumulative sums of a given vector v. The cumulative sums should be stored in a vector of the same length as v. 1, 3, 5, 0 v 1, 4, 9, 9 cumulative sums of v 32

34

Function header is the contract for how the function will be used (called) You have this function: function [x, y] = polar2xy(r, theta) % Convert polar coordinates (r, theta) to % Cartesian coordinates (x,y). Theta in degrees. Code to call the above function: % Convert polar (r1,t1) to Cartesian (x1,y1) r1= 1; t1= 30; [x1, y1]= polar2xy(r1, t1); plot(x1, y1, b* ) 35

Given this function: function m = convertlength(ft,in) % Convert length from feet (ft) and inches (in) % to meters (m).... How many proper calls to convertlength are shown below? % Given f and n d= convertlength(f,n); d= convertlength(f*12+n); d= convertlength(f+n/12); x= min(convertlength(f,n), 1); y= convertlength(pi*(f+n/12)^2); A: 1 B: 2 C: 3 D: 4 E: 5 or 0

Example Write a function evalpoly to evaluate an n th order polynomial of x : a 0 a Input parameter coef has length n+1, contains the coefficients of the polynomial coef(1) is the coefficient for the term x 0 Input parameter x 1 x a n a n x Return the value of the polynomial evaluated at x No Matlab predefined function other than length 2 x 2 37

38

39

Other notes for the test (course) Read questions/instructions carefully Use Matlab syntax Do not use break, continue Do not use randi, instead use rand Many students make index out-of-bounds error 40

Other notes for the test (course) Read questions/instructions carefully Use Matlab syntax Do not use break, continue Do not use randi, instead use rand Many students make index out-of-bounds error 41