Advertisement
alexcarrega

Solution Test Exam 2021/05/27

Jul 10th, 2021 (edited)
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. ' Average age of people working as 'impiegato'.
  4. ' Important: the work field is not written always in the same way. Sometimes lower case, other times upper case...
  5. Function esercizio_1() As Double
  6.     Dim i As Integer, n As Integer
  7.     esercizio_1 = 0
  8.     n = 0
  9.     For i = 2 To 21
  10.         If LCase(Cells(i, 4)) = "impiegato" Then
  11.             esercizio_1 = esercizio_1 + Cells(i, 3)
  12.             n = n + 1
  13.         End If
  14.     Next i
  15.     esercizio_1 = esercizio_1 / n
  16. End Function
  17.  
  18. ' Minimum age of people with name starts with "m" (or "M") and surname ends with "o" (or "O").
  19. Function esercizio_2() As Integer
  20.     Dim i As Integer
  21.     esercizio_2 = -1
  22.     For i = 2 To 21
  23.         If Left(LCase(Cells(i, 1)), 1) = "m" And Left(LCase(Cells(i, 2)), 1) = "o" And (Cells(i, 3) < esercizio_2 Or esercizio_2 = -1) Then
  24.             esercizio_2 = Cells(i, 3)
  25.         End If
  26.     Next i
  27. End Function
  28.  
  29. ' Standard deviation of people age that are students.
  30. Function esercizio_3() As Double
  31.     Dim i As Integer, n As Integer
  32.     Dim avg As Double
  33.     avg = 0
  34.     n = 0
  35.     For i = 2 To 21
  36.         If LCase(Cells(i, 4)) = "studente" Then
  37.             avg = avg + Cells(i, 3)
  38.             n = n + 1
  39.         End If
  40.     Next i
  41.     avg = avg / n
  42.     esercizio_3 = 0
  43.     For i = 2 To 21
  44.         If LCase(Cells(i, 4)) = "studente" Then
  45.             esercizio_3 = esercizio_3 + (Cells(i, 3) - avg) ^ 2
  46.             n = n + 1
  47.         End If
  48.     Next i
  49.     esercizio_3 = esercizio_3 / n
  50. End Function
  51.  
  52. ' Number of people with age gretear than the result of previous exercize.
  53. Function esercizio_4() As Integer
  54.     Dim i As Integer, ref As Double
  55.     ref = esercizio_3()
  56.     esercizio_4 = 0
  57.     For i = 2 To 21
  58.         If Cells(i, 3) < ref Then
  59.             esercizio_4 = esercizio_4 + 1
  60.         End If
  61.     Next i
  62. End Function
  63.  
  64. ' Number of people with age between the average age of student and "libero professionista".
  65. Function esercizio_5() As Integer
  66.     Dim i As Integer, n_stud As Integer, n_lib_prof As Integer
  67.     Dim avg_stud As Double, avg_lib_prof As Double
  68.     avg_stud = 0
  69.     avg_lib_prof = 0
  70.     n_stud = 0
  71.     n_lib_prof = 0
  72.     For i = 2 To 21
  73.         If LCase(Cells(i, 4)) = "studente" Then
  74.             avg_stud = avg_stud + Cells(i, 3)
  75.             n_stud = n_stud + 1
  76.         ElseIf LCase(Cells(i, 4)) = "libero professionista" Then
  77.             avg_lib_prof = avg_lib_prof + Cells(i, 3)
  78.             n_lib_prof = n_lib_prof + 1
  79.         End If
  80.     Next i
  81.     avg_stud = avg_stud / n_stud
  82.     avg_lib_prof = avg_lib_prof / n_lib_prof
  83.     esercizio_5 = 0
  84.     For i = 2 To 21
  85.         If (Cells(i, 3) >= avg_stud And Cells(i, 3) <= avg_lib_prof) Or (Cells(i, 3) <= avg_stud And Cells(i, 3) >= avg_lib_prof) Then
  86.             esercizio_5 = esercizio_5 + 1
  87.         End If
  88.     Next i
  89. End Function
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement