Exercise
Array of struct and menu
Objetive
Write a Visual Basic (VB.Net) program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to:
- Add data for one point
- Display all the entered points
- Calculate (and display) the average values for x and y
- Exit the program
Code
Imports System
Public Class exercise81
Structure points
Public x As Byte
Public y As Byte
End Structure
Private Shared Sub Main(ByVal args As String())
Dim p As points() = New points(999) {}
Dim Finish As Boolean = False
Dim countArray As Integer = 0
Dim TotalX As Integer = 0
Dim TotalY As Integer = 0
Do
Console.WriteLine("1.Add data for one point")
Console.WriteLine("2.Display all the entered points")
Console.WriteLine("3.Calculate (and display) the average values for x and y")
Console.WriteLine("0.Exit the program")
Console.Write("Enter a number: ")
Dim respuesta As Byte = Convert.ToByte(Console.ReadLine())
Select Case respuesta
Case 1
Console.WriteLine()
Console.Write("Enter a number for point x: ")
p(countArray).x = Convert.ToByte(Console.ReadLine())
TotalX += p(countArray).x
Console.WriteLine()
Console.Write("Enter a number for point y: ")
p(countArray).y = Convert.ToByte(Console.ReadLine())
TotalY += p(countArray).y
Console.WriteLine()
countArray += 1
Case 2
If countArray > 0 Then
For i As Integer = 0 To countArray - 1
Console.WriteLine("Valor x{0}: {1}", i + 1, p(i).x)
Console.WriteLine("Valor y{0}: {1}", i + 1, p(i).y)
Next
Else
Console.WriteLine("No hay datos")
End If
Case 3
If countArray > 0 Then
Console.WriteLine("Average x: {0}", TotalX / countArray)
Console.WriteLine("Average y: {0}", TotalY / countArray)
Else
Console.WriteLine("No hay datos")
End If
Case 0
Finish = True
Case Else
End Select
Loop While Not Finish
End Sub
End Class