Trace call recsum(x, 3)

Similar documents
Trace call recsum(x, 3)

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

Better sorting algorithms (Weiss chapter )

Loop Example 1: Sum from 1 to N

Functions. Arash Rafiey. September 26, 2017

Mergesort again. 1. Split the list into two equal parts

Chapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Quicksort (Weiss chapter 8.6)

Subject: Fundamental of Computer Programming 2068

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Recursion: Recursion: repeated curses

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Solutions to Assessment

CS103L SPRING 2017 UNIT 8: RECURSION

COS 126 General Computer Science Spring Written Exam 1

Mergesort again. 1. Split the list into two equal parts

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

CS205: Scalable Software Systems

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Functions. Functions. Identify Repeated Code. Identify Repeated Code. Identify Similar Code. Use Parameters to Customize 2/25/14

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

FORM 2 (Please put your name and form # on the scantron!!!!)

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Exercise 1.1 Hello world

Sorting Algorithms part 1

CS 310 Advanced Data Structures and Algorithms

#include <iostream> using namespace std;

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Sorting. Weiss chapter , 8.6

CS 310 Advanced Data Structures and Algorithms

Searching in General

QUEEN MARY, UNIVERSITY OF LONDON

Midterm 2 A. 10 Questions. While some questions may seem familiar to practice problems, there are likely to be subtle

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

PDF created with pdffactory Pro trial version Recursion

4. Java language basics: Function. Minhaeng Lee

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Lecture 8 Tao Wang 1

Pointers, References and Arrays

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

STUDENT LESSON A9 Recursion

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid

CS61B Lecture #2. Public Service Announcements:

EAS 230 Fall 2002 Section B

Searching and Sorting (Savitch, Chapter 7.4)

Intermediate Programming, Spring 2017*

Solutions to the 2005 exam

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

What is an algorithm?

DCS/100 Procedural Programming

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Search,Sort,Recursion

Summer Final Exam Review Session August 5, 2009

Programming II (CS300)

Lecture P6: Recursion

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CS1150 Principles of Computer Science Arrays

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

Laboratorio di Algoritmi e Strutture Dati

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Exam 2. CSC 121 TTH Class. Lecturer: Howard Rosenthal. April 26, 2016

CS 12 Fall 2003 Solutions for mid-term exam #2

Data Structures And Algorithms

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Sample Problems for Quiz # 2

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

A function that invokes itself is said to

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Fundamentals of Programming Session 13

Analyzing Complexity of Lists

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

Study Guide for Test 2

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Recursion. Example R1

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

Lecture 7 CS2110 Fall 2014 RECURSION

CSCE 2014 Final Exam Spring Version A

Transcription:

1. Section 8 Exercises Program Memory Exercise 8-1 Trace for this value of X Working memory Global Memory Trace call recsum(x, 3) GIVENS: x (reference to an array of integers) n (number of elements to sum in the array) RESULT: s (sum of n elements in the array) partials (partial sum of first n-1 elements in x) HEADER: s recsum(x, n) x n partials s n - 1 x Address1 3 8 10 Address1 3 2 n = 1? n partials 2 m n - 1 s 8 partials recsum(x, m) s x[0] n - 1 s partials + x[n - 1] s Address1 n 1 partials s CPU 1

Exercise 8-1 Trace, Table 1 recsum(x, 3) statement Ref. by x n partials s initial values, 3, 2 3?? 1. test: n = 1? 2. partials recsum(x,n-1) 8 see Table 2 3. s partials + x[n-1] 10 s recsum(x,n-1) 8,3,2 2 s recsum( x, n) Exercise 8-1 Trace, Table 2 recsum(x, 2) statement Ref. by x n partials s initial values, 3, 2 2?? 1. test: n = 1? 2. partials recsum(x,n-1) see Table 3 3. s partials + x[n-1] 8 s recsum(x,n-1),3,2 1 s recsum( x, n) Exercise 8-1 Trace, Table 3 recsum(x, 1) statement Ref. by x n partials s initial values, 3, 2 1?? 1. test: N = 1? 4. s X[0] X 3 2 2

Program Memory Exercise 8-2 Translating recsum to Working memory Global Memory GIVENS: x (a reference to an array of integers) n (number of elements to sum in the array) RESULT: x (sum of n elements in the array) partials (partial sum of first N-1 elements in the array) HEADER: s recsum(x, n) 2 partials recsum(x, n-1) 1 Test: n = 1? 3 s partials + x[n - 1] 4 s x[0] public static int recsum(int [] x, int n) int partials; int s; if (n == 1) s = x[0]; partials = recsum(x, n-1); s = partials + x[n - 1]; return sum; CPU 3

Exercise 8-3 (a) - Find x n where x and n are integers and n 0, x 1. (Version 1) GIVENS: x (base of exponentiation) n (power to which x is raised) RESULT: xtothen (x to the power of n) m (set to n-1; smaller!) xtothem (partial results) HEADER: xtothen power (x,n) m n - 1 n = 0? // METHOD power: find x to the power n public static int power(int x, int n) // VARIABLE DECLARATION / DATA DICTIONNARY int m; // INTERMEDIATE: reduced value int xtothem; // INTERMEDIATE: partial result int xtothen; // RESULT: expected result // ALGORITHM BODY if (n == 0) xtothen = 1; m = n-1; xtothem = power(x, m); xtothen = xtothem * x; xtothem power(x, m) xtothen 1 // RETURN RESULT return xtothen; xtothen xtothem * X 4

Exercise 8-3 (b) - Find x n where x and n are integers and n 0, x 1. (Version 2 efficient version) GIVENS: x (base of exponentiation) n (power to which x is raised) RESULT: xtothen (x to the power of n) m (set to n/2; smaller!) xtothem (partial results) HEADER: xtothen power (x,n) xtothen xtothem * xtothem * x m n / 2 xtothem power(x, m) (n MOD 2) = 0? n = 0? xtothen xtothem * xtothem xtothen 1 // METHODE power: find x to the power n public static int power(int x, int n) // VARIABLE DECLARATION / DATA DICTIONNARY int m; // INTERMEDIATE: reduced value int xtothem; // INTERMEDIATE: partial result int xtothen; // RESULT: expected result // ALGORITHM BODY if (n == 0) xtothen = 1; m = n / 2; xtothem = power(x, m); if ( n%2 == 0) xtothen = xtothem * xtothem; xtothen = xtothem * xtothem * x; // RETURN RESULT return xtothen;

Exercise 8-4 - Given an array a of more than n numbers, return TRUE if all the numbers in positions 0 n of a are equal, and otherwise.. GIVENS: a (An array of numbers) n (Number of array elements to test) RESULT: isequal (Boolean: TRUE if all first N values in elements are equal) INTERMEDIATE: partialres (partial result) HEADER: isequal uptonsame(a, n) partialres Ñ uptonsame(a, n-1) isequal Ñ partialres AND (a[n-1] = a[0]) n 1 isequal Ñ TRUE // Method uptonsame Are elements up to n in a equal? public static boolean uptonsame(int[] a, int n) // VARIABLE DECLARATION / DATA DICTIONNARY boolean partialres; // INTERMEDIATE: partial result boolean isequal; // RESULT: expected result // ALGORITHM BODY if (n <= 1) isequal = ; partialres = uptonsame(a, n-1); // m=n-1 implicite isequal = partialresult && (a[n-1] == a[0]); // RETURN RESULT return isequal; Efficient Version n 1 (a[n-1] = a[0]) isequal Ñ FALSE isequal Ñ uptonsame(a, n-1) isequal Ñ TRUE // Method uptonsame efficient version public static boolean uptonsame(int[] a, int n) // VARIABLE DECLARATION / DATA DICTIONNARY boolean isequal; // RESULT: expected result // ALGORITHM BODY if (n <= 1) isequal = ; if(a[n-1) == a[0]) isequal = uptonsame(a, n-1); isequal = ; // RETURN RESULT return isequal; 6

Givens: n (integer) Results: f (integer, n factorial) Intermediates: (none) Header: f fact(n) Body: n = 0? f fact(n-1)*n f 1 Exercise 8- - Calculate N!. // Method fact // Given: n, an integer public static int fact(int n) int f; // RESULT if (n == 0) f = 1; // base case f = fact(n-1) * n; return f; GIVENS: n (An integer) RESULT: sumn (sum of integers from 1 to n) INTERMEDIATE: m (set to n-1; smaller!) summ (sum of integers from 1 to m) HEADER: sumn sumton(n) m Ñ n=1 summ Ñ sumton(m) sumn Ñ summ + n n = 1? sumn Ñ 1 Exercise 8-6 - Find the sum of 1+2+ +N. public static int sumton(int n) // Variable Declarations int sumn; // RESULT int m; // INTERMEDIATE int summ; // INTERMEDIATE if(n == 1) sumn = 1; // base case m = n-1; summ = sumton(m); // recursive call sumn = summ + 1; // Return results return(sumn); 7

Exercise 8-7 - Given an array A of N characters, reverse the values stored in positions Start to Finish. GIVENS: a (reference to a char. array to reverse) low (low index ) high (high index ) RESULTS: (none) MODIFIED: a (referenced array content changes) newhigh (new high index) newlow (new low index) temp (used for swapping) HEADER reverse(a, low, high) newlow Ñ low+1 newhigh Ñ high+1 reverse(a, newlow, newhigh) high low? temp Ñ a[low] a[low] Ñ a[high] a[high] Ñ temp] Æ (base case) // Method : reverse: Reverse the characters in array a with // size n. // To be called initially with reverse(a, 0, n-1) public static void reverse(char [] a, int low, int high) // VARIABLE DECLARATION / DATA DICTIONNARY int newhigh; // INTERMEDIATE: smaller high int newlow; // INTERMEDIATE: greater low char temp; // INTERMIDATE: buffer for char // ALGORITHM BODY if (high - low <= 1) /* base case: do nothing */ ; newlow = low + 1, newhigh = high - 1; // 2 variables to make smaller! reverse(a, newlow, newhigh); // reverse a[low] and a[high] temp = a[low]; a[low] = a[high]; a[high] = temp; // RESULT: The array reference by a is modified! 8

