Console Array

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
So I've finished a few more chapters of my Visual Basic 2005 How To Program book and I've done all of the end of chapter "practice" programs. I am stuck on one though. Here are the instructions :


Write a Visual Basic Console application that prompts the user for 10 whole numbers. If this number has not already been entered by the user, the program should store the value in an array. After 10 numbers have been entered, display a complete list of the unique numbers entered by the user.

NOTE: Be sure to provide for the worse case scenario, when your user enters 10 unique values.

Your program should be broken into at least the following methods:

* GetInput - this Function will read a number from the Console and return it
* WasNumberEntered - this Function will check through the array for the number and return True if it was entered or False if it wasn't.
* DisplayNumbers - this subroutine will display a list of unique values entered by the user

Although not required to look this way, your program output might look like:

Please enter 10 whole numbers:
Please enter a value: 4
Please enter a value: 2
Please enter a value: 6
Please enter a value: 5
Please enter a value: 2
Please enter a value: 5
Please enter a value: 2
Please enter a value: 5
Please enter a value: 2
Please enter a value: 5

The values you entered were:
4
2
6
5
 
Last edited by a moderator:
The problem can be solved very simply without any subs or numbers. I just used strings throughout since no math processing is involved. You can elaborate as you wish and turn some of the code into subs as indicated in the description. You can also convert the string input into numbers and use an integer array. But this working solution should get you started:


VB.NET:
Expand Collapse Copy
   Sub Main()
        Dim dup As Boolean
        Dim unique As String = ""
        Dim tenvals(10) As String
        Console.WriteLine("Please enter 10 whole numbers.")
        For x As Integer = 1 To 10
            dup = False
            Console.Write("Enter value #" & x & ":  ")
            tenvals(x) = Console.ReadLine
            For y As Integer = 1 To x - 1
                If tenvals(y) = tenvals(x) Then dup = True
            Next
            If dup = False Then unique &= tenvals(x) & vbCrLf
        Next
        Console.WriteLine("The unique values you entered were:")
        Console.WriteLine(unique)
        Console.ReadLine()
    End Sub
 
Last edited by a moderator:
What a nice beginner exercise in OOP ! :) In contrast to Solitaire I think this is all about writing and using those methods, and also about validating the input. But I also disagree with the instructions in using arrays, dynamic helpful collections manage regular array work these days. Below are my suggestions.

My GetInput implementation loop until a valid whole number is entered.
VB.NET:
Expand Collapse Copy
Private Function GetInput() As Integer
    Do
        Console.Write("Please enter a value:")
        Dim number As Integer
        If Integer.TryParse(Console.ReadLine, number) Then
            Return number
        Else
            Console.Write("Invalid input, try again. ")
        End If
    Loop
End Function
WasNumberEntered method check if given number exist in given numbers collection using its Contains method. This one is for the sake of the exercise, I'd rather just use "numbers.contains(number)" directly in main code.
VB.NET:
Expand Collapse Copy
Private Function WasNumberEntered(ByVal number As Integer, ByVal numbers As List(Of Integer)) As Boolean
    Return numbers.Contains(number)
End Function
DisplayNumbers method loop the numbers and display them.
VB.NET:
Expand Collapse Copy
Private Sub DisplayNumbers(ByVal numbers() As Integer)
    Console.WriteLine("The unique numbers you entered was:")
    For Each number As Integer In numbers
        Console.WriteLine(number.ToString)
    Next
End Sub
Main program logic, using the defined methods explained above the rest is pretty much self-explanatory:
VB.NET:
Expand Collapse Copy
Sub Main()
    Dim numbers As New List(Of Integer)
    Console.WriteLine("Please enter 10 whole numbers:")
    For i As Integer = 1 To 10
        Dim number As Integer = GetInput()
        If Not WasNumberEntered(number, numbers) Then
            numbers.Add(number)
        End If
    Next
    DisplayNumbers(numbers.ToArray)
    Console.WriteLine("Press any key to exit.")
    Console.ReadKey(True)
End Sub
It can also be discussed in regard to good OOP whether the numbers could be stored in a private class field and used by these private methods, or if, like in my suggestion, all needed data is provided to the methods through the parameters.
 
Back
Top