program TestStatistics;

uses
  sysutils, math;

const
  NUM_STUDENTS = 10;
  PASS_SCORE = 60;

type
  TScoreArray = array[1..NUM_STUDENTS] of Integer;

var
  scores: TScoreArray = (50, 48, 78, 70, 85, 95, 98, 62, 40, 60);
  i, j, temp, sum, maxScore, minScore, passCount: Integer;
  sortedScores: TScoreArray;
  mean, median, variance, stdDev, deviationValue: Double;

begin
  // --- 初期化と基本統計の計算 ---
  sum := 0;
  maxScore := scores[1];
  minScore := scores[1];
  passCount := 0;
  sortedScores := scores; // 中央値計算用に配列をコピー

  for i := 1 to NUM_STUDENTS do
  begin
    sum := sum + scores[i];
    
    if scores[i] > maxScore then maxScore := scores[i];
    if scores[i] < minScore then minScore := scores[i];
    if scores[i] >= PASS_SCORE then passCount := passCount + 1;
  end;

  mean := sum / NUM_STUDENTS;

  // --- 中央値の計算 (バブルソートで昇順に整列) ---
  for i := 1 to NUM_STUDENTS - 1 do
    for j := 1 to NUM_STUDENTS - i do
      if sortedScores[j] > sortedScores[j+1] then
      begin
        temp := sortedScores[j];
        sortedScores[j] := sortedScores[j+1];
        sortedScores[j+1] := temp;
      end;

  // 要素数が10（偶数）なので、中央の2つの値の平均を中央値とする
  median := (sortedScores[5] + sortedScores[6]) / 2.0;

  // --- 標準偏差の計算 ---
  variance := 0;
  for i := 1 to NUM_STUDENTS do
  begin
    variance := variance + sqr(scores[i] - mean);
  end;
  variance := variance / NUM_STUDENTS;
  stdDev := sqrt(variance);

  // --- 結果の出力 ---
  writeln('=== 全体の統計結果 ===');
  writeln('平均点   : ', mean:0:2);
  writeln('中央値   : ', median:0:2);
  writeln('最高点   : ', maxScore);
  writeln('最低点   : ', minScore);
  writeln('標準偏差 : ', stdDev:0:2);
  writeln('合格者数 : ', passCount, '名 (', PASS_SCORE, '点以上)');
  writeln;

  writeln('=== 各学生の偏差値 ===');
  for i := 1 to NUM_STUDENTS do
  begin
    // 標準偏差が0でないことを確認して偏差値を計算
    if stdDev > 0 then
      deviationValue := 50.0 + 10.0 * (scores[i] - mean) / stdDev
    else
      deviationValue := 50.0;

    writeln('学生 ', i:2, ' (点数: ', scores[i]:2, ') -> 偏差値: ', deviationValue:0:2);
  end;
end.