160 likes | 296 Views
מערכים דו ממדי ו STRUCTS. פונקציות בוליאנית. 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
E N D
פונקציות בוליאנית 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 Sub End Module
שינוי בגודל המערך Module Module1 Sub Print(ByVal x() As Integer) 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 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 Sub End Module
והנה בעיה כתוב תוכנית אשר תקלוט מספר לא ידוע של מספרים ותדפיס ... מבוא לתכנות לתו"נ- שבוע מספר 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 Sub End Module
2 8 9 3 4 0 5 1 0 4 1 1 1 0 1 2 3 9 9 8 7 2 1 6 מערך דו-ממדי הפנייה לתא במערך a (1,3) מי זה? 4 מבוא לתכנות למנע"ס - שבוע מספר 9- מאיר קומר - סמסטר ב' - תשס"ו
דוגמה של קוד Module Module1 Sub Main() Dim a(2, 3) As Integer Dim i, j As Integer For i = 0 To 2 For j = 0 To 3 Console.WriteLine("Enter value for row {0} value {1} ", i, j) a(i, j) = Console.ReadLine() Next Next Console.WriteLine("Value 1,3 is " & a(1, 3)) End Sub End Module
דוגמה נוספת Module Module1 Sub Main() Dim x(9, 9) As Integer Dim i, j As Integer Dim num As New Random() For i = 0 To 9 For j = 0 To 9 x(i, j) = num.Next(1, 10) Next Next For i = 0 To 9 Console.Write("Now in Row " & i & ":") For j = 0 To 9 Console.Write(" " & x(i, j) & " ") Next Console.WriteLine() Next End Sub End Module
חישוב הסכום של עמודה Module Module1 Sub Main() Dim x(9, 9) As Integer Dim i, j As Integer Dim num As New Random() Dim sum As Integer = 0 For i = 0 To 9 For j = 0 To 9 x(i, j) = num.Next(1, 10) Next Next j = Console.ReadLine() For i = 0 To 9 sum += x(i, j) 'j column set means row changes Next Console.WriteLine("The sum is " & sum) End Sub End Module
חישוב הסכום של שורה Module Module1 Sub Main() Dim x(9, 9) As Integer Dim i, j As Integer Dim num As New Random() Dim sum As Integer = 0 For i = 0 To 9 For j = 0 To 9 x(i, j) = num.Next(1, 10) Next Next i = Console.ReadLine() For j = 0 To 9 sum += x(i, j) 'i row set means column changes Next Console.WriteLine("The sum is " & sum) End Sub End Module
9 3 4 0 0 4 1 1 1 2 3 9 7 2 1 6 מערך דו-ממדי תרגיל קטן מבוא לתכנות למנע"ס - שבוע מספר 9- מאיר קומר - סמסטר ב' - תשס"ו
פתרון התרגיל Module Module1 Sub Main() Dim x(9, 9) As Integer Dim i, j As Integer Dim num As New Random() Dim sum As Integer = 0 For i = 0 To 9 For j = 0 To 9 x(i, j) = num.Next(1, 10) Next Next For i = 0 To 9 sum += x(i, i) 'One diagonal sum += x(i, 9 - i) 'Second diagonal Next Console.WriteLine("The sum is " & sum) End Sub End Module
Structs (Structures) Module Module1 Structure Oved Public name As String Public maskoret As Integer End Structure Sub Main() Dim x As Oved x.name = "Avi" x.maskoret = 1234 Dim People(10) As Oved People(0).name = "Yossi" People(1).name = "Moshe" People(2).name = "Lea" End Sub End Module
עם לולאה... Structure Oved Public name As String Public maskoret As Integer End Structure Sub Main() Dim People(5) As Oved Dim i As Integer For i = 0 To People.Length() - 1 Console.WriteLine("Please enter the person's name") People(i).name = Console.ReadLine Console.WriteLine("Please enter their maskoret") People(i).maskoret = Console.ReadLine Next Console.WriteLine("High " & PrintHighest(People, People.Length())) End Sub
ואם פונקציה Function PrintHighest(ByVal x() As Oved, ByVal length As Integer) As Integer Dim i As Integer Dim high As Integer = x(0).maskoret For i = 0 To length - 1 If x(i).maskoret > high Then high = x(i).maskoret End If Next Return high End Function