In: Computer Science
Write a program in Visual Basics that does the following:
Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.
Solution for the above program is provided below. If any doubt please comment below.
Visual Basic Code:
Module Module1
Sub Main()
' Declare variables
Dim value As Integer
Dim str As String
Dim i As Integer
Dim j As Integer
Dim flag As Boolean
' Declare array
Dim arr(20) As Integer
' Read 20 integers
For i = 0 To 19
' Read from input
str = Console.ReadLine()
' Convert to integer
value = CInt(str)
' Check value is between 10 and 100
If value < 10 Or value > 100 Then
i = i - 1
Else
flag = False
' Check the given value is alraedy read
For j = 0 To i
' If alraedy read set flag is true
If (value = arr(j)) Then
flag = True
End If
Next
' If not duplicate print the value
If flag = False Then
Console.WriteLine("Read Value " + CStr(i + 1) + ": " + str)
arr(i) = value
Else
i = i - 1
End If
End If
Next
Console.ReadLine()
End Sub
End Module
Output: