Upload
eldora
View
44
Download
0
Embed Size (px)
DESCRIPTION
הרצאה 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. למה מערך?. - PowerPoint PPT Presentation
Citation preview
המחשב למדעי מבואוניהול לתעשייה
10הרצאה
2
מערך
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
1 לגודל המערך פחות 0המערך ממוספר מ
scores
שם המערך לכל ערך יש אינדקס
9 ל 0 ערכים, האינדקסים נעים בין 10במערך הנ"ל
? מערך למה משתנים של רב במספר להשתמש נרצה רבות פעמים
סוג מאותו... , סטודנטים טבלאות ציונים רשימות
סוג מאותו משתנים רשימת לשמור לנו מאפשר מערך ממוספרת במערך הרשימה
בנפרד מהאיברים אחד לכל לגשת ניתן
המערך לכל לגשת ניתן
) " בלולאות ) שימוש י ע במערך האיברים כל על לעבור ניתן
4
בתאים – יש מה ?מערכים
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
טיפוס מאותו הם במערך האיברים כל(Integer) שלנו בדוגמא
מערך להם להגדיר שאפשר הטיפוסים סוג על הגבלה אין( בסיסיים (…Integer, single, booleanטיפוסיםStructs) בהמשך ) נראה) בהמשך ) נראה מערכים אפילו) הבאים ) בקורסים נראה אוביקטים
scores
5
בודד – לתא גישה מערכים
0 1 2 3 4 5 6 7 8 9
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
scores
6
בודד – בתא ערך שינוי מערכים
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
ל השישי התא של הערך את לשנות רוצים :77אם
scores(5) = 77;
scores
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 77 87 81 74 91scores
7
: הבא באופן מערך על להצהיר ניתן
Dim scores(9) As Integer
" עם מערך מגדירה ל כנ תאים 10הצהרה
ויצירה – הצהרה מערך
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91scores
Sub Main()
Dim arr(4) As Integer
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))
End Sub
בו – 1דוגמא ושימוש מערך הגדרת
Sub Main()
Dim arr(4) As Integer
arr(0) = 2
arr(1) = 32
arr(2) = 4
arr(3) = 0
arr(4) = 81
For i = 0 To 4
Console.WriteLine("arr in place i is: " & arr(i))
Next
End Sub
המערך – 2דוגמא תאי בכל הערכים הדפסת
10
: הבא באופן מערך שהגדרנו נניח
Dim scores(10) As Integer
בין הן המערך שגבולות 10ל 0אומרים
למערך מחוץ לתא לגשת אסור!! תעוף והתוכנית לשגיאה גורמת למערך מחוץ לתא גישה
המערך גבולות
Sub Main()
Dim arr(4) As Integer
arr(0) = 2
arr(1) = 32
arr(2) = 4
arr(3) = 0
arr(4) = 81
For i = 0 To 5
Console.WriteLine("arr in place i is: " & arr(i))
Next
End Sub
שגוי – 2דוגמא ניסיון
לגבול מחוץ
12
: הבא באופן מערך הגדרנו אם
Dim scores(9) As Integer
" בפונקציה שימוש י ע המערך אורך את למצוא Lengthניתן
Dim a As Integer a = scores.Length()
(10יהיה aב)
מערך אורך
Sub Main()
Dim arr(4) As Integer
arr(0) = 2
arr(1) = 32
arr(2) = 4
arr(3) = 0
arr(4) = 81
For i = 0 To arr.Length() - 1
Console.WriteLine("arr in place i is: " & arr(i))
Next
End Sub
גבולות – 2דוגמא מחציית הימנעות
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פעמים במערך הבא המספר את הדפס
ההתקדמות כיוון ומהו המערך גבולות מהם לולאה בכל לב לשים יש
והדפסה – למערך קליטה תרגיל
Sub Main()
Dim nums(9) As Integer
Console.WriteLine("Please enter 10 numbers")
For i = 0 To nums.Length() - 1
nums(i) = Console.ReadLine()
Next
Console.WriteLine("The numbers in revered order are")
For i = nums.Length() - 1 To 0 Step -1
Console.Write(nums(i) & ", ")
Next
Console.WriteLine()
End Sub
התרגיל פתרון
17
המערך אם לבדוק עולה arr נרצה בסדר ממשממוין משתנה בסדר trueלהיות sortedנקבע ממוין המערך אם
עולה ) שלפניו" ) לערכים שווה לא ממש גדול תא בכל שהערך א ז
אחרתsorted יהיהfalse
ממוין – הוא מערך אם בדיקה תרגיל
0 1 2 3 4
2 4 5arr1 97
0 1 2 3 4
2 10 5arr2 95
עולה בסדר ממוין(sorted = true)
בסדר ממוין לא = sortedעולה )false)
18
אלגוריתם – - ממוין הוא מערך אם בדיקה תרגיל:פתרון
ממוין שהמערך נניח
את trueלהיות sortedנאתחל
תא כל על נעבור
שאחריו מהתא גדול התא אם נבדוק
את נעדכן כן falseלהיות sortedאם
0 1 2 3 4
2 4 5arr1 97
2>4?
4>5?
5>7?
7>9? ? האחרון התא לגבי מה
ריצה בזמן שגיאה למנוע כדי האחרון לפני אחד בתא נעצור
9? _>
Sub Main()
Dim nums() As Integer = {2, 3, 5, 5, 9}
Dim flag As Boolean = True
For i = 0 To nums.Length() - 2
If nums(i) >= nums(i + 1) Then
flag = False
End If
Next
If (flag) Then
Console.WriteLine("array is sorted")
Else
Console.WriteLine("array is NOT sorted")
End If
End Sub
התרגיל פתרון
Sub Main()
Dim x(9) As Integer
Dim i As Integer
For i = 0 To 9
x(i) = Console.ReadLine()
Next
Dim sum As Integer = 0
Dim max = x(0)
For i = 0 To 9
sum += x(i)
If (x(i) > max) Then
max = x(i)
End If
Next
Console.WriteLine (“Sum is {0} and Max is {1} ", sum, max)
End Sub
במערך מקסימום מציאת וכן המספרים סכום חישוב
21
המערך אורך מהו מראש יודעים שלא במקרה לעשות מה
התוכנית כדי תוך אורך לשנות רוצים
בפקודת להשתמש Resizeניתן Dim x(9) As Integer
Console.WriteLine("the length is: " & x.Length())
Array.Resize(x, 20)
Console.WriteLine("now the length is: " & x.Length())
) מערך ) אורך שינוי דינמי Resizeמערך
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 Sub
דינמי מערך עם תרגיל
פלט - דינמי מערך עם תרגיל
24
המערך גודל שינוי Resizeראינו
מערך - Sortמיון
- הפיכהReverse
מערך על המוגדרות בפונקציות שימוש
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))
Array.Reverse(x)
Console.WriteLine("Position 0 is {0} ", x(0))
End Sub
מערכים על בפונקציות . Arrayשימוש
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 Module
) בעצמנו ) מערכים עם פונקציות כתיבת
Sub Print(ByVal x() As Integer)
Dim i As Integer
Console.WriteLine("Begin the Print Array Sub")
For i = 0 To x.Length - 1
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 Sub
קיימות + בפונקציות שימוש פונקציות כתיבת
מחרוזות של מערך
Sub Main()
Dim arr(3) As String
arr(0) = "Hi"
arr(1) = "Bye"
arr(2) = Console.ReadLine()
arr(3) = arr(0) & arr(1)
For i = 0 To arr.Length() - 1
Console.Write(arr(i) & " | ")
Next
Console.WriteLine()
For i = 0 To arr.Length() - 1
Console.WriteLine("length of string i: " & arr(i).Length())
Next
End Sub
מחרוזות – של מערך דוגמא
Sub Main()
Dim x() As String = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "shabat"}
Dim num As Integer
num = Console.ReadLine()
Console.WriteLine(x(num - 1))
End Sub
? התוכנית – עושה מה מחרוזות של מערך נוספת דוגמא
Sub Main()
Dim words() As String = {"Hello", "Shalom", "Bye", "Lehitraot"}
For i As Integer = 0 To words.Length() - 1
Console.WriteLine(words(i).Length())
Next
For i As Integer = 0 To words.Length() - 1
If i Mod 2 = 0 Then
Console.WriteLine(words(i).Replace("e", "o"))
Else
Console.WriteLine(words(i).Insert(0, "*"))
End If
Next
End Sub
במערכים מחרוזות משחקי עוד
Sub Main()
Dim names() As String = {"Meir Levi", "Yoram Tal", "Ran Hilel"}
Dim initials(names.Length()) As String
Dim p As Integer
For i As Integer = 0 To names.Length() - 1
Console.WriteLine(names(i))
Next
For i As Integer = 0 To names.Length() - 1
initials(i) = names(i).Chars(0) + "."
p = names(i).IndexOf(" ")
initials(i) += names(i).Chars(p + 1) + "."
Next
For i As Integer = 0 To initials.Length() - 1
Console.WriteLine(initials(i))
Next
End Sub
? הזאת התוכנית עושה ומה