1 / 24

תרגול 3 – מערך דו מימדי, פונקציות

תרגול 3 – מערך דו מימדי, פונקציות. מבוא לתכנות מערכות. היום בתרגול. מערך דו-מימדי: למה צריך? איך מגדירים? איך זה נראה בזכרון? דוגמאות לשימוש במערך דו-מימדי. העמסה של פונקציות. 2. מערך דו-מימדי. מערך שבו כל איבר הוא גם מערך,כלומר מערך של מערכים. 3. מערך דו-מימדי.

thisbe
Download Presentation

תרגול 3 – מערך דו מימדי, פונקציות

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. תרגול 3 – מערך דו מימדי, פונקציות מבוא לתכנות מערכות

  2. היום בתרגול מערך דו-מימדי: למה צריך? איך מגדירים? איך זה נראה בזכרון? דוגמאות לשימוש במערך דו-מימדי. העמסה של פונקציות 2

  3. מערך דו-מימדי מערך שבו כל איבר הוא גם מערך,כלומר מערך של מערכים. 3

  4. מערך דו-מימדי int[][] array1 = new int [3][2]; array1 0 0 0 0 0 0 4

  5. מערך דו-מימדי int[][] array2 ={{1,3},{5,6,7},{4}}; array2 3 5 6 4 1 7 5

  6. length – אורך של מערך array1.length  array1[0].length  array2.length  array2[2].length  array2[1].length  array2[3].length  array1 array2 3 5 6 4 1 7 3 2 3 1 3 Run time Error 6

  7. פנייה לאיבר System.out.println(array2[1][0]); array2[2][0] = 9; array2 3 5 6 4 1 7 9 // prints 5 7

  8. class ArrayRef{ publicstaticvoidmain(String []args){ int[][] arr; arr = newint[3][]; arr[0] = newint[3]; arr[1] = arr[0]; arr[0][1] = 7; //prints the 2D array for (int i = 0 ; i < arr.length ; i= i+1){ for(int j=0 ; j<arr[i].length; j = j+1){ System.out.print(arr[i][j]+” ”); } System.out.println(); } } } 0 7 0 0 7 0 NullPointerException 8

  9. שאלה מה יקרה אם נוסיף את השורה הבאה? arr[2]= arr; תשובה: java מבדילה בין מערך של מספרים ובין מערך של מערכים של מספרים ולכן השורה הזאת לא תעבור קומפילציה (שכן איברי המערך צריכים להיות מאותו טיפוס). 9

  10. העתקת מערך class Copy2dArray{ publicstaticvoid main(String[] args){ int[][] iA = {{1,3},{5,6,7},{4}}; int[][] iC; //Create the 2dArray row by row iC= newint[iA.length][]; int i,j; for ( i=0 ; i < iC.length; i = i+1) iC[i] = newint[iA[i].length]; for ( i=0; i < iC.length; i = i+1) for (j=0 ; j < iC[i].length ; j = j+1) iC[i][j] = iA[i][j]; } //main } 10

  11. משולש פסקל מה החוקים לחישוב משולש פסקל 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Pascal[j][0] = Pascal[j][j]=1 Pascal[j][i] = Pascal[j-1][i] + Pascal[j-1][i-1] Wikipedia http://en.wikipedia.org/wiki/Pascal%27s_triangle 11

  12. הדפסת לוח הכפל class MultiplicationTable { publicstaticvoid main(String[] arg) { finalint iROW = 13; finalint iCOLUMN = 13; int[][] mat = newint[iROW][iCOLUMN]; for (int i=0; i < iROW; i=i+1) for (int j=0; j < iCOLUMN; j=j+1) mat[i][j] = i*j; //prints the matrix for (int i=0; i < iROW; i=i+1) { for (int j=0; j < iCOLUMN; j=j+1) System.out.print(mat[i][j]+"\t"); System.out.println(); } } //main } //class 12

  13. העמסה (Overloading) • ‘+’ with strings • שתי פונקציות בעלות שם זהה (אבל חתימה שונה!). • לדוגמא: • Math.min(int,int) ו- Math.min(double,double) • או • System.out.println(int) ו- System.out.println(char) • כמו כן, אופרטורים אריתמטיים הם דוגמאות נוספות להעמסה. • למשל, אופרטור החלוקה יכול לקבל שני ארגומנטים מסוג int או מסוג double. • איזה עוד דוגמאות לאופרטורים מועמסים ישנם? 13

  14. דוגמא להעמסה: • publicclass Max{ • // returns the larger between the arguments • publicstaticdouble max(double d1, double d2){ • double ans; • if(d1 < d2) • ans = d2; • else • ans = d1; • return ans; • } • publicstaticint max(int i, double d){ • int ans; • if(i < d) • ans = (int)d; • else • ans = i; • return ans; • } אם היינו מחליפים את טיפוס הארגומנט בטיפוס int מה היה קורה? public static int foo(int num){…} √ public static int foo(double num){…} √ public static double foo(int num){…} X 14

  15. publicstaticvoid main(String[] args){ int i1 = 1, i2 = 2 ; double d1 = 2, d2 = 0 ; System.out.println(max(d1,d2)); System.out.println(max(i1,d1)); System.out.println(max(i1,i2)); // example for casting } /* output 2.0 2 2 */ 15

  16. שאלות משנים קודמות

  17. שאלה 1 מבוחן 2002 (20 נקודות) מערך דו-ממדי נקרא מטריצה אם אינו ריק ואם כל שורותיו שוות באורכן ואורך זה גדול מאפס.השלימו את הגדרת השיטה הסטטית checkMatrix(int[][] m), אשר בודקת אם מערך דו-מימדי, m, היינו מטריצה ומחזירה ערך בוליאני בהתאם.הערה:אם קוראים לשיטה עם פרמטר שערכו null, על השיטה להחזיר את הערך false. בכל ריבוע ריק יש להשלים הוראה יחידה )כלומר לא יופיע בו (";".[דוגמא] && 18

  18. לשם הבהרה, השיטה :main publicstaticvoid main (String[] args){ int[] line1 = {1,2,3,4}; int[] line2 = {5,6,7,8}; int[] line3 = {9}; int[] line4 = newint[0]; int[][] m1 = {line1,line2}; int[][] m2 = {line1,line2,line3}; int[][] m3 = null; int[][] m4 = {null,null}; int[][] m5 = {line4,line4}; int[][] m6 = {line2,null}; int[][] m7 = newint[0][0]; System.out.println(checkMatrix(m1)); System.out.println(checkMatrix(m2)); System.out.println(checkMatrix(m3)); System.out.println(checkMatrix(m4)); System.out.println(checkMatrix(m5)); System.out.println(checkMatrix(m6)); System.out.println(checkMatrix(m7)); } תדפיס: true false false false false false false

  19. שאלה2 מבוחן 2002(20 נק') • נאמר ששני מספרים טבעיים חיוביים הם זרים אם אין להם מחלק משותף (פרט ל- 1). נתונה לכם שיטה סטטית public static int gcd (int m, int n) אשר מחזירה את המחלק המשותף הגדול ביותר של שני מספרים טבעיים חיוביים.השלימו את השיטה checkGCD(int[] a) אשר מחזירה ערך בוליאני המציין האם מערך a מקיים אחת משתי התכונות הבאות: • א. כל שני מספרים שונים ב- a זרים. • ב. כל שני מספרים שונים ב-a אינם זרים. • למשל: • האוסף 4,7,25 מקיים את תכונה א' • האוסף 6,10,15 מקיים את תכונה ב',למרות שאין שום מספר גדול מ- 1המחלק את שלושת המספרים. • האוסף20,7,20 מקיים את תכונה א' כי רק 7 ו- 20 שונים והם זרים. • האוסף4,7,28 אינו מקיים אף אחת משתי התכונות. • הפעלתcheckGCD על שלושת המערכים הראשונים מחזירה true. ואילו על הרביעי false -. • יש להניח שבמערך יש לפחות שני מספרים שונים זה מזה. תוכלו גם להניח שכל המספרים הנתונים הם חיוביים, אך שימו לב שיתכן ואינם שונים זה מזה. 21

  20. פתרון: • publicstaticboolean checkGCD(int[] a){ • boolean aliens = true; // each 2 are aliens • boolean notAliens = true; // each 2 are not aliens • for (int i = 0; i < a.length – 1 && (aliens || notAliens); i=i+1){ • for (int j = i+1; j < a.length && (aliens || notAliens); j=j+1){ • if (a[i] != a[j]) { • if (gcd(a[i], a[j]) == 1) notAliens = false; • else • aliens = false; • } • } • } • return aliens || notAliens; • }

  21. שאלה 5מבוחן 17 ) 2004נקודות) נתונה הפונקציה public static int oddGCD(int m, int n) לחישוב המחלק המשותף המקסימלי (gcd) של שני מספרים שלמים m ו- n .שיטה זו פועלת נכון רק במקרה ולפחות אחד המספרים הינו אי-זוגי. השלימו את הגדרת השיטה הבאה, תוך שימוש בשיטה oddGCD הנתונה, כך שתחשב את המחלק המשותף המקסימאלי עבור כל שני שלמים אי-שליליים (הניחו כי לפחות אחד המספרים אינו אפס.( בפתרון שתציעו אין לבצע כל בדיקה על ערכו של משתנה פרט לבדיקה האם הוא זוגי או לא. כמובן שניתן גם להשתמש בפעולות לוגיות כמו (!, &, |).ניתן להניח שהקלט הינו חוקי (כלומר ש- n ו- m אי-שליליים ולפחות אחד מהם חיובי ממש). publicstatic int generalGCD(int m, int n) { // השלימו } רמז: gcd(x∙y, x∙z) = x∙gcd(y,z) 23

  22. פתרון: • publicstatic int generalGCD(int m, int n) { • int multiply = 1; • while( (m % 2 == 0) && (n % 2 == 0) { • multiply = multiply * 2; • m = m / 2; • n = n / 2; • } • return multiply * oddGCD(m, n); • }

More Related