Recent

Author Topic: Bubble Sort for string and integers - Problem with conversion to ascii  (Read 10518 times)

guest57137

  • Guest
Hello,

i am programming an application for comparing differnet sorting algorithms. I am importing my input from textfile into a memo box. Strings as well as integers have to be sorted. Therefore, I want to transform my inputs into ascii and back. However, I get an error here:
Code: [Select]
a[j]:= ord(a[j]);  Ordinal expression expected.

Here is my code:

Code: [Select]
unit vgl_alg_unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ActnList, Crt;

type
 RetArStr = AnsiString;
  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    OpenDialog1: TOpenDialog;
    OpenDialog2: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    function BubbleSort: RetArStr;
  end;

var
  Form1: TForm1;
  filename, sort1, sort2, s : string;
  dname : textfile;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Filter := 'All Files|*.txt;';
if OpenDialog1.Execute then
     try
        memo1.Lines.LoadFromFile(opendialog1.filename);
     except
       on E: EInOutError do
       writeln('Fehler aufgetreten. Details: ', E.Message);
     end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog2.Execute then
   begin
     filename := OpenDialog2.filename;
     ShowMessage(filename);
   end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   case ComboBox1.ItemIndex of
    0: sort1 := BubbleSort;
    1: sort1:='Heap Sort';
    2: sort1:='Insertion Sort';
    3: sort1:='Quick Sort';
    4: sort1:='Selection Sort';
    5: sort1:='Shaker Sort';
    6: sort1:='Shell Sort';
   end;
   case ComboBox2.ItemIndex of
    0: sort2:= BubbleSort;
    1: sort2:='Heap Sort';
    2: sort2:='Insertion Sort';
    3: sort2:='Quick Sort';
    4: sort2:='Selection Sort';
    5: sort2:='Shaker Sort';
    6: sort2:='Shell Sort';
   end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  memo1.clear;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin

end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin

end;

function TForm1.BubbleSort : RetArStr;
var
  i,c,j : integer;
  temp : string;
  a : array of string;
  merker : boolean;
  b : char;
  asc : byte;
begin
c := memo1.lines.count;
SetLength(a, c);
for j:=0 to length(a) do
  begin
  a[j] := memo1.lines[j];
  a[j]:= ord(a[j]);
  end;
repeat
  merker := false;
  for i:=0 to high(a)-1 do
    if a[i] > a[i+1] then
       begin
         temp := a[i];
         a[i] := a[i+1];
         a[i+1] := temp;
         merker := true;
       end;
until not merker;

memo2.clear;
 for i:= 0 to high(a) do
       begin
         memo2.lines.add(a[i]);
       end;
end ;

end.

I think it is a problem due to ainsi.... can someone give me a hint how to solve this?

Thanks a lot for your help in advance!  :)

taazz

  • Hero Member
  • *****
  • Posts: 5368
a is an array of string a string is not an ordinal value what do you expect a[j] to have?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

guest57137

  • Guest
actually a char... in order to get the ascii value

ok... is there a workaround in order to do this? Like

var
b:char

b:=a[j];

that does not seem to work...

taazz

  • Hero Member
  • *****
  • Posts: 5368
actually a char... in order to get the ascii value

ok... is there a workaround in order to do this? Like

var
b:char

b:=a[j];

that does not seem to work...
if "a" is an array of string then a[n] is a string so in order to access the chars in the a[n] you simple write
Code: [Select]
a[n][y] where n>=0, N<length(a) and x>0, x<length(a[n]);
« Last Edit: April 26, 2015, 12:17:02 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

guest57137

  • Guest
Thanks that helped. I added now this to my code:

Code: [Select]
var
  i,c,j,z : integer;
  temp : string;
  a : array of string;
  merker : boolean;
  b:char;
begin
c := memo1.lines.count;
SetLength(a, c);
for j:=0 to high(a) do
  begin
  a[j] := memo1.lines[j];
  for z:=0 to length(a[j])do
  b:= ord(a[j][z]);
  end;

and I get now this error: Incompatible types: got "Byte" expected "Char"
for this part
Code: [Select]
  b:= ord(a[j][z]);
 :-[

I am sorry.. I am sill learning

taazz

  • Hero Member
  • *****
  • Posts: 5368
Thanks that helped. I added now this to my code:

Code: [Select]
var
  i,c,j,z : integer;
  temp : string;
  a : array of string;
  merker : boolean;
  b:char;
begin
c := memo1.lines.count;
SetLength(a, c);
for j:=0 to high(a) do
  begin
  a[j] := memo1.lines[j];
  for z:=0 to length(a[j])do
  b:= ord(a[j][z]);
  end;

and I get now this error: Incompatible types: got "Byte" expected "Char"
for this part
Code: [Select]
  b:= ord(a[j][z]);
 :-[

I am sorry.. I am sill learning
How is b declared? if it is to keep a byte why declare it as char?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

typo

  • Hero Member
  • *****
  • Posts: 3051
Do you need to sort the integers as numbers or as strings?

guest57137

  • Guest
that is not specified. But since I get the numbers as strings due to textfile import it would be good to sort them as strings....

guest57137

  • Guest
Generally, my bubble sort works. Why I am doing all this is:

a) if I want to sort a list of "words", e.g. "hh, ab, Bc, bc", it will give me the following output: Bc, ab, bc, hh (due to ascii...)
b) if I want to sort a list of number that was importet as string, e. g. "12, 4, 88,2", it will give me the following output: 12, 2, 4, 88 (i think due to ascii, too...)

I was hoping, if I converted all my strings to ascii I could solve this problem.

Maybe someone has a better idea to solve this?

typo

  • Hero Member
  • *****
  • Posts: 3051
Here a BubbleSort procedure present on FPC examples:

Code: [Select]
PROCEDURE bubblesort;
{ like the head of a beer, reaaal slow and easy-going }
VAR i,j,max: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  max := num;
  REPEAT
    j := 1;
    FOR i := 1 TO max-1 DO
      IF descending(i,i+1) THEN BEGIN
        swapit(i,i+1); j := i;
      END;
    max := j;
  UNTIL (max=1) OR cancel;
  RestoreWin;
END; 

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #10 on: April 26, 2015, 04:44:46 pm »
A couple pointers in the right direction...

Compare the uppercase values of your strings & compare the integer values of your numbers, this will resolve your ascii issues.
A halo is a mere circle, when does it end?


guest57137

  • Guest
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #12 on: April 26, 2015, 04:59:38 pm »
Thans for the code extract. But it seems, one is just dealing with integer sorting and the other with string sorting. They are dealing not with integer and string sorting.

Maybe I'm getting it wrong... :-\

Is it better/easier to make two bubble sort algorithms? one for string and one for integer?

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #13 on: April 26, 2015, 05:01:57 pm »
Nope, your correct but my post does point you in the right direction.
A halo is a mere circle, when does it end?

guest57137

  • Guest
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #14 on: April 26, 2015, 05:04:30 pm »
Nope, your correct but my post does point you in the right direction.

I am already thinking about your hint :-)

 

TinyPortal © 2005-2018