1 / 32

מבוא למדעי המחשב לתעשייה וניהול

הרצאה 10. מבוא למדעי המחשב לתעשייה וניהול. שם המערך. לכל ערך יש אינדקס. 79 87 94 82 67 98 87 81 74 91. scores. מערך. 0 1 2 3 4 5 6 7 8 9. המערך ממוספר מ 0 לגודל המערך פחות 1. במערך הנ"ל 10 ערכים, האינדקסים נעים בין 0 ל 9. למה מערך?.

eldora
Download Presentation

מבוא למדעי המחשב לתעשייה וניהול

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. הרצאה 10 מבוא למדעי המחשב לתעשייה וניהול

  2. שם המערך לכל ערך יש אינדקס 79 87 94 82 67 98 87 81 74 91 scores מערך 0 1 2 3 4 5 6 7 8 9 המערך ממוספר מ 0 לגודל המערך פחות 1 במערך הנ"ל 10 ערכים, האינדקסים נעים בין 0 ל 9

  3. למה מערך? • פעמים רבות נרצה להשתמש במספר רב של משתנים מאותו סוג • רשימות ציונים, טבלאות סטודנטים... • מערך מאפשר לנו לשמור רשימת משתנים מאותו סוג • הרשימה במערך ממוספרת • ניתן לגשת לכל אחד מהאיברים בנפרד • ניתן לגשת לכל המערך • ניתן לעבור על כל האיברים במערך (ע"י שימוש בלולאות)

  4. 79 87 94 82 67 98 87 81 74 91 מערכים – מה יש בתאים? • כל האיברים במערך הם מאותו טיפוס • (Integerבדוגמא שלנו) • אין הגבלה על סוג הטיפוסים שאפשר להגדיר להם מערך • טיפוסים בסיסיים (Integer, single, boolean…) • Structs (נראה בהמשך) • אפילו מערכים (נראה בהמשך) • אוביקטים(נראה בקורסים הבאים) 0 1 2 3 4 5 6 7 8 9 scores

  5. 79 87 94 82 67 98 87 81 74 91 מערכים – גישה לתא בודד • מתייחסים לתא בודד במערך ע"י () • למשל scores(3) מתייחס לתא הרביעי במערך, וערכו 82. • המערך הזה הוא של Integer ולכן בתא בודד ניתן להשתמש כמו בכל Integer אחר • למשל: Dim x As Integer = scores(4) x = scores(0) + 10 0 1 2 3 4 5 6 7 8 9 scores

  6. 79 87 94 82 67 98 87 81 74 91 79 87 94 82 67 77 87 81 74 91 מערכים – שינוי ערך בתא בודד • אם רוצים לשנות את הערך של התא השישי ל 77: scores(5) = 77; 0 1 2 3 4 5 6 7 8 9 scores 0 1 2 3 4 5 6 7 8 9 scores

  7. 79 87 94 82 67 98 87 81 74 91 מערך – הצהרה ויצירה • ניתן להצהיר על מערך באופן הבא: Dim scores(9) As Integer • הצהרה כנ"ל מגדירה מערך עם 10 תאים 0 1 2 3 4 5 6 7 8 9 scores

  8. דוגמא 1 – הגדרת מערך ושימוש בו Sub Main() Dimarr(4) AsInteger arr(0) = 2 arr(1) = 32 arr(2) = 4 arr(3) = 0 arr(4) = 81 Console.WriteLine("The number in the third place: " & arr(2)) arr(1) = Console.ReadLine() Console.WriteLine("The number in the second place: " & arr(1)) EndSub

  9. דוגמא 2 – הדפסת הערכים בכל תאי המערך Sub Main() Dimarr(4) AsInteger arr(0) = 2 arr(1) = 32 arr(2) = 4 arr(3) = 0 arr(4) = 81 Fori = 0 To 4 Console.WriteLine("arr in place i is: " & arr(i)) Next EndSub

  10. גבולות המערך • נניח שהגדרנו מערך באופן הבא: Dim scores(10) As Integer • אומרים שגבולות המערך הן בין 0 ל 10 • אסור לגשת לתא מחוץ למערך • גישה לתא מחוץ למערך גורמת לשגיאה והתוכנית תעוף!!

  11. דוגמא 2 – ניסיון שגוי Sub Main() Dimarr(4) AsInteger arr(0) = 2 arr(1) = 32 arr(2) = 4 arr(3) = 0 arr(4) = 81 Fori = 0 To5 Console.WriteLine("arr in place i is: " & arr(i)) Next EndSub מחוץ לגבול

  12. אורך מערך • אם הגדרנו מערך באופן הבא: Dim scores(9) As Integer • ניתן למצוא את אורך המערך ע"י שימוש בפונקציה Length Dim a As Integer a = scores.Length() (ב a יהיה 10)

  13. דוגמא 2 – הימנעות מחציית גבולות Sub Main() Dimarr(4) AsInteger arr(0) = 2 arr(1) = 32 arr(2) = 4 arr(3) = 0 arr(4) = 81 Fori = 0 Toarr.Length() - 1 Console.WriteLine("arr in place i is: " & arr(i)) Next EndSub

  14. רשימת אתחול • ניתן ליצור מערך ומיד למלא בו ערכים ע"י שימוש ברשימת אתחול Dim scores() As Integer = {79, 87, 94, 82, 67, 98, 87, 81, 74, 91} • הערכים מופרדים ע"י פסיק • גודל המערך נקבע לפי אורך הרשימה • אפשר להשתמש ברשימת אתחול רק בזמן הגדרת המערך • אפשר גם לטיפוסים אחרים, למשל: Dim words() As String = {"one", "two", "three"}

  15. תרגיל – קליטה למערך והדפסה • קלוט 10 מספרים ממשתמש, והדפס אותם בסדר הפוך • אלגוריתם: • נגדיר מערך באורך 10 • בצע 10 פעמים (לולאת for) • קלוט מספר מהמשתמש • שמור את המספר במקום הבא במערך • בצע 10 פעמים (לולאת for) • הדפס את המספר הבא במערך • יש לשים לב בכל לולאה מהם גבולות המערך ומהו כיוון ההתקדמות

  16. פתרון התרגיל Sub Main() Dimnums(9) AsInteger Console.WriteLine("Please enter 10 numbers") Fori = 0 Tonums.Length() - 1 nums(i) = Console.ReadLine() Next Console.WriteLine("The numbers in revered order are") Fori = nums.Length() - 1 To 0 Step -1 Console.Write(nums(i) & ", ") Next Console.WriteLine() EndSub

  17. 0 1 2 3 4 0 1 2 3 4 2 2 4 10 5 5 9 9 7 5 arr1 arr2 תרגיל – בדיקה אם מערך הוא ממוין • נרצה לבדוק אם המערך arrממוין בסדר עולה ממש • נקבע משתנה sorted להיות true אם המערך ממוין בסדר עולה • ז"א שהערך בכל תא גדול ממש (לא שווה) לערכים שלפניו • אחרת sorted יהיה false ממוין בסדר עולה (sorted = true) לא ממוין בסדר עולה (sorted = false)

  18. 0 1 2 3 4 2 4 5 9 7 arr1 תרגיל – בדיקה אם מערך הוא ממוין - אלגוריתם • פתרון: • נניח שהמערך ממוין • נאתחל את sorted להיות true • נעבור על כל תא • נבדוק אם התא גדול מהתא שאחריו • אם כן נעדכן את sorted להיות false 2>4 ? 4>5 ? 5>7 ? 7>9 ? • מה לגבי התא האחרון? • נעצור בתא אחד לפני האחרון כדי למנוע שגיאה בזמן ריצה 9>_ ?

  19. פתרון התרגיל Sub Main() Dim nums() AsInteger = {2, 3, 5, 5, 9} Dim flag AsBoolean = True Fori = 0 Tonums.Length() - 2 Ifnums(i) >= nums(i + 1) Then flag = False EndIf Next If (flag) Then Console.WriteLine("array is sorted") Else Console.WriteLine("array is NOT sorted") EndIf EndSub

  20. חישוב סכום המספרים וכן מציאת מקסימום במערך Sub Main() Dim x(9) AsInteger DimiAsInteger Fori = 0 To 9 x(i) = Console.ReadLine() Next Dim sum AsInteger = 0 Dim max = x(0) Fori = 0 To 9 sum += x(i) If (x(i) > max) Then max = x(i) EndIf Next Console.WriteLine(“Sum is {0} and Max is {1} ", sum, max) EndSub

  21. מערך דינמי (שינוי אורך מערך) Resize • מה לעשות במקרה שלא יודעים מראש מהו אורך המערך • רוצים לשנות אורך תוך כדי התוכנית • ניתן להשתמש בפקודת Resize Dim x(9) AsInteger Console.WriteLine("the length is: " & x.Length()) Array.Resize(x, 20) Console.WriteLine("now the length is: " & x.Length())

  22. תרגיל עם מערך דינמי SubMain() Dimx() AsInteger = {} Dimanswer AsString = "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() EndIf EndWhile DimiAsInteger Fori = 0 Tox.Length - 1 Console.WriteLine("In position {0} I have {1} ", i, x(i)) Next EndSub

  23. תרגיל עם מערך דינמי - פלט

  24. שימוש בפונקציות המוגדרות על מערך • ראינו שינוי גודל המערך Resize • מיון מערך Sort - • הפיכה - Reverse

  25. שימוש בפונקציות על מערכים Array. Sub Main() Dim x() AsInteger = {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)) Array.Reverse(x) Console.WriteLine("Position 0 is {0} ", x(0)) EndSub

  26. כתיבת פונקציות עם מערכים (בעצמנו) ModuleModule1 FunctionMax(ByVal x() AsInteger) AsInteger Dim temp AsInteger = x(0) DimiAsInteger Fori = 1 Tox.Length - 1 If x(i) > temp Then temp = x(i) EndIf Next Return temp EndFunction SubMain() Dim x() AsInteger = {1, 7, 5, 50, -1, 0, 100, -2} Console.WriteLine("The max is " & Max(x)) EndSub EndModule

  27. כתיבת פונקציות + שימוש בפונקציות קיימות SubPrint(ByVal x() AsInteger) DimiAsInteger Console.WriteLine("Begin the Print Array Sub") Fori = 0 Tox.Length - 1 Console.WriteLine("In position {0} I have {1} ", i, x(i)) Next Console.WriteLine("End of the Print Array Sub") EndSub SubMain() Dim x() AsInteger = {1, 7, 5, 50, -1} Print(x) Array.Resize(x, 10) Print(x) Array.Sort(x) Print(x) Array.Reverse(x) Print(x) EndSub

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

  29. דוגמא – מערך של מחרוזות Sub Main() Dimarr(3) AsString arr(0) = "Hi" arr(1) = "Bye" arr(2) = Console.ReadLine() arr(3) = arr(0) & arr(1) Fori = 0 Toarr.Length() - 1 Console.Write(arr(i) & " | ") Next Console.WriteLine() Fori = 0 Toarr.Length() - 1 Console.WriteLine("length of string i: " & arr(i).Length()) Next EndSub

  30. דוגמא נוספת – מערך של מחרוזות מה עושה התוכנית? SubMain() Dimx() AsString = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "shabat"} DimnumAsInteger num= Console.ReadLine() Console.WriteLine(x(num- 1)) EndSub

  31. עוד משחקי מחרוזות במערכים Sub Main() Dimwords() AsString = {"Hello", "Shalom", "Bye", "Lehitraot"} ForiAsInteger = 0 Towords.Length() - 1 Console.WriteLine(words(i).Length()) Next ForiAsInteger = 0 Towords.Length() - 1 IfiMod 2 = 0 Then Console.WriteLine(words(i).Replace("e", "o")) Else Console.WriteLine(words(i).Insert(0, "*")) EndIf Next EndSub

  32. ומה עושה התוכנית הזאת? SubMain() Dimnames() AsString = {"Meir Levi", "Yoram Tal", "Ran Hilel"} Diminitials(names.Length()) AsString Dimp AsInteger ForiAsInteger = 0 Tonames.Length() - 1 Console.WriteLine(names(i)) Next ForiAsInteger = 0 Tonames.Length() - 1 initials(i) = names(i).Chars(0) + "." p = names(i).IndexOf(" ") initials(i) += names(i).Chars(p + 1) + "." Next ForiAsInteger = 0 Toinitials.Length() - 1 Console.WriteLine(initials(i)) Next EndSub

More Related