יסודות מדעי המחשב – תרגול 4

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Add two strings representing numbers Carry = 0 S1 = S2 = S3 =
Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Answers to Assignment #1 (Assignment due: Wed. Feb. 05, 2003) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
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.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Checking character case class characterCase { public static void main (String arg[]) { char c = ‘A’; if (isUpperCase(c)) { System.out.println(c + “ is.
Object Oriented Programming Lecture 2: BallWorld.
Staples are our staple Building upon our solution.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
AP Computer Science Exam Review Number Systems Binary.
תרגול חזרה לבוחן נמרוד מילוא.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Array in C# Array in C# RIHS Arshad Khan
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
יסודות מדעי המחשב – תרגול 3
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Exercise Java programming
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Alg2_1c Extra Material for Alg2_1
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
An Introduction to Java – Part I
CS 200 Arrays, Loops and Methods
CS Week 8 Jim Williams, PhD.
פונקציות קרן כליף.
CS 200 Arrays, Loops and Methods
Computing Adjusted Quiz Total Score
תרגול Introduction to C - Fall Amir Menczel.
מיונים וחיפושים קרן כליף.
An Introduction to Java – Part I, language basics
CS Week 9 Jim Williams, PhD.
תרגול 4.
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
מבוא למדעי המחשב תרגול 4 – פונקציות.
מבוא למדעי המחשב תרגול 4 – פונקציות.
Code Animation Examples
Recursive GCD Demo public class Euclid {
CS 180 Assignment 6 Arrays.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Recursion Problems.
Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Scope of variables class scopeofvars {
Object Oriented Programming
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Question 1a) What is printed by the following Java program? int s;
Introduction to java Part I By Shenglan Zhang.
More on iterations using
CS302 Week 6 Jim Williams.
Presentation transcript:

יסודות מדעי המחשב – תרגול 4 מיונים ורקורסיה

מהלך התרגול המשך מיונים בדיקת קלט משחקי מצביעים רקורסיות "פשוטות" חיפוש רקורסיבי

מיון מחרוזות שימו לב – המחלקה Sort נמצאת בחומר עזר באתר public class sortExample { /** * @param args */ public static void main(String[] args) { String[] strArr = {"g","r","a","e","hello","world","bungee","alligator","java","gray","bun","t", "c","grab"}; char[] charArr = {'g','c','e','a','r','s','z','l'}; System.out.println("printing string array"); printArray(strArr); Sort.strLengthSort(strArr); System.out.println("printing length sorted array"); Sort.stringSort(strArr); System.out.println("printing lexicographicly sorted array"); System.out.println("printing character array"); printArray(charArr); Sort.charSort(charArr); System.out.println("printing sorted character array"); } public static void printArray(String[] arr) { int i; for (i=0;i<arr.length;i++) System.out.println(arr[i]); } public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println();   שימו לב – המחלקה Sort נמצאת בחומר עזר באתר

בדיקת קלט public static void printArray(String[] arr) { int i; for (i=0;i<arr.length;i++) System.out.println(arr[i]); } public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println();   public static void printArray(String[] arr) { if (arr!= null) int i; for (i=0;i<arr.length;i++) System.out.println(arr[i]); } else System.out.println(“Cannot print null array.”); public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println();  

משחקי מצביעים public static void switchString(String s1, String s2) { String str = s1; s1 = s2; s2 = str; } public static void swapArray(int[] arr1, int[] arr2) int[] tmp = arr1; arr1 = arr2; arr2 = tmp; public static int swap(int[] arr1, int[] arr2, int begin) { int tmp; if (begin < Math.min(arr1.length, arr2.length)) tmp = arr1[begin]; arr1[begin] = arr2[begin]; arr2[begin] = tmp; return swap(arr1, arr2, begin+1)+1; } else return 0;

Fibonacci סדרת פיבונצ'י מוגדרת עבור n>=2 בצורה הבאה: F(n) = F(n-1) + F(n-2) כאשר על פי ההגדרה F(1)=1 ו F(0)=0 public static long F(int n) { if (n == 0) return 0; } else if (n == 1) return 1; else return F(n-1) + F(n-2);

עוד רקורסיה – מה היא עושה?

חיפוש בינרי מטרה: חיפוש איבר במערך ממוין.