What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS Data Structures Section 2.6.
Advertisements

Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Multiple-Subscripted Array
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS1101X: Programming Methodology Recitation 9 Recursion II.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
CSCI 51 Introduction to Programming March 12, 2009.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSCI 51 Introduction to Programming March 10, 2009.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
WEEK 10 Class Activities Lecturer’s slides.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Chapter 15 Recursion.
Building Java Programs
Chapter 15 Recursion.
Recursion
slides adapted from Marty Stepp and Hélène Martin
Big-Oh and Execution Time: A Review
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Introduction to Programming
C Arrays (2) (Chapter 6) ECET 264.
slides created by Marty Stepp and Alyssa Harding
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp
Building Java Programs
Algorithms and data structures: basic definitions
Presentation transcript:

What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion) grouping entities (arrays)

METHOD EXECUTION

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

Boolean Operators && || (row == 3) && (topCoins > 5) || (botCoins > 5) is equivalent in Java to ((row == 3) && (topCoins > 5)) || (botCoins > 5)

int i = 1; FOR loop and WHILE loop while (i < 10) { sum = sum + i; i = i + 1; } int sum = 0; for ( ; ; ) { } i < 10 i = i + 1 int i = 1; int sum = 0; sum = sum + i;

NESTED LOOPS

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :0 j :3

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :0 j :3 k :0 v :1 k :1 3, 0, 1

int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :1 k :2 Tracing Through Nested Loops 3 v :1 j :3 v :2 3, 0, 1 3, 1, 2

int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :2 k :3 Tracing Through Nested Loops 3 v :2 j :3 v :3 3, 0, 1 3, 1, 2 3, 2, 3

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :3 j :3 j :2 3, 0, 1 3, 1, 2 3, 2, 3

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :3 j :2 k :0 v :4 k :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4

int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :1 k :2 Tracing Through Nested Loops 3 v :4 j :2 v :5 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :5 j :2 j :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :5 j :1 k :0 v :6 k :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :1 j :0 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :0 k :0 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :0 j :-1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

2D ARRAYS

2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to

2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 6] 1 [1.. 5] 2 [2.. 4] 3 [3.. 3] row columns 0 [0.. 7) 1 [1.. 6) 2 [2.. 5) 3 [3.. 4)

2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 7) = [0.. size-0) 1 [1.. 6) = [1.. size-1) 2 [2.. 5) = [2.. size-2) 3 [3.. 4) = [3.. size-3) r [?.. ?) r [r.. size-r)

2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 7) = [0.. size-0) 1 [1.. 6) = [1.. size-1) 2 [2.. 5) = [2.. size-2) 3 [3.. 4) = [3.. size-3) int[][] sliceTriangle(int[][] table) { int size = table.length; int[][] result = new int[size][size]; for (int r = 0; r <= size/2; r++) { for (int c = r; c < size-r; c++) { result[r][c] = table[r][c]; } return result; } r [?.. ?) r [r.. size-r)

ALGORITHMS

Algorithms Sorting – Bubble Sort and Selection Sort Comparison – best case, average, case, worst case Searching – Linear Search and Binary Search Assumptions and Comparison

RECURSION

void mystery(int n) { if (n == 0) { System.out.println("done!"); } else { System.out.println(n); mystery(n - 1); } } void mystery(int n) { if (n == 0) { System.out.println("done!"); } else { mystery(n - 1); System.out.println(n); } } done done What is displayed after the call mystery(5) ?

Draw the “ears” first, then draw the “face” so it covers the “ears”. Step 1: Each “ear” is itself a Mickey – draw recursively! Step 2: Draw the “face” – a simple circle! Generate the following pattern

void drawMickey(double x, double y, double radius, int depth) { if(depth>0) { // DRAW EARS drawMickey(x-radius,y-radius,radius/2,depth-1); drawMickey(x+radius,y-radius,radius/2,depth-1); // DRAW FACE – will cover the ears canvas.drawCircle(x,y,radius+2,"white"); canvas.drawCircle(x,y,radius,"black"); } } void drawMickey(double x, double y, double radius, int depth) { if(depth>0) { // DRAW FACE canvas.drawCircle(x,y,radius+2,"white"); canvas.drawCircle(x,y,radius,"black"); // DRAW EARS – will cover the face drawMickey(x-radius,y-radius,radius/2,depth-1); drawMickey(x+radius,y-radius,radius/2,depth-1); } }

Things to Keep in Mind Write for people not for the machine –indent the code –put comments –design self-contained functions –use appropriate names –develop good test cases

What’s Next CS I – focus on describing procedures and how they interact CS II – focus on describing objects and how they interact CS III – focus on algorithms and data structures