I have two questions I need help with it: Questions: A) Create a VB.NET Console
ID: 3806003 • Letter: I
Question
I have two questions I need help with it:
Questions:
A) Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (
1) Prompt a message (using Console.WriteLine) to ask the user to input three integers.
(2) Call the built-in function Console.ReadLine() three times to get the user’s input.
(3) Convert the user’s input from strings to integers.
(4) Call the function Smallest, with the user’s input as its parameters.
(5) Display the result of the function Smallest in the Console window using Console.WriteLine.
You may assume that the user always correctly inputs integers from the keyboard, and your program does not have to check whether the user inputs something other than integers. Print your program and handwrite the user’s input and output at the end of the printout of your program.
B) Create a VB.NET Console Application that does the following:
(1) Create an Integer array which contains 10 elements.
(2) Assign the following values to the elements in the array: 9, 1, 8, 4, 2, 6, 7, 3, 5, 10
(3) Use the For-Each-loop to compute and printout the sum of all elements of the array in the Console window.
Print your program and handwrite the output at the end of the printout of your program.
Explanation / Answer
A)
'VB console application that prompts user to enter three integer
'values and finds the smallest integer value and print to console.
'Module1.vb
Module Module1
Sub Main()
'Declare variables of type integers
Dim a, b, c, small As Integer
Console.WriteLine("Enter a value")
'read as string and convert to integer
a = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter b value")
'read as string and convert to integer
b = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter c value")
'read as string and convert to integer
c = Convert.ToInt32(Console.ReadLine())
'call Smallest function
small = Smallest(a, b, c)
'Print small to console
Console.WriteLine("Smallest value : {0}", small)
'Read a key value
Console.ReadKey()
End Sub
'The function procedure that takes thress integer values
'and returns the smallest value among the three values
Function Smallest(ByVal a As Single, ByVal b As Single, ByVal c As Single) As
Single
Dim small As Single = 0
If a < b And a < c Then
small = a
ElseIf b < a And b < c Then
small = b
Else
small = c
End If
Return small
'end of function
End Function
'end of module
End Module
Sample output:
Enter a value
5
Enter b value
4
Enter c value
3
Smallest value : 3
----------------------------------------------------------------------
B)
'The VB.Net module that initializes the array of elements
'of type integers and find the sum of the values in the array
Module Module1
'Main method
Sub Main()
'declare and intialize an array of 10 elements
Dim arr As Integer() = {9, 1, 8, 4, 2, 6, 7, 3, 5, 10}
Dim total As Integer = 0
Dim index As Integer
'Calculate the total sum of the values in array,arr
For index = 0 To arr.Length - 1
'add element in arr to total
total += arr(index)
Next
'Print total to console
Console.WriteLine("Sum of Array : {0}", total)
Console.ReadKey()
End Sub
End Module
Sample Output:
Sum of Array :55