Matriz de números positivos y negativos Curso de Programación Visual Basic

Lección:

Matrices, Estructuras y Cadenas


Ejercicio:

Matriz de números positivos y negativos 5


Objetivo:

Cree un programa de Visual Basic para pedir al usuario 10 números reales y mostrar el promedio de los positivos y el promedio de los negativos.


Código:

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