Array of positive and negative numbers Learn programming Visual Basic (VB.net)

Lesson:

Arrays, Structures and Strings


Exercise:

Array of positive and negative numbers 50


Objetive:

Create a Visual Basic (VB.net) program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones.


Code:

Imports System
Public Class exercise75
    Public Shared Sub Main()
        Dim numbers As Single() = New Single(9) {}
        Dim totalPositive As Single = 0.0F
        Dim totalNegative As Single = 0.0F
        Dim countPositive As Integer = 0
        Dim countNegative As Integer = 0

        For i As Integer = 0 To 10 - 1
            Console.Write("Number {0}= ", i + 1)
            numbers(i) = Convert.ToSingle(Console.ReadLine())
        Next

        For i As Integer = 0 To 10 - 1

            If numbers(i) < 0 Then
                totalNegative = totalNegative + numbers(i)
                countNegative += 1

                If numbers(i) > 0 Then
                    totalPositive = totalPositive + numbers(i)
                    countPositive += 1
                End If

                Console.WriteLine("Average numbers negatives = {0}", totalNegative / countNegative)
                Console.WriteLine("Average numbers positives = {0}", totalPositive / countPositive)
            End If
        Next
    End Sub
End Class