60 likes | 210 Views
COMP 116: Introduction to Scientific Programming . Lecture 26: Writing Loops. Vector B=2*A. Write a function that takes as an input a vector A and returns the vector B=2*A. You are allowed to multiply only scalars We have to visit every element of A. We have to compute all elements of B.
E N D
COMP 116: Introduction to Scientific Programming Lecture 26: Writing Loops
Vector B=2*A Write a function that takes as an input a vector A and returns the vector B=2*A. You are allowed to multiply only scalars • We have to visit every element of A. • We have to compute all elements of B. • ith element of A affects only the ith element of B
Matrix B=2*A Write a function that takes as an input matrix A and returns the matrix B=2*A. You are allowed to multiply only scalars • We have to visit every element of A. • We have to compute all elements of B. • (i,j)thelement of A affects only the (i,j)thelement of B
Find the number of vowels in a string • The input is a string str, output is a single number num_vowels • We have to visit every character of str • Each character of str affects num_vowels • i.e. accumulate the effect of each character of str
Break sentence into words • Write a function get_the_words that • Takes as an input a sentence (in the form of a string) e.g. ‘This is a true statement’ • And returns a cell array of the words in the sentence i.e. {‘This’, ‘is’, ‘a’, ‘true’, ‘statement’} • Is there a clear correspondence between the input and output pointers? • What smaller problems would be helpful in solving the above problem?