# Board index » delphi » algorithm to calculate the median for a sorted array

## algorithm to calculate the median for a sorted array

Hi,

I'm new at algorithms and i'm trying to do an algorithm to calculate the
median for a sorted array.

I've managed to figure out how to do the sort but when it comes to writing
the median I get totally lost.. I don't know how to call the Switch and
Selection Sort  procedures within the median algorithm.

Below is the class and at the bottom you'll see the two procedures for the
slection sort... Can you help please????

Thanks
Ellan

unit Statistics;

interface
uses
StudentClass;

Type
StatisticsClass = class
public
Constructor createsubject(insubjectname:string);
procedure Removestudent(inname:string);
procedure changestudentdetails(inname:string; inmark,inage:integer);
procedure getstudentdetails(Inname:string; var outMark,outage:integer);
Function MaximumMark:integer;
Function MaximumAge:integer;
Function MinimumMark:integer;
Function MinimumAge:integer;
Function MeanMark:real;
Function MeanAge:real;
Procedure Median;

Private
statistics:array[1..50] of Examclass;
SubjectName:string;

end;
{Procedure switch(num1,num2:integer);
procedure Selectionsort(var sarray:sortarray; n:word);}
implementation
Constructor StatisticsClass.createsubject(insubjectname:string);
var
count:integer;
begin
create;
SubjectName := inSubjectName;
for count := 1 to 50 do
begin
Statistics[count] :=  Nil;
end;
end;

var
count :integer;
begin
count := 1;
while (statistics[count] <> nil) and (count < 50) do
begin
count := count + 1;
end;
if statistics[count] = nil then
begin
statistics[count] := examclass.createstudent(inname,
inmark,inage);
end;
end;

procedure statisticsclass.removestudent(inname:string);
var
count :integer;
found:boolean;
begin
count := 1;
found := false;
begin
if statistics[count].outname = inname then
begin
found := true;
end
else
begin
count := count + 1;
end
end;
if found then
begin
statistics[count] := nil;
end;
end;

procedure statisticsclass.changeStudentdetails(inname:string;
inmark,inage:integer);

var
count :integer;
found:boolean;
begin
count := 1;
found := false;
begin
if statistics[count].outname = inname then
begin
found := true;
end
else
begin
count := count + 1;
end
end;
if found then
begin
statistics[count].changedetails(inmark,inage);
end;
end;

procedure statisticsclass.getstudentdetails(Inname:string; var
outMark,outage:integer);
var
count :integer;
found:boolean;
tempName:string;
begin
count := 1;
found := false;
begin
if statistics[count] <> nil then
begin
if statistics[count].outname = inname then
begin
found := true;
end
else
begin
count := count + 1;
end
end
else
count := count + 1;
if found then
begin
statistics[count].getdetails(tempname,
outmark,outage);
;
end;
end;
end;

Function StatisticsClass.MaximumMark:integer;

var
count,
max:integer;
begin
max := statistics[1].getmark;
for count := 1 to 50 do
if (statistics[count] <> nil) then
if statistics[count].getmark > max then
max := statistics[count].getmark;

maximumMark := max;
end;

Function statisticsclass.MaximumAge:integer;
var
count,
max:integer;
begin
max := statistics[1].getage;
for count := 1 to 50 do
if (statistics[count] <> nil) then
if statistics[count].getage > max then
max := statistics[count].getage;

maximumage := max;
end;

Function StatisticsClass.minimumMark:integer;

var
count,
min:integer;
begin
min := statistics[1].getmark;
for count := 1 to 50 do
if (statistics[count] <> nil) then
if statistics[count].getmark < min then
min := statistics[count].getmark;

minimummark := min;
end;
Function statisticsclass.MinimumAge:integer;
var
count,
min:integer;
begin
min := statistics[1].getage;
for count := 1 to 50 do
if (statistics[count] <> nil) then
if statistics[count].getage < min then
min := statistics[count].getage;

minimumage := min;
end;

function statisticsclass.meanmark:real;
var
count:integer;
total:real;
pos:integer;

begin
pos := 0;
total := 0;
for count := 1 to 50 do
if (statistics[count] <> nil) then
begin
total := total + statistics[count].getmark;
pos := pos + 1;
end ;

meanmark := total/pos;

end;

function statisticsclass.meanage:real;
var
count:integer;
total:real;
pos:integer;

begin
pos := 0;
total := 0;
for count := 1 to 50 do
if (statistics[count] <> nil) then
begin
total := total + statistics[count].getage;
pos := pos + 1;
end;
meanage := total/pos;
end ;

Procedure switch(var num1,num2:integer);
var
temp:integer;
begin
temp := num1;
num1 := num2;
num2 := temp;
end;

procedure Selectionsort(var sarray : array of integer; n:integer);
var
fill,next,
indexofmin:integer;
begin
for fill :=  1 to n-1 do
begin
indexofmin := fill;
for next := fill + 1 to n do
if sarray[next] < sarray[indexofmin] then
indexofmin := next;
if indexofmin <> fill then
switch (sarray[fill], sarray[indexofmin]);

end;
end;
Procedure statisticsclass.Median;
begin
end;
end.

## Re:algorithm to calculate the median for a sorted array

##### Quote
Ellan Goddard wrote:

> Hi,

> I'm new at algorithms and i'm trying to do an algorithm to calculate the
> median for a sorted array.

If you have a sorted array, and you know the definition of
"median", then you can simply write down the median (assuming you
know N, the number of points).  Note that if N is even, one generally
uses the number between the two middle values.

Bob Schor

## Re:algorithm to calculate the median for a sorted array

If you have an array that is already sorted you can obtain the median by
picking a number from its very middle.

Nik.