31
םםםם םםםםםם םםםםםםםםםםם) םםםםם םםםם- םםםםם( ם"ם םםם םםםםםםם ם"ם םםםםםם םםםםםםםם

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (

Embed Size (px)

DESCRIPTION

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (. ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון. שלום!. המייל של אבי: [email protected] המייל של אריאלה: [email protected] כתובת האתר: www.jct.ac.il/~rosenfa/ibud סילאבוס הקורס: www.jct.ac.il/~rosenfa/ibud/syllabus.htm. מה לומדים?. מבנה נתונים - PowerPoint PPT Presentation

Citation preview

Page 1: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבנה נתונים ואלגוריתמים)לשעבר - עיבוד מידע(

ד"ר אבי רוזנפלד

ד"ר אריאלה ריכרדסון

Page 2: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

שלום!

[email protected]המייל של אבי: •

[email protected]המייל של אריאלה: •

www.jct.ac.il/~rosenfa/ibud כתובת האתר: •

סילאבוס הקורס: •www.jct.ac.il/~rosenfa/ibud/syllabus.htm

Page 3: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מה לומדים?

מבנה נתונים•אלגוריתמים•WINDOWSתכנות •

Page 4: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

דרישות הקורס

חשיבה!•

דרך ארץ!•

)אין חובת נוכחות!(•

20%תרגילים – •

80% מבחן –•

Page 5: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מערכים

Page 6: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

והנה בעיה

כתוב תוכנית אשר תקלוט מספרים ותדפיס כמה 36

מתוכם גדולים יותר מהממוצע.

Page 7: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

בעיה ?

Page 8: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

הגדרת מערך

Dim (גודל )שם As סוג

Dim A )9( As Integer

הגדרתמערך

אבל יש כאן לדוגמא תאים!10

Arraysמערכים

Page 9: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

9 -4 0 3 6 7 1 7 5 -2

A[5]?

Dim A )9( As Integer

7

Arraysמערכים

Page 10: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

מספר כללים

מספור התאים - החל מאפס•

האינדקס חייב להיות בתחום•

Arraysמערכים

Page 11: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

I

A)I(

אינדקס

תוכן

Arraysמערכים

שימו לב להבדל

Page 12: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

מערך של מחרוזות

Dim A )5( As String

moshe david gad yossi yair avi

Arraysמערכים

Page 13: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו8

moshe david gad yossi yair avi

Arraysמערכים

Len)a)2(( 3

Mid)a)1(,2,3( avi

a)2( + a)4( gadyair

Page 14: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

דוגמאModule Module1

Sub Main)( Dim x)10( As Integer Dim i As Integer Dim num As New Random)( For i = 0 To 10 'Why 10? x)i( = num.Next)1, 10( Next For i = 0 To 10 Console.WriteLine)x)i(( Next End Sub

End Module

Page 15: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

תרגיל כיתה: מספרים למערך ולהדפיס את הסכום והערך 10 יש לקלוט

המקסימלי.

Module Module1 Sub Main)( Dim x)10( As Integer Dim i As Integer For i = 1 To 10 'Why not 0? x)i( = Console.ReadLine)( Next Dim sum As Integer = 0 Dim max = x)1( For i = 1 To 10 sum += x)i( If )x)i( > max( Then max = x)i( End If Next Console.WriteLine)sum( Console.WriteLine)"Max is {0} ", max( End SubEnd Module

Page 16: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

תרגיל כיתה:מספרים למערך. להוסיף לכל מספר את המספר שבא 10יש לקלוט

אחריו במערך. למספר האחרון במערך לא להוסיף דבר. יש להדפיס את המערך.

Module Module1 Sub Main)( Dim x)10( As Integer Dim i As Integer For i = 0 To 9 'Why not 10? x)i( = Console.ReadLine)( Next For i = 0 To 9 x)i( = x)i( + x)i + 1( Next For i = 0 To 9 Console.WriteLine)"In position {0} I have {1} ", i, x)i(( Next

End SubEnd Module

Page 17: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

שיפורים...

Module Module1

Sub Main)( Dim x)10( As Integer Dim i As Integer Dim len As Integer = x.Length)( Console.WriteLine)"Length is {0} ", len( For i = 0 To len - 1 'Familiar? x)i( = Console.ReadLine)( Next For i = 0 To len - 2 'why - 2? x)i( = x)i( + x)i + 1( Next For i = 0 To len - 1 Console.WriteLine)"In position {0} I have {1} ", i, x)i(( Next End SubEnd Module

Page 18: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

פונקציות קיימות...Module Module1

Sub Main)( Dim x)( As Integer = {1, 7, 5, 50, -1} Console.WriteLine)"Length is {0} ", x.Length)(( Console.WriteLine)"Position 0 is {0} ", x)0(( Array.Resize)x, 10( Console.WriteLine)"Length is {0} ", x.Length)(( Console.WriteLine)"Position 0 is {0} ", x)0(( Console.WriteLine)"But position 9 is {0} ", x)9(( Array.Sort)x( Console.WriteLine)"Position 0 is {0} ", x)0((

End Sub

End Module

Page 19: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

פלט

Length is 5Position 0 is 1Length is 10Position 0 is 1But position 9 is 0Position 0 is -1

Page 20: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות לתו"נ- שבוע מספר - אבי רוזנפלד - סמסטר ב' – 11

תש"ע

והנה בעיה

כתוב תוכנית אשר תקלוט מספר לא ידוע של מספרים ותדפיס ...

Page 21: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מבוא לתכנות לתו"נ- שבוע מספר - אבי רוזנפלד - סמסטר ב' – 11

תש"ע

בעיה ?

Page 22: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מערך דינמיModule Module1 Sub Main)( Dim x)( As Integer = {} Dim answer As String = "yes" While )answer = "yes"( Console.WriteLine)"Do you want another number?"( answer = Console.ReadLine)( If )answer = "yes"( Then Array.Resize)x, x.Length + 1( Console.WriteLine)"Size is now " & x.Length( Console.WriteLine)"Now Enter a value"( x)x.Length - 1( = Console.ReadLine)( End If End While Dim i As Integer For i = 0 To x.Length - 1 Console.WriteLine)"In position {0} I have {1} ", i, x)i(( Next End SubEnd Module

Page 23: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

מערך דינמי - פלט

Do you want another number?yesSize is now 1Now Enter a value33Do you want another number?yesSize is now 2Now Enter a value45Do you want another number?noIn position 0 I have 33In position 1 I have 45

Page 24: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

1פונקציות – דוגמא

Module Module1

Function Max)ByVal x)( As Integer( As Integer Dim temp As Integer = x)0(

Dim i As Integer For i = 1 To x.Length - 1

If x)i( > temp Then temp = x)i(

End If Next

Return temp End Function

Sub Main)( Dim x)( As Integer = {1, 7, 5, 50, -1, 0, 100, -2}

Console.WriteLine)"The max is " & Max)x(( End Sub

End ModuleThe max is 100 פלט:

Page 25: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

2פונקציות – דוגמא Module Module1 Sub Print)ByVal x)( As Integer( Dim len As Integer = x.Length - 1 Dim i As Integer Console.WriteLine)"Begin the Print Array Sub"( For i = 0 To len Console.WriteLine)"In position {0} I have {1} ", i, x)i(( Next Console.WriteLine)"End of the Print Array Sub"( End Sub Sub Main)( Dim x)( As Integer = {1, 7, 5, 50, -1} Print)x( Array.Resize)x, 10( Print)x( Array.Sort)x( Print)x( Array.Reverse)x( Print)x( End SubEnd Module

Page 26: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

- פלט2המשך דוגמא Begin the Print Array SubIn position 0 I have 1In position 1 I have 7In position 2 I have 5In position 3 I have 50In position 4 I have -1End of the Print Array SubBegin the Print Array SubIn position 0 I have 1In position 1 I have 7In position 2 I have 5In position 3 I have 50In position 4 I have -1In position 5 I have 0In position 6 I have 0In position 7 I have 0In position 8 I have 0In position 9 I have 0End of the Print Array SubBegin the Print Array SubIn position 0 I have -1In position 1 I have 0In position 2 I have 0In position 3 I have 0In position 4 I have 0In position 5 I have 0In position 6 I have 1In position 7 I have 5In position 8 I have 7In position 9 I have 50End of the Print Array SubBegin the Print Array SubIn position 0 I have 50In position 1 I have 7In position 2 I have 5In position 3 I have 1In position 4 I have 0In position 5 I have 0In position 6 I have 0In position 7 I have 0In position 8 I have 0In position 9 I have -1End of the Print Array Sub

Page 27: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

3פונקציות – דוגמא

Sub Print)ByVal x)( As Integer(

Dim i As Integer Console.WriteLine)(

Console.WriteLine)"Now printing the array"( For i = 0 To x.Length)( - 1 'why -1???

Console.Write)x)i( & " "( If )i + 1( Mod 15 = 0 Then Console.WriteLine)""(

Next

End Sub

Page 28: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

- שימוש בפונקציה3המשך דוגמא Sub Main)( Dim i As Integer Dim targetArray)100( As Integer Dim rand As New Random For i = 0 To 100 targetArray)i( = rand.Next)-1000, 1000( Next ' Sort the entire targetArray. Array.Sort)targetArray( Print)targetArray( Array.Reverse)targetArray( Print)targetArray( Array.Resize)targetArray, 10( Array.Sort)targetArray( Print)targetArray( Console.WriteLine)vbNewLine(

End Sub

Page 29: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

- פלט3המשך דוגמא Now printing the array-996 -934 -917 -881 -870 -851 -848 -824 -818 -807 -791 -786 -778 -709 -704-666 -642 -618 -617 -597 -580 -509 -477 -426 -418 -403 -394 -354 -346 -337-317 -299 -289 -276 -253 -232 -231 -200 -193 -157 -124 -120 -74 -73 -72-69 -59 -1 20 23 24 113 140 198 228 262 264 272 315 322324 404 408 408 453 467 479 495 498 513 519 544 554 570 572584 597 619 633 642 684 703 704 720 731 737 739 776 778 786798 842 864 876 884 904 971 974 974 997 998Now printing the array998 997 974 974 971 904 884 876 864 842 798 786 778 776 739737 731 720 704 703 684 642 633 619 597 584 572 570 554 544519 513 498 495 479 467 453 408 408 404 324 322 315 272 264262 228 198 140 113 24 23 20 -1 -59 -69 -72 -73 -74 -120-124 -157 -193 -200 -231 -232 -253 -276 -289 -299 -317 -337 -346 -354 -394-403 -418 -426 -477 -509 -580 -597 -617 -618 -642 -666 -704 -709 -778 -786-791 -807 -818 -824 -848 -851 -870 -881 -917 -934 -996Now printing the array842 864 876 884 904 971 974 974 997 998

Page 30: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

פונקציות בוליאניתModule Module1 Function Flip)ByVal word1 As String( As Boolean Dim i As Integer For i = 0 To word1.Length)( - 1 If word1)i( <> word1)word1.Length)( - 1 - i( Then Return False End If Next Return True End Function Sub Main)( Dim a, b As String a = Console.ReadLine If )Flip)a(( Then Console.WriteLine)"It is a palindrome"( Else Console.WriteLine)"It isn't"( End If End SubEnd Module

Page 31: מבנה נתונים ואלגוריתמים )  לשעבר - עיבוד מידע (

עוד דרך לשנות את גודל המערך

Module Module1 Sub Main)( Dim x)( As Integer = {1, 2, 53, 3, 1, 23} Print)x( ReDim Preserve x)10( Print)x( ReDim x)15( Print)x( End SubEnd Module

פלט:Now printing the array1 2 53 3 1 23Now printing the array1 2 53 3 1 23 0 0 0 0 0Now printing the array0 0 0 0 0 0 0 0 0 0 0 0 0 0 00