(* Statistics/Basic *) block unit basic_stats: procedure (sample_size: integer); var r: arrayof real, sum, scale, mean, diff, sd: real, h: arrayof integer, h_sum, i, j, adj: integer; begin if sample_size < 1 then return fi; array r dim (1 : sample_size); array h dim (0 : 9); (* all zero by default *) sum := 0.0; h_sum := 0; (* Generate 'sample_size' random numbers in the interval [0, 1). Calculate their sum and in which box they will fall when drawing the histogram *) for i := 1 to sample_size do r(i) := random; sum := sum + r(i); h(entier(r(i) * 10)) := h(entier(r(i) * 10)) + 1 od; for i := 0 to 9 do h_sum := h_sum + h(i) od; (* Adjust one of the h() values if necessary to ensure h_sum = sample_size *) adj := sample_size - h_sum; if adj =/= 0 then for i := 0 to 9 do h(i) := h(i) + adj; if h(i) >= 0 then exit fi; h(i) := h(i) - adj od; fi; mean := sum / sample_size; sum := 0.0; (* Now calculate their standard deviation *) for i := 1 to sample_size do diff := r(i) - mean; sum := sum + diff * diff od; sd := sqrt(sum / sample_size); (* Draw a histogram of the data with interval 0.1. If sample size > 500 then normalize histogram to 500. *) scale := 1.0; if sample_size > 500 then scale := 500.0 / sample_size fi; writeln("Sample size", sample_size); writeln(" Mean ", mean: 8: 6, " SD ", sd: 8: 6); for i := 0 to 9 do write(" ", i / 10.0: 4: 2, " : ", h(i): 5, " "); for j := 1 to entier(h(i) * scale + .5) do write("*") od; writeln od; end basic_stats; begin call basic_stats(100); writeln; call basic_stats(1000); writeln; call basic_stats(10000); writeln; end;