Baixe o app para aproveitar ainda mais
Prévia do material em texto
Gabaritos Gabarito para Exercício para Casa 02 Questao 1: Sub MediaColunaA2() Dim Valor As Double Dim Soma As Double Dim Media As Double Dim N As Integer Dim lin As Integer N = 25 Soma = 0 lin = 1 Whilei lin <= N Valor = Cells(lin, 1) Soma = Soma + Valor lin = lin + 1 Wend Media = Soma / N Cells(N+1, 1) = Media End Sub Questao 2: Resposta no final do cpítulo de Comando de seleção da apostila. Questao 3: Resposta no final do capítulo de Comando de seleção da apostila. Questao 4: Executando a macro escrita, você terá primeira linha de tabela de multiplicação 9X9. Agora se você trocar o comando i = 1 pelo i = 9 e executa-la, você terá a última linha de tabela. Então o segredo é a variável i variar 1 até 9, logo temos a resposta abaixo: Sub TabelaDeMult() Dim i As Integer, j As Integer i = 1 While <= 9 j = 1 While j <= 9 Cells(i,j) = i & "x" & j & "=" & i * j j = j + 1 Wend i = i+1 Wend End Sub Gabarito para Exercício 02 Exercício 1: Sub NelevadoM() Dim N As Integer Dim M As Integer Dim Prod As integer Dim cont As Integer N = Cells(1, 1) M = Cells(1, 2) Prod = 1 cont = 0 While cont < M Prod = Prod * N cont = cont + 1 Wend Cells(1, 3) = Prod End Sub Exercício 2: Sub DezPorcentoMais() Dim lin As Integer Dim num As Integer lin = 1 While lin <= 4 num = Cells(lin, 1) Cells(lin, 2) = num * 1.1 lin = lin + 1 Wend End Sub Exercício 3: Sub DezPorcentoMais() Dim lin As Integer Dim num As Integer Dim N As Integer lin = 1 N = Cells(1, 3) While lin <= N num = Cells(lin, 1) Cells(lin, 2) = num * 1.1 lin = lin + 1 Wend End Sub Exercício 4: Considere X1, X2, ... ,Xn e seja M a média dos X1~Xn, então o desvio padrão dos X1~Xn é: {[(x1-M)^2 + ... +(Xn-M)^2]/(N-1)}^(1/2) Sub DesvioPadrao() Dim lin As Integer Dim N As Integer Dim X As Double Dim Soma as Double Dim Media As Double Dim DP As Double N = Cells(1, 3) Soma = 0 lin = 1 While lin <= N X = Cells(lin, 1) Soma = Soma + X lin = lin + 1 Wend Media = Soma / N Soma = 0 lin = 1 While X = Cells(lin, 1) Soma = Soma + (X - Media)^2 lin = lin + 1 Wend DP = ( Soma/ (N-1) )^(1/2) Cells(N+2, 1) = DP End Sub Exercícios de MAC113 Lista 02 Exercício 1 Faça uma macro que realiza o seguinte: Le um inteiro positivo, N, na célula A1 Le outro inteiro positivo, M, na célula B1 Imprime na célula C1 N elevado a M. Gabarito para Exercício 03 Exercício 1: Sub ClassificaConsumo1A() Dim lin As Integer Dim PorcentDeGasto As Double lin = 1 Whilei lin <= 11 PorcentDeGasto = Cells(lin, 1) If PorcentDeGasto <= 0.25 Then Cells(lin, 2) = ''Esbanjador'' ElseIf PorcentDeGasto <= 0.53 Then Cells(lin, 2) = ''Gastador'' ElseIf PorcentDeGasto <= 0.75 Then Cells(lin, 2) = ''Comedido'' Else Cells(lin, 2) = ''Essencila'' End If lin = lin + 1 Wend End Sub Sub ClassificaConsumo1B() Dim lin As Integer Dim PorcentDeGasto As Double lin = 1 Whilei lin <= 11 PorcentDeGasto = Cells(lin, 1) If PorcentDeGasto <= 0.25 Then Cells(lin, 2) = ''Esbanjador'' End If If (0.25 < PorcentDeGasto) AND (PorcentDeGasto <= 0.53) Then Cells(lin, 2) = ''Gastador'' End If If (0.53 < PorcentDeGasto) AND (PorcentDeGasto <= 0.75) Then Cells(lin, 2) = ''Comedido'' End If If 0.75 < PorcentDeGasto Then Cells(lin, 2) = ''Essencila'' End If lin = lin + 1 Wend End Sub Exercício 2: Sub ClassificaConsumo2A() Dim lin As Integer Dim PorcentDeGasto As Double lin = 1 Whilei lin <= 11 PorcentDeGasto = Cells(lin, 1) If PorcentDeGasto < 0.20 Then Cells(lin, 2) = ''Esbanjador'' ElseIf (0.25 < PorcentDeGasto) AND (PorcentDeGasto < 0.53) Then Cells(lin, 2) = ''Gastador'' ElseIf (0.53 < PorcentDeGasto) AND (PorcentDeGasto < 0.75) Then Cells(lin, 2) = ''Comedido'' ElseIf (0.80 < PorcentDeGasto) AND (PorcentDeGasto < 0.95) Then Cells(lin, 2) = ''Essencila'' Else Cells(lin, 2) = ''Indefinido'' End If lin = lin + 1 Wend End Sub Sub ClassificaConsumo2B() Dim lin As Integer Dim PorcentDeGasto As Double lin = 1 Whilei lin <= 11 PorcentDeGasto = Cells(lin, 1) If PorcentDeGasto < 0.20 Then Cells(lin, 2) = "Esbanjador" ElseIf PorcentdeGasto <= 0.25 Then Cells(lin, 2) = "Indefinido" ElseIf PorcentdeGasto < 0.45 Then Cells(lin, 2) = "Gastador" ElseIf PorcentdeGasto <= 0.53 Then Cells(lin, 2) = "Indefinido" ElseIf PorcentdeGasto < 0.75 Then Cells(lin, 2) = "Comedido" ElseIf PorcentdeGasto <= 0.80 Then Cells(lin, 2) = "Indefinido" ElseIf PorcentdeGasto < 0.95 Then Cells(lin, 2) = "Essencial" Else Cells(lin, 2) = "Indefinido" End If lin = lin + 1 Wend End Sub Sub ClassificaConsumo2C() Dim lin As Integer Dim PorcentDeGasto As Double lin = 1 Whilei lin <= 11 PorcentDeGasto = Cells(lin, 1) If PorcentDeGasto < 0.20 Then Cells(lin, 2) = ''Esbanjador'' End If If (0.25 < PorcentDeGasto) AND (PorcentDeGasto < 0.53) Then Cells(lin, 2) = ''Gastador'' End If If (0.53 < PorcentDeGasto) AND (PorcentDeGasto < 0.75) Then Cells(lin, 2) = ''Comedido'' End If If (0.80 < PorcentDeGasto) AND (PorcentDeGasto < 0.95) Then Cells(lin, 2) = ''Essencila'' End If If ((0.20 <= PorcentDeGasto) AND (PorcentDeGasto <=0.25)) OR ((0.45 <= PorcentDeGasto) AND (PorcentDeGasto <= 0.53)) OR ((0.75 <= PorcentDeGasto) AND (PorcenteDeGasto <= 0.80 )) Then Cells(lin, 2) = ''Indefinido'' End If lin = lin + 1 Wend End Sub Exercício 3: Sub ClassifRaiz() Dim a As Double Dim b As Double Dim c As Double Dim Delta As Double Dim lin As Integer lin = 1 a = Cells(lin, 1) b = Cells(lin, 2) c = Cells(lin, 3) While a <> 0 Delta = b^2-4*a*c If Delta < 0 Then Cells(lin, 4) = ''Nao ha raizes reais'' ElseIf Delta = 0 Then Cells(lin, 4) = a-b/(2*a) Cells(lin, 5) = ''Uma unica raiz'' Else Cells(lin, 4) = (-b-Delta^2)/(2*a) Cells(lin, 5) = (-b+Delta^2)/(2*a) End IF lin = lin+1 a = Cells(lin, 1) b = Cells(lin, 2) c = Cells(lin, 3) Wend End Sub Exercício 4: Sub valorAbs() Dim x As Double Dim Vabs As Double 'O no. na clelula A1 e a resposta na celula B1 x = Cells(1, 1) If x < 0 Then Vabs = -x Else Vabs = x End If Cells(1, 2) = Vabs End Sub Exercício 5: Sub Divisivilidade() Dim A As Integer Dim B As Integer Dim Resto As Integer 'A na clelula A1, B na celula B1 e resposta na celula C1 A = Cells(1, 1) B = Cells(1, 2) Resto = A MODD B If Resto = 0 Then Cells(1, 3) = ''A divisivel por B'' Else Cells(1, 3) = ''A nao divisivel por B'' End If End Sub Exercício 6: Sub AnoBissexto() Dim ano As Integer 'O ano na clelula A1 e resposta na celula C1 ano = Cells(1, 1) If ( (ano MOD 4) = 0 AND (ano MOD 100 <> 0) ) OR (ano MOD 400 = 0) Then Cells(1, 3) = '' bissexto'' Else Cells(1, 3) = ''nao bissexto'' End If End Sub Gabarito para Exercício 04 Exercício 1: Sub Divisibilidade() Dim M As Integer Dim N As Integer Dim Resto As Integer M = Cells(1, 1) N = Cells(1, 2) Resto = M-N If Resto < 0 Then 'caso M < N Cells(2, 1) = ''O Resto e'' & M Else 'caso M <=N, o mais comum While Resto >= N Resto = Resto - N Wend If Resto = 0 Then Cells(2, 1) = ''O Resto e'' & Resto End If End If End Sub Exercício 2: Nesta questão tem delicado problema de arredondamento que vamos desconsiderar. Não sabemos qual é a cidade menor no princípio, o exemplo é apenas um caso particular. Vamos supor que as populações não são iguais. Sub CrescPop() Dim PopUa As Integer Dim PopNy As Integer Dim TaxaUa As Double Dim taxaNy As Double Dim ano As Integer PopUa = Cells(1, 1) PopNy = Cells(1, 2) TaxaUa = Cels(1, 3) TaxaNy = cells(1, 4) If PopUa < PopNy Then If TaxaUa <= TaxaNy Then Cells(1, 3) = ''impossivel'' Else ano=0 While PopUa <= PopNy PopUa = PopUa * (1+TaxaUa) PopNy = PopNy * (1+TaxaNy) ano = ano + 1 Wend Cells(1, 3) = ''ultrapassa em'' & ano & ''anos'' End If Else 'caso de PopUa > PopNy If TaxaUa >= TaxaNy Then Cells(1, 3) = ''impossivel'' Else ano=0 While PopUa >= PopNy PopUa = PopUa * (1+TaxaUa) PopNy = PopNy * (1+TaxaNy) ano = ano + 1 Wend Cells(1, 3) = ''ultrapassa em'' & ano & ''anos'' End If End Sub Exercício 3: Sub MultipDeIouJ() Dim i As Integre Dim j As Integer Dim n As Integer Dim lin As Integer Dim cont As Integer 'contador de no. de impressao Dim num As Integer 'numero candidato 'vamos supor que i esta na celula A1, j na celula B1, n na 'celula C1 e escrever a resposta na coluna A a partir da 3a.linha. i = Cells(1, 1) j = Cells(1, 2) n = Cells(1, 3) cont = 0 num = 1 lin = 3 while cont < n If (num MOD i)=0 OR (num MOD j)=0 Then Cells(lin, 1) = num lin = lin + 1 'mudar a linha cont = cont + 1 'conta o No. de impressao End If num = num + 1 'proximo candidato Wend End Sub
Compartilhar