Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program bubbleSort;
- {$APPTYPE CONSOLE}
- uses
- SysUtils, StrUtils, Math;
- const
- IN_FILE = 'bubblesort.in';
- OUT_FILE = 'bubblesort.out';
- ENTRIES = 10;
- type
- numberArray = array of integer;
- var
- infile, outfile: textfile;
- username: string;
- lines, i, loops: integer;
- data: numberArray;
- function NoL: integer; // number of lines in infile
- var
- i: integer;
- value: string;
- begin
- i := 0;
- if not fileexists(IN_FILE) then begin
- Result := 0;
- exit;
- end;
- reset(infile);
- while NOT eof(infile) do begin
- readln(infile);
- i := i+1;
- end;
- Result := i;
- end;
- procedure construct;
- begin
- if NOT fileexists(IN_FILE) then begin // a little housekeeping
- writeln('[ERROR] File ', IN_FILE, ' does not exist.');
- readln;
- exit;
- end;
- assignfile(infile, IN_FILE);
- reset(infile);
- assignfile(outfile, OUT_FILE);
- rewrite(outfile);
- lines := NoL();
- writeln('What is your name?');
- readln(username);
- writeln;
- writeln(outfile, 'Run by ', username, '.');
- writeln(outfile);
- end;
- function fetchData(): numberArray; overload; // Pull all data from infile
- var
- i: integer;
- data: numberArray;
- begin
- if NOT fileexists(IN_FILE) then exit;
- reset(infile);
- setlength(data, lines);
- for i := 1 to lines do readln(infile, data[i]);
- Result := data;
- end;
- function fetchData(subint, list: integer): numberArray; overload; // Pull selected data from infile
- { parameters: subint: number of entries per list, list: which list [ex. 1: 1-10, 2: 11-20, etc] }
- var
- i, start: integer;
- data, ans: numberArray;
- begin
- if NOT fileexists(IN_FILE) then exit;
- reset(infile);
- data := fetchData();
- setlength(ans, subint);
- start := 1+subint*(list-1);
- for i := 1 to 10 do
- ans[i] := data[i+subint*(list-1)];
- fetchData := ans;
- end;
- function sort(data: numberArray; reverse: boolean): numberArray; // bubble sort the numbers
- var
- t: array[0..1] of integer;
- i, j: integer;
- begin
- j := 0;
- while j < length(data) do begin
- for i := 1 to (length(data) - j) do begin
- if reverse = false then begin // least to greatest
- if data[i-1] > data[i] then begin
- t[0] := data[i];
- t[1] := data[i-1];
- data[i-1] := t[0];
- data[i] := t[1];
- end;
- end
- else begin // greatest to least
- if (data[i] > data[i-1]) AND (i > 1) then begin
- t[0] := data[i-1];
- t[1] := data[i];
- data[i] := t[0];
- data[i-1] := t[1];
- end;
- end;
- end; //for
- j := j+1;
- end; // while
- sort := data;
- end;
- function mean(data: numberArray): real;
- var
- l, ans: real;
- i: integer;
- begin
- ans := 0;
- l := length(data);
- for i := 1 to length(data) do ans := ans+data[i];
- ans := ans/l;
- mean := ans;
- end;
- function median(data: numberArray): integer;
- begin
- data := sort(data, false);
- median := data[round(length(data)/2)];
- end;
- function mode(data: numberArray): integer;
- var
- i, max, value: integer;
- occurances: numberArray;
- begin
- data := sort(data, false);
- max := 0;
- setlength(occurances, data[length(data)]);
- for i := 1 to length(data) do
- occurances[data[i]] := occurances[data[i]]+1;
- for i := 0 to length(occurances) do begin
- if(occurances[i] > 0) AND (occurances[i] > max) then begin
- max := occurances[i];
- value := i;
- end;
- end;
- mode := value;
- end;
- procedure displayData(data: numberArray); overload; // Send data to outfile using LINE BREAKS
- var
- i: integer;
- begin
- for i := 1 to length(data) do writeln(outfile, data[i]);
- end;
- procedure displayData(data: numberArray; horizontal: boolean); overload; // Send data to outfile using COMMAS
- var
- i: integer;
- begin
- for i := 1 to length(data) do begin
- if i = length(data) then write(outfile, data[i])
- else write(outfile, data[i],', ');
- end;
- end;
- procedure destruct;
- begin
- if fileexists(IN_FILE) then begin
- writeln(outfile, 'This program was written by Michael MacLean 11/24/14.');
- closefile(outfile);
- writeln('Output saved to ', OUT_FILE,'.');
- readln;
- end;
- end;
- begin //driver
- construct;
- loops := floor(lines/ENTRIES); // number of times to loop code
- for i := 1 to loops do begin
- writeln(outfile, 'ORIGINAL DATA FOR LIST ', floatToStr(i), ': ');
- displayData(fetchData(ENTRIES, i), true);
- writeln(outfile); writeln(outfile);
- writeln(outfile, 'LIST ', floatToStr(i), ' SORTED FROM LEAST TO GREATEST: ');
- displayData(sort(fetchData(ENTRIES, i), false), true);
- writeln(outfile); writeln(outfile);
- writeln(outfile, 'LIST ', floatToStr(i), ' SORTED FROM GREATEST TO LEAST: ');
- displayData(sort(fetchData(ENTRIES, i), true), true);
- writeln(outfile); writeln(outfile);
- writeln(outfile, 'List ', floatToStr(i), '''s mean: ', mean(fetchData(ENTRIES, i)):0:0);
- writeln(outfile, 'List ', floatToStr(i), '''s median: ', median(fetchData(ENTRIES, i)));
- writeln(outfile, 'List ', floatToStr(i), '''s mode: ', mode(fetchData(ENTRIES, i)));
- writeln(outfile);
- end;
- destruct;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement