Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 מבוא למדעי המחשב הרצאה 5: פונקציות. 2 מבוא לפונקציות חלוקה של אלגוריתם לתת משימות: משימה - פונקציה: דוגמאות מציאת המקסימלי מבין שני איברים האינדקס של.

Similar presentations


Presentation on theme: "1 מבוא למדעי המחשב הרצאה 5: פונקציות. 2 מבוא לפונקציות חלוקה של אלגוריתם לתת משימות: משימה - פונקציה: דוגמאות מציאת המקסימלי מבין שני איברים האינדקס של."— Presentation transcript:

1 1 מבוא למדעי המחשב הרצאה 5: פונקציות

2 2 מבוא לפונקציות חלוקה של אלגוריתם לתת משימות: משימה - פונקציה: דוגמאות מציאת המקסימלי מבין שני איברים האינדקס של האיבר הכי קטן במערך. System.out.println(), sc.nextInt(), Math.random() תוכניות שונות יכולות להשתמש באותן פונקציות!

3 3 מבנה הפונקציה public static ( ){ ans; // local variable... return ans; { כיצד מתבצעת קריאה לפונקציה? כיצד מתבצעת חזרה מפונקציה?

4 4 קריאה לפונקציה – gcd public static void main(String[] args) { int x, y, result; /* Code to read values x,y */ result = gcd(x, y); System.out.println(result); { public static int gcd(int m, int n) { int ans; /* Code to compute ans=gcd(m,n); */ return ans; }

5 5 פונקציה – findMinIndex public static void main(String[] args) { Scanner sc = new Scanner(System.in); int minIndex; int[] arr; int size = sc.nextInt(); arr = new int[size]; for (int i=0; i<size; i=i+1) arr[i] = sc.nextInt(); minIndex = findMinIndex(arr); System.out.println(minIndex); } // main public static int findMinIndex(int[] arr) { … } // findMinIndex

6 6 פונקציה – checkPrime public static boolean checkPrime(int candidate, int[] prArray, int prIndex) { boolean isPrime = true; // Innocent till found guilty for (int i = 0; i < prIndex & isPrime & (prArray[i]*prArray[i]) <= candidate; i=i+1) { if (candidate%prArray[i] == 0) isPrime = false; } // for return isPrime; } // checkPrime

7 7 קריאה לפונקציה – checkPrime public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] primes = new int[n]; // To keep the primes found so far int primeIndex = 0; // Index of the next prime found for (int x=2; x<=n; x=x+1) { if (checkPrime(x, primes, primeIndex) == true) { primes[primeIndex] = x; primeIndex = primeIndex+1; } // if } // for for (int i=0; i<primeIndex; i=i+1) System.out.println(primes[i]); } // main

8 8 תחום הכרת משתנה - scope ניתן להשתמש במשתנה החל מרגע ההצהרה עליו ועד לסיום הבלוק בו הוצהר בלוקים בלולאות while ו- for פרמטר של פונקציה וכן משתנה מקומי – מוכר רק בתוך הפונקציה

9 9 תוכנית עם מספר פונקציות פשוטות public class FunctionExample { public static void main(String[] args) { int m = sc.nextInt(); int n = sc.nextInt(); int ans1 = max(m, n); int ans2 = sum(m, n); System.out.println("The maximum is "+ans1); System.out.println("The sum is "+ans2); } // main

10 10 והפונקציות עצמן public static int max(int x, int y) { int maxVal; if (x>y) maxVal = x; else maxVal = y; return maxVal; } // max

11 11 והפונקציות עצמן public static int sum(int x, int y){ int res; res = x+y; return res; } // sum } // class FunctionExample

12 12 חיפוש איבר במערך ( חיפוש לינארי ) public static int linearSearch(int[] arr, int key) { int ans = -1; // default (not found) value for (int i=0; i<arr.length; i=i+1) { if (key==arr[i]) ans = i; } return ans; } & ans == -1

13 13 חיפוש בינארי ( במערך ממוין !) public static int binarySearch(int[] arr, int key) { int ans = -1; boolean found = false; int low = 0; int high = arr.length - 1; while (!found & low <= high) { int middle = (low + high)/2; if (arr[middle] == key) {found = true; ans = middle;} else if (key < arr[middle]) high = middle - 1; else low = middle + 1; } if (!found) ans = -(low+1); // addition to return position to add key return ans; }

14 14 פונקציות עם מערכים public class DiffArrays { public static void main(String[] args) { int[] arr1={10,4,1,44,-2242,-345}; int[] arr2={10,4,1,44,-2242,-345}; boolean different = diff(arr1, arr2); System.out.println(different); } // main

15 15 האם שני מערכים שונים בערכיהם ? public static boolean diff(int[] a, int[] b){ boolean ans; if (a==null | b==null) ans = (a!=b); else { if (a.length != b.length) ans = true; else ans = false; // ans=(a.length != b.length); for (int i=0; !ans & i<a.length; i=i+1) if (a[i] != b[i]) ans = true; } return ans; } // diff } // class DiffArrays


Download ppt "1 מבוא למדעי המחשב הרצאה 5: פונקציות. 2 מבוא לפונקציות חלוקה של אלגוריתם לתת משימות: משימה - פונקציה: דוגמאות מציאת המקסימלי מבין שני איברים האינדקס של."

Similar presentations


Ads by Google