How to pass dynamic arrays in FreePascal correctly - parameter-passing

In this FreePascal code I write, I found that in a dynamic array of length 'n', it always contained a random value in element 'n'.
I understand why that is, however, I am wondering if perhaps there is a flaw in the way i've written my code. I've pasted it below. I'd welcome any corrections.
program LinearSearch;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils
{ you can add units after this };
{$R *.res}
type
ArrayOfByte = array of Byte;
Function findMaximum(studentMarks : ArrayOfByte; numberOfStudents : integer)
: integer;
var
maximumMark : Integer;
studentWithMaximumMark : Integer;
index : integer;
begin
setLength(studentMarks, numberOfStudents);
maximumMark := studentMarks[0];
for index:=0 to numberOfStudents do
begin
write(IntToStr(studentMarks[index]));
if index = numberOfStudents then writeln('.')
else write(',');
end;
for index:= 0 to (numberOfStudents - 1) do
begin
if studentMarks[index] > maximumMark then
begin
maximumMark := studentMarks[index];
studentWithMaximumMark := index;
end;
end;
write('Maximum score of ',IntToStr(maximumMark),' was achieved by ');
Result := studentWithMaximumMark+1;
end;
var
studentMarks : ArrayOfByte;
numberOfStudents : Integer;
studentWithMaximumMarks : Integer;
counter : integer;
begin
write('Enter the number of students: ' );
readln(numberOfStudents);
setLength(studentMarks,numberOfStudents);
writeln('Input the grades for the following students:');
for counter := 0 to (numberOfStudents - 1) do
begin
write('Student ',counter+1,': ');
readln(studentMarks[counter]);
end;
writeln('Data has been input. Finding student with maximum marks.');
studentWithMaximumMarks := findMaximum(studentMarks,numberOfStudents);
write('student no. ',IntToStr(studentWithMaximumMarks));
writeln;
writeln('Press ANY key to continue');
readln;
end.

In Free Pascal dynamic arrays are zero based. So the first element is MyArray[0] and last element is MyArray[High(MyArray)] which is equal to MyArray[Length(MyArray) - 1]. You should never access MyArray[Length(MyArray)].

...I found that in a dynamic array of length 'n', it always contained a random value in element 'n'. this is because you are accessing a value beyond of the bounds of the array, check in the procedure findMaximum the line
for index:=0 to numberOfStudents do
must be
for index:=0 to numberOfStudents-1 do
Also you can remove the first line of the same procedure setLength(studentMarks, numberOfStudents);

Related

Create tree structured JSON

