exercícios resolvidos

exercícios resolvidos

por Flávio Soares Correa da Silva -
Número de respostas: 1

Oi,

um aluno enviou o código para resolver o problema proposto na última sexta-feira (dadas as notas de uma sala, determinar a menor nota, a maior nota e a média, e usar essas subs para calcular a distância entre a menor nota e a média e entre a maior nota e a média). O código tinha um pequeno erro e, depois de corrigido, funcionou. O programa corrigido segue abaixo:

Sub Maior(Quant As Integer, MaiorNota As Double)

MaiorNota = 0
i = 1

Do While (i <= Quant)
If (Cells(i, 1) > MaiorNota) Then
MaiorNota = Cells(i, 1)
End If
i = i + 1
Loop

Cells(2, 4) = MaiorNota

End Sub

Sub Menor(Quant As Integer, MenorNota As Double)

MenorNota = 10
i = 1

Do While (i <= Quant)
If (Cells(i, 1) < MenorNota) Then
MenorNota = Cells(i, 1)
End If
i = i + 1
Loop

Cells(3, 4) = MenorNota

End Sub

Sub Media(Quant As Integer, Media As Double)

Media = 0
i = 1

Do While (i <= Quant)
Media = Media + Cells(i, 1)
i = i + 1
Loop

Media = Media / Quant

Cells(4, 4) = Media

End Sub

Sub Diferenca()

Dim A As Double
Dim B As Double
Dim M As Double
Dim Q As Integer
Dim D1 As Double
Dim D2 As Double

Q = Cells(1, 4)

Call Media(Q, M)

Call Maior(Q, A)
D1 = A - M
Cells(2, 5) = D1

Call Menor(Q, B)
D2 = M - B
Cells(3, 5) = D2

End Sub

Em resposta à Flávio Soares Correa da Silva

Re: exercícios resolvidos

por Flávio Soares Correa da Silva -

Oi,

outro aluno pediu para eu resolver o exercício de colocar uma lista de números em ordem crescente. Esse exercício é um pouquinho mais complicado, recomendo a todos tentarem fazer antes de olhar a resposta. Se não conseguirem de jeito nenhum, olhem a resposta abaixo e vejam se a entendem.

Sub MenorValor(Ini As Integer, Fim As Integer)

Dim Menor As Integer
Dim LinhaMenor As Integer
Dim i As Integer

i = Ini

Do While (Cells(i, 4) = "x")
    i = i + 1
Loop

Menor = Cells(i, 1)
LinhaMenor = i

Do While (i <= Fim)
    If ((Cells(i, 1) <= Menor) And (Cells(i, 4) <> "x")) Then
        Menor = Cells(i, 1)
        LinhaMenor = i
    End If
    i = i + 1
Loop
 
Cells(2, 3) = Menor
Cells(LinhaMenor, 4) = "x"

End Sub

Sub Ordem()

Dim Quant As Integer
Dim i As Integer

Quant = Cells(1, 3)

i = 1

Do While (i <= Quant)
    Cells(i, 4) = ""
    i = i + 1
Loop

i = 1

Do While (i <= Quant)
    Call MenorValor(1, Quant)
    Cells(i, 2) = Cells(2, 3)
    i = i + 1
Loop

End Sub