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

Post on 31-Dec-2015

32 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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

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

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

שלום!

rosenfa@gmail.comהמייל של אבי: •

ariellarich@gmail.comהמייל של אריאלה: •

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

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

מה לומדים?

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

דרישות הקורס

חשיבה!•

דרך ארץ!•

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

20%תרגילים – •

80% מבחן –•

מערכים

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

והנה בעיה

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

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

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

בעיה ?

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

הגדרת מערך

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

Dim A )9( As Integer

הגדרתמערך

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

Arraysמערכים

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

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

A[5]?

Dim A )9( As Integer

7

Arraysמערכים

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

מספר כללים

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

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

Arraysמערכים

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

I

A)I(

אינדקס

תוכן

Arraysמערכים

שימו לב להבדל

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

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

Dim A )5( As String

moshe david gad yossi yair avi

Arraysמערכים

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

moshe david gad yossi yair avi

Arraysמערכים

Len)a)2(( 3

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

a)2( + a)4( gadyair

דוגמא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

תרגיל כיתה: מספרים למערך ולהדפיס את הסכום והערך 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

תרגיל כיתה:מספרים למערך. להוסיף לכל מספר את המספר שבא 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

שיפורים...

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

פונקציות קיימות...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

פלט

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

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

תש"ע

והנה בעיה

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

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

תש"ע

בעיה ?

מערך דינמי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

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

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

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 פלט:

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

- פלט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

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

- שימוש בפונקציה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

- פלט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

פונקציות בוליאנית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

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

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

top related