Jump to content
om3raltiparmak

Find Max, Min and Avarage in Memo or Edit Values

Recommended Posts

Hi all,

 

I am trying to get the maximum value, minimum value and average in the values in a string and separated by ; but it gives an error. :classic_unsure:

 

I am waiting for help.

 

Example code ;
 

Error :


---------------------------
'1.5' is not a valid floating point value.
---------------------------

 

 

Memo text :
1,5;2,1;1,6;
1,1;1,3;1;
1,9;0,1;0,5;

 

var
Data: array of Double;
MinValue, MaxValue, Sum: Double;
Average: Double;

 

var i : integer;
s : string;
strArray : TArray<string>;
charArray : Array[0..0] of Char;
begin

s := Memo1.Text;
charArray[0] := ';';
strArray := s.Split(charArray);

i := 0;
for i := 0 to Length(strArray) do
begin
Data[i] := StrToFloat(strArray[i]);
end;

 

var
  Count: Integer;
  i: Integer;
begin

  MinValue := Data[0];
  MaxValue := Data[0];
  Sum := 0;
  Count := Length(Data);

  for i := 0 to Count - 1 do
  begin
    if Data[i] < MinValue then
      MinValue := Data[i];

    if Data[i] > MaxValue then
      MaxValue := Data[i];

    Sum := Sum + Data[i];
  end;

  Average := Sum / Count;

  Edit1.Text := FloatToStr(MinValue);
  Edit2.Text := FloatToStr(MaxValue);
  Edit3.Text := FloatToStr(Average);
  Edit4.Text := FloatToStr(Sum);

 

Edited by om3raltiparmak

Share this post


Link to post
12 minutes ago, om3raltiparmak said:

Hi all,

 

I am trying to get the maximum value, minimum value and average in the values in a string and separated by ; but it gives an error. :classic_unsure:

 

I am waiting for help.

 

Example code ;
 

Error :

Memo text :
1,5;2,1;1,6;
1,1;1,3;1;
1,9;0,1;0,5;
---------------------------
'1.5' is not a valid floating point value.
---------------------------

 

AND

 

 

Memo text :
1,5;2;3,1

---------------------------

Range check error.

---------------------------

 


var
Data: array of Double;
MinValue, MaxValue, Sum: Double;
Average: Double;

 


var i : integer;
s : string;
strArray : TArray<string>;
charArray : Array[0..0] of Char;
begin

s := Memo1.Text;
charArray[0] := ';';
strArray := s.Split(charArray);

i := 0;
for i := 0 to Length(strArray) do
begin
Data[i] := StrToFloat(strArray[i]);
end;

 


var
  Count: Integer;
  i: Integer;
begin

  MinValue := Data[0];
  MaxValue := Data[0];
  Sum := 0;
  Count := Length(Data);

  for i := 0 to Count - 1 do
  begin
    if Data[i] < MinValue then
      MinValue := Data[i];

    if Data[i] > MaxValue then
      MaxValue := Data[i];

    Sum := Sum + Data[i];
  end;

  Average := Sum / Count;

  Edit1.Text := FloatToStr(MinValue);
  Edit2.Text := FloatToStr(MaxValue);
  Edit3.Text := FloatToStr(Average);
  Edit4.Text := FloatToStr(Sum);

 

 

Share this post


Link to post

One issue what already stands out:

for i := 0 to Length(strArray) do

This should be Length - 1.

 

This can give you a range check error.

Edited by aehimself

Share this post


Link to post
2 hours ago, om3raltiparmak said:

Hi all,

 

I am trying to get the maximum value, minimum value and average in the values in a string and separated by ; but it gives an error.

 

I keep getting errors.

 

I am waiting for help.

 

Example code ;
 

Memo text :
1,5;2,1;1,6;
1,1;1,3;1;
1,9;0,1;0,5; 

 


var
Data: array of Double;
MinValue, MaxValue, Sum: Double;
Average: Double;

 


var i : integer;
s : string;
strArray : TArray<string>;
charArray : Array[0..0] of Char;
begin

s := Memo1.Text;
charArray[0] := ';';
strArray := s.Split(charArray);

i := 0;
for i := 0 to Length(strArray) do
begin
ShowMessage(strArray[i]);

Data[i] := StrToFloat(strArray[i]);

end;

 


var
  Count: Integer;
  i: Integer;
begin

  MinValue := Data[0];
  MaxValue := Data[0];
  Sum := 0;
  Count := Length(Data);

  for i := 0 to Count - 1 do
  begin
    if Data[i] < MinValue then
      MinValue := Data[i];

    if Data[i] > MaxValue then
      MaxValue := Data[i];

    Sum := Sum + Data[i];
  end;

  Average := Sum / Count;

  Edit1.Text := FloatToStr(MinValue);
  Edit2.Text := FloatToStr(MaxValue);
  Edit3.Text := FloatToStr(Average);
  Edit4.Text := FloatToStr(Sum);

 

 

 

 

SetLength(Data, x); I forgot. I added it and it worked.

 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×