om3raltiparmak 0 Posted February 26 (edited) 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 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 February 26 by om3raltiparmak Share this post Link to post
om3raltiparmak 0 Posted February 26 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. 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
aehimself 396 Posted February 26 (edited) 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 February 26 by aehimself Share this post Link to post
om3raltiparmak 0 Posted February 26 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