Lecture 7 Sept 29 Goals: Chapters 5 and 6. Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example:

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
1 Quick summary: print print "Kansas" Print string plus a newline print "Kansas", Print string plus a space print "Kansas", "City" Print strings with space.
Dr. Sharon Persinger October 30,  Recursion is a type of repetition used in mathematics and computing to create objects and to define functions.
CS1010 Programming Methodology
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Lecture 14 Go over midterm results Algorithms Efficiency More on prime numbers.
Lecture 3 Sept 3 Complete Chapter 3 Exercises quiz.
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Lecture 12 Oct 15 Recursion – more examples Chapter 8 problems and solutions Cell arrays, Chapter 10.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Sha Tin Methodist College F.4 Computer Studies Pascal Programming.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
Topic 6 Recursion.
Recall: d is a divisor of n  n % d == 0
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Numerical Algorithms x x-1
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Application: Algorithms
Application: Algorithms
COMPUTING.
Presentation transcript:

Lecture 7 Sept 29 Goals: Chapters 5 and 6

Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example: >> a = 1; >> b = 2; >> swap >> a a = 2 >> b b = 1

Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example: >> a = 1; >> b = 2; >> swap >> a a = 2 >> b b = 1 Script: temp = a; a = b; B = temp;

Exercise 5.3 Store a text file in the working directory. Write a script to create a cell array containing all the individual words of the text file. The cell array should not have any duplicate words. (i.e., keep only one copy of each word.)

Exercise 5.3 Store a text file in the working directory. Write a script to create a cell array containing all the individual words of the text file. The cell array should not have any duplicate words. (i.e., keep only one copy of each word.) Since this is a home work problem, we will not completely solve it.

Functions functions encapsulate computations that are repeatedly performed. input and output parameters. Example 1: Write a function to compute the hypotenuse of a right triangle given the two smaller sides a a and b.

function c = hyp(a, b) c = sqrt(a*a + b * b); This file should be stored in the current directory that is visible to Matlab. Then we can perform: >> hyp(3, 4) ans = 5

Example 2: Write a function swap that takes as input an array of integers and returns an array by swapping the max key and the key in index 1. For example: >> B = [1, 2, 8, 4, 7, 5, 6]; >> C = swap(B); >> C Ans = [8, 2, 1, 4, 7, 5, 6]; Etc.

Function swap function B = swap (A) [temp, id] = max(A); A(1) = A(1)+ A(id); A(id)= A(1) - A(id); A(1) = A(1) - A(id); B = A; Equivalently: function B = swap (A) [temp, id] = max(A); temp = A(1); A(1)= A(id); A(id) = temp; B = A;

Local vs. global variables

Example 3: Write a function GCD that outputs the greatest common divisor of two positive integers n and m. Recall Euclid’s algorithm: GCD of 52, 9 compute mod(52, 9) = 7 new pair: 9, 7 mod(9, 7) = 2 7, 2 mod(7, 2) = 1 2, 1 mod(2, 1) = 0 1, 0 When we reach pair (x, 0), x is the GCD.

GCD function We need to know how to create a loop. There are two ways to do this: for loop while loop For this problem, we will use the while loop.

GCD function function m = gcd(a, b) while ~(b==0) rem = mod(a, b); a = b; b = rem; end; m = a;

Exercise: Write a function that takes as input a positive integer n and returns the first prime number greater than n. Recall the function isprime in Matlab that returns true (false) if the input is a prime (is not a prime).

Exercise: Write a function that takes as input a positive integer n and returns the first prime number greater than n. Recall the function isprime in Matlab that returns true (false) if the input is a prime (is not a prime). function n = nextPrime(m) n = m + 1 while 1 if isprime(n) break; else n = n + 1; end;

Exercise 6.1: Write a function, powersum, that computes for any specified values of z and n.

Solution:

Exercise 6.2 (d) Write a function eliminate that takes as input a character string and returns a string of the same length in which e and E have been replaced by *. Hint: Use find and the index operator. Example: >> eliminate(‘TherE’) ans = Th*r*

Exercise 6.2 (d) Write a function eliminate that takes as input a character string and returns a string of the same length in which e and E have been replaced by *. Hint: Use find and the index operator. Solution:

Exercise 6.3: function out = addToEnd(a, x) out = [a, x]; Just calling the function with a as argument does not change its value. a = addToEnd(a, x) will change a as desired.

Exercise 6.6 >> BooleanToTF([ ]) ans = ‘TFFTTFTF’ etc.

Exercise 6.6 >> BooleanToTF([ ]) ans = ‘TFFTTFTF’ etc. function res = BooleanToTF(bool) res = char('F' + zeros(size(bool))); res(bool) = 'T';