I have a list of objects, of type TDepartment which looks like this
TDepartment = class
ID : Integer;
Name : string;
ParentDepartmentID : Integer;
end;
I need to create a TJSONObject, with an array of departments, which all can also have an array of departments. So the depth of this is unknown.
I am at a point right now where it simply doesn't make sense to me, but I would like the resulting JSON to look like this:
"department_id": "5",
"department_name": "100",
"parent_dept_id": "",
"subdepartments": [{
"department_id": "8",
"department_name": "300",
"parent_dept_id": "5",
"subdepartments": [{
"department_id": "1",
"department_name": "310",
"parent_dept_id": "8",
"subdepartments": []
Keep in mind that each level has unknown amount of siblings and children.
I guess i need to write a recursive procedure, but I am unable to visualize it.
First, you probably want your declaration of TDepartmentto match the nested structure you describe:
TDepartment = class
ID : Integer;
Name : string;
ParentDepartmentID : Integer;
SubDepartments: array of TDepartment;
end;
In order to serialize this I would recommend using the SuperObject library rather than the inbuilt JSON classes:
function TDepartment.Serialize: ISuperObject;
var Context: TSuperRttiContext;
begin
Context := TSuperRttiContext.Create;
try
Result := Context.AsJson<TDepartment>(self);
finally
Context.Free;
end;
end;
In the comments, OP mentioned that TDepartment contains a lot more fields, but only the ones in the question should be serialized; also TJSONObject has to be used, and a department does not know about its children. You could do something like that:
function TDepartment.Serialize2(AllDepartments: TList<TDepartment>): TJSONObject;
var Department: TDepartment;
Subdepartments: TJSONArray;
begin
Result := TJSONObject.Create;
Result.AddPair(TJSONPair.Create('department_id', TJSONNumber.Create(ID)));
Result.AddPair(TJSONPair.Create('department_name', Name));
Result.AddPair(TJSONPair.Create('parent_dept_id', TJSONNumber.Create(ParentDepartmentID)));
Subdepartments := TJSonArray.Create;
for Department in AllDepartments do
begin
if (Department.ParentDepartmentID <> ID) then Continue;
Subdepartments.AddElement(Department.Serialize2(AllDepartments));
end;
Result.AddPair(TJSONPair.Create('subdepartments', Subdepartments));
end;
I would create a parallel tree structure leaving the original intact. Your current structure is inverted as to what you need, so you scan through your current objects placing them in the tree. But without knowing the current structure this is difficult give sample code, but assuming all departments exist in some sort of list, (let us say called 'Departments') and the 'root' department has a parent department ID of zero it would go something like this:
unit Unit1;
interface
uses
System.Generics.Collections;
type
TDepartment = class
ID : Integer;
Name : string;
ParentDepartmentID : Integer;
end;
TDepartmentStructure = class
ID : Integer;
Name : string;
ParentDepartmentID : Integer;
SubDepartments: TList< TDepartmentStructure >;
constructor Create( const pBasedOn : TDepartment );
end;
var
Department : TObjectList<TDepartment>;
function CopyStructure( pDepartment : TList<TDepartment> ) : TDepartmentStructure; // returns root
implementation
var
DepartmentStructure : TObjectList<TDepartmentStructure>;
function CopyStructure( pDepartment : TList<TDepartment> ) : TDepartmentStructure;
var
i, j: Integer;
begin
// stage one - copy everything
for i := 0 to pDepartment.Count - 1 do
begin
DepartmentStructure.Add( TDepartmentStructure.Create( pDepartment[ i ] ));
end;
// now go through and build structure
Result := nil;
for i := 0 to DepartmentStructure.Count - 1 do
begin
if DepartmentStructure[ i ].ID = 0 then
begin
// root
Result := DepartmentStructure[ i ];
end
else
begin
for j := 0 to DepartmentStructure.Count - 1 do
begin
if DepartmentStructure[ i ].ParentDepartmentID = DepartmentStructure[ j ].ID then
begin
DepartmentStructure[ j ].SubDepartments.Add( DepartmentStructure[ i ] );
break;
end;
end;
end;
end;
end;
{ TDepartmentStructure }
constructor TDepartmentStructure.Create(const pBasedOn: TDepartment);
begin
inherited Create;
ID := pBasedOn.ID;
Name := pBasedOn.Name;
ParentDepartmentID := pBasedOn.ParentDepartmentID;
SubDepartments:= TObjectList< TDepartmentStructure >.Create( FALSE ); // we do NOT own these objects!
end;
initialization
DepartmentStructure := TObjectList<TDepartmentStructure>.Create( TRUE );
finalization
DepartmentStructure.Free;
end.
Note that this is for illustration purposes only. You would probably not create and destroy the structures where I have. Once you have the structure you can create your JSON records using your current routines no doubt.

How to compare string to integer with while do loop in pascal?

How to compare string to integer using while loop in Pascal?
Like this:
var Destination:string;
while (Destination>'11') do begin
writeln('Error');
write('Destination Number');
readln(Destination);
end;
You have to convert Destination to an integer:
program Project1;
uses sysutils;
var
converted: integer;
Destination: string;
begin
converted := 12;
Destination := '';
while (converted > 11) do
begin
writeln('Error');
writeln('Destination Number');
readln(Destination);
converted := StrToIntDef(Destination, 12);
end;
end.
convertion routines are avalaible in sysutils:
http://www.freepascal.org/docs-html/rtl/sysutils/index-5.html
Why not just do the conversion in the WHILE--DO statement?
ReadLn(Destination);
WHILE StrToInt(Destination) > 11 DO NumberIsTooHigh;
where NumberIsTooHigh simply is a procedure you write to handle your "error". Ex:
PROCEDURE NumberIsTooHigh;
BEGIN
WriteLn('Your number is above valid range');
write('Destination Number');
readln(Destination);
END;
The reason for the previous routine to make "error" on the first run is that "Destination" does not yet have a value at all. The converted-variable is then set manually to 12, just outside the Ok-range, hence it will always produce the error on startup.

Json Array into ListBox/Memo in Delphi xe7

I'm trying to catch the following JSON array :
[{"name":"Bryan","email":"Bryan#hotmail.com"},
{"name":"Louis","email":"Louis#hotmail.com"},
{"name":"Maria","email":"Maria#hotmail.com"},
{"name":"Test","email":"test#hotmail.com"},
{"name":"Anthony","email":"anthony#hotmail.com"}]
and put it in Memo or ListBox in Delphi :
the code is the following :
procedure TForm1.Button1Click(Sender: TObject);
var jv: TJSONValue;
jo: TJSONObject;
jp: TJSONPair;
ja: TJSONArray;
i: integer;
j: integer;
begin
RESTRequest1.Execute;
jv:=RESTResponse1.JSONValue;
jo:= TJSONObject.ParseJSONValue(jv.ToString) as TJSONObject;
try
for i := 0 to jo.Size - 1 do
begin
jp := jo.Get(i);
if jp.JsonValue is TJSONArray then
begin
ja := jp.JsonValue as TJSONArray;
for j := 0 to ja.Size -1 do
Memo1.Lines.Add(ja.Get(i).ClassName + ': ' + ja.Get(j).ToString);
end
else
Memo1.Lines.Add(jp.ClassName + ': '+ jp.ToString);
end;
finally
jo.Free;
end;
end;
When I click in Button I got the following error message :
Invalid class typecast
during debugging the following line has a problem :
jo:= TJSONObject.ParseJSONValue(jv.ToString) as TJSONObject;
I don't know how to resolve this problem or this mistake ,
Could you please help me ?
Thanks.
This could perfectly well be worked out by reading the code and looking at the JSON. However, I'd like to show you how to go about debugging such a problem in case you cannot work it out by static analysis. When an as cast fails that is always because the object on the left hand side of the as does not derive from the type on the right hand side. The next step then is always to inquire as to what the type of the object on the left hand side is. I've included a short MCVE above as a means to demonstrate.
The output of this program:
{$APPTYPE CONSOLE}
uses
System.JSON;
const
JSON = '[{"name":"Bryan","email":"Bryan#hotmail.com"},' +
' {"name":"Louis","email":"Louis#hotmail.com"},' +
' {"name":"Maria","email":"Maria#hotmail.com"},' +
' {"name":"Test","email":"test#hotmail.com"},' +
' {"name":"Anthony","email":"anthony#hotmail.com"}]';
begin
Writeln(TJSONObject.ParseJSONValue(JSON).ClassName);
end.
is
TJSONArray
Now, TJSONArray does not derive from TJSONObject. Hence your as cast raise a runtime error. If you cast the value returned by ParseJSONValue to TJSONArray that will succeed.
This is to be expected since the root of your JSON is an array and is not an object.
You need to modify your code so that it does not assume that the root level is always an object. You need different behaviour for arrays and objects.
I'm not sure what the problem of TJSONObject is with the string you posted.
For some reason it will parse it if you changed it.
{"Persons":[{"name":"Bryan","email":"Bryan#hotmail.com"},{"name":"Louis","email":"Louis#hotmail.com"},{"name":"Maria","email":"Maria#hotmail.com"},{"name":"Test","email":"test#hotmail.com"},{"name":"Anthony","email":"anthony#hotmail.com"}]}
If I run the code as it is I get the following result
If you don't mind using something different than default Delphi units I would suggest superobject (Link here)
superobject will parse your JSON edited and as posted.
Your code would look like this:
Const
MyJSON = '[{"name":"Bryan","email":"Bryan#hotmail.com"},{"name":"Louis","email":"Louis#hotmail.com"},{"name":"Maria","email":"Maria#hotmail.com"},{"name":"Test","email":"test#hotmail.com"},{"name":"Anthony","email":"anthony#hotmail.com"}]';
procedure ParseJSON;
var
obj: ISuperObject;
Ar: TSuperArray;
I: integer;
begin
obj := SO(MyJSON);
if obj.IsType(stArray) then
begin
Ar := obj.AsArray;
try
for I := 0 to Ar.Length-1 do
L.Add(Ar.O[I].AsString);
finally
Ar.Free;
end;
end
else
L.Add(Obj.AsString);
end;
Result:
For Koul, to get the element names and values.
Like I said not very pretty code but ok.
Ar.O[0].AsObject.GetNames.AsArray.S[0]
To cut it up in pieces a bit.
Ar.O[0] //Get the first element in the array as ISuperObject
.AsObject //Get it as TSuperTableString
.GetNames //Gets all names in the array, in this case "name" and "email"
.AsArray[0]//Get the first name in the names array.
It will result in email (Names are sorted A-Z)
You can do the same for the values by calling GetValues instead of GetNames.
I think the prettiest way to get it will be defining 2x more TSuperArray
procedure PrintNamesAndValues;
Var
Ar, ArNames, ArValues:TSuperArray;
I: Integer;
begin
Ar := SO(<JSON string>).asArray;
ArNames := Ar.O[0].AsObject.GetNames.AsArray;
ArValues := Ar.O[0].AsObject.GetValues.AsArray;
For I := 0 to ArNames.Length-1 do
WriteLn(Format('%s: %s',[ArNames.S[I], ArValues.S[I]]));
end;
Hope it's all clear enough :)

Encoding a message with Vigenere cipher in Delphi?

I want to encrypt a message with a simple Vigenere cipher by assigning each letter in the alphabet a numeric value e.g. A= 1; B= 2;..Z= 26. The problem is I don't know which function to use to recognize a character in a string(as it is a message that has to be encoded, complete with spaces), and then giving it a specific, numeric value.
Next up, that numeric message has to be converted into binary, which is easy, but how do I convert that message that was a string into an integer(other that the StrToInt function)?
I just need to know which function to use for the Vigenere Cipher.
*I am still in High school so I apologize in advance for using the wrong terms.
You can use a case statement to perform the encoding.
function EncodedChar(C: Char): Byte;
begin
case C of
'A'..'Z':
Result := 1 + ord(C) - ord('A');
' ':
Result := ???;
',':
Result := ???;
... // more cases here
else
raise ECannotEncodeThisCharacter.Create(...);
end;
end;
Encode a string with a for loop:
function EncodedString(const S: string): TBytes;
var
i: Integer;
begin
SetLength(Result, Length(S));
for i := 1 to Length(S) do begin
// dynamic arrays are 0-based, strings are 1-based, go figure!
Result[i-1] := EncodedChar(S[i]);
end;
end;

How do I obtain a reference to the object or its data from my external viewer debugger visualizer?

I am trying to write a debugger visualizer for a TJSONObject or a TJSONValue. I have most of the visualizer working nicely. The problem I am having is getting a reference to the TJSONObject, or at least to the tostring() value of the TJSONObject.
According to the samples I've seen, as well as the nice post by Jeremy North at http://edn.embarcadero.com/article/40268, I should get what I need from the Show method of my IOTADebuggerVisualizerExternalViewer implementation. Specifically, from the Expression, TypeName, and EvalResult string parameters.
From what I understand, Expression is the name of variable being inspected (visualized), TypeName is the classname of the variable, and EvalResult is the default string representation of the variable.
For a simple test I placed a TMemo on my TFrame descendant. From the IOTADebuggerVisualizerExternalViewer.Show method I call the ShowJSONObject method of my TFrame, to which I pass Expression, TypeName, and EvalResult. The relevant code appears here:
function TDebuggerJSONVisualizer.Show(const Expression, TypeName, EvalResult: string;
SuggestedLeft, SuggestedTop: Integer):
IOTADebuggerVisualizerExternalViewerUpdater;
var
AForm: TCustomForm;
AFrame: TJSONViewerFrame;
VisDockForm: INTACustomDockableForm;
begin
VisDockForm := TJSONVisualizerForm.Create(Expression) as INTACustomDockableForm;
AForm := (BorlandIDEServices as INTAServices).CreateDockableForm(VisDockForm);
AForm.Left := SuggestedLeft;
AForm.Top := SuggestedTop;
(VisDockForm as IFrameFormHelper).SetForm(AForm);
AFrame := (VisDockForm as IFrameFormHelper).GetFrame as TJSONViewerFrame;
AFrame.ShowJSONObject(Expression, TypeName, EvalResult);
Result := AFrame as IOTADebuggerVisualizerExternalViewerUpdater;
end;
{ TStringListViewerFrame }
procedure TJSONViewerFrame.ShowJSONObject(const Expression, TypeName,
EvalResult: string);
begin
Memo1.Lines.Add(Expression);
Memo1.Lines.Add(TypeName);
Memo1.Lines.Add(EvalResult);
end;
As you can see, I at this point I am only trying to display the values of these three parameters from my ShowJSONObject method.
Here is a simple TJSONObject that I tried to display using the visualizer:
var
jo: TJSONObject;
begin
jo := TJSONObject.Create;
jo.AddPair('one', 'one');
jo.AddPair('two', TJSONNumber.Create(1)); //a breakpoint here
The result looks like this:
I was hoping that EvalResult would return the tostring representation of the TJSONObject, but it only returned the uninformative (), which is the same thing you see by default in the local variables window.
How do I get either the tostring representation of the TJSONObject for which the visualizer was invoked or a handle to the actual object, so I can deconstruct and display its value?
You need to evaluate your expression (including ToString call) using this procedure (just copied from my own visualizer source so it could use some local variables that are not declared here):
function TJSONViewerFrame.Evaluate(Expression: string): string;
var
CurProcess: IOTAProcess;
CurThread: IOTAThread;
ResultStr: array[0..4095] of Char;
CanModify: Boolean;
ResultAddr, ResultSize, ResultVal: LongWord;
EvalRes: TOTAEvaluateResult;
DebugSvcs: IOTADebuggerServices;
begin
begin
Result := '';
if Supports(BorlandIDEServices, IOTADebuggerServices, DebugSvcs) then
CurProcess := DebugSvcs.CurrentProcess;
if CurProcess <> nil then
begin
CurThread := CurProcess.CurrentThread;
if CurThread <> nil then
begin
EvalRes := CurThread.Evaluate(Expression, #ResultStr, Length(ResultStr),
CanModify, eseAll, '', ResultAddr, ResultSize, ResultVal, '', 0);
case EvalRes of
erOK: Result := ResultStr;
erDeferred:
begin
FCompleted := False;
FDeferredResult := '';
FDeferredError := False;
FNotifierIndex := CurThread.AddNotifier(Self);
while not FCompleted do
DebugSvcs.ProcessDebugEvents;
CurThread.RemoveNotifier(FNotifierIndex);
FNotifierIndex := -1;
if not FDeferredError then
begin
if FDeferredResult <> '' then
Result := FDeferredResult
else
Result := ResultStr;
end;
end;
erBusy:
begin
DebugSvcs.ProcessDebugEvents;
Result := Evaluate(Expression);
end;
end;
end;
end;
end;
end;
So now you can replace your Show function with something like this:
AFrame.ShowJSONObject(Expression, TypeName, Evaluate(Expression + '.ToString'));