Exercise 8-8 - Sort an array of numbers in increasing order: sort algorithm/method GIVENS: a (reference to an array to sort) n (number of elements in array) RESULTS: (none) MODIFIED: a (sorted array) maxpos (position of largest value in array) temp (used for swapping) HEADER sort( a, n ) maxpos LocateLargestValue(A, N) temp a[maxpos] a[maxpos] a[n-1] a[n-1] temp n = 1? Æ public static void sort(int[] a, int n) // VARIABLE DECLARATIONS // GIVENS: a reference to array to sort // n number of elements to sort note // that a.length CANNOT be used. // INTERMEDIATES int maxpos; // position of largest value int temp; // used for swapping // BODY if(n <= 1) /* do nothing */ ; maxpos = locatelargestvalue(a, n); temp = a[maxpos]; a[maxpos] = a[n-1]; a[n-1] = temp; sort(a,n-1); // sort rest of array sort(a, n-1) 9

Exercise 8-8 - Sort an array of numbers in increasing order: locatelargestvalue algorithm/method GIVENS: a (an array to sort) n (number of elements in array) RESULTS: pos (index of largest value in array) m (integer, smaller interval) posm (position of largest value in smaller array) HEADER pos LocateLargestValue( a, n ) posm locatelargestvalue(a, m) pos n-1 m n -1 a[posm] > a[n-1]? pos posm n = 1? pos 0 public static void locatelargestvalue(int[] a, int n) // VARIABLE DECLARATIONS // GIVENS: a reference to array to sort // n number of elements to sort note // that a.length CANNOT be used. int pos; // RESULT position of largest value // INTERMEDIATES int m; // smaller int posm; // used for swapping if(n == 1) pos = 0; // base case m = n-1; posm = locatelargestvalue(a, m); // recursion if(a[posm] > a[n-1]) pos = posm; pos = n-1; // RESULT return(pos); 10