Saving text file from code - file

I have made this code:
Program Pzim ;
var
i:integer;
vect:array[1..1001] of integer;
Begin
i:=1;
for i:= 1 to 999 do
vect[i]:=i+1;
for i:= 1 to 999 do
writeln (vect[i]);
readln;
End.
The program print a number sequence.
I want to save in a text file what is printed.
it could be made using the Pascal yet or even using another source? Notepad++ maybe?

Of course you can write to a text file in Pascal.
Program Pascal ;
var
i:integer;
vect:array[1..1001] of integer;
Myfile: text;
begin
i:=1;
for i:= 1 to 999 do
vect[i]:=i+1;
Assign(Myfile, 'Myfile.txt');
Rewrite(MyFile);
for i:= 1 to 999 do
begin
WriteLn (vect[i]);
WriteLn(Myfile, vect[i]);
end;
Close(Myfile);
ReadLn;
end.

It may depend on which version of Pascal you are using, but in many versions this will work.
In the var section, add
f : textfile; // f can be any variable name
After the vect[i]:=i+1 line, insert
assign( f, 'c:\path\filename.txt'; // where path and filename are what you want.
rewrite( f);
then change the write statement to writeln( f, ...) where f is the name used above. And before the end statement, insert closefile( f); (or close(f) in some versions).

Related

Comparison of two arrays in Lazarus

I have a problem with Pascal, especially Lazarus.
First of all, I created two random arrays of integer:
procedure TForm1.b_arraycreate1Click(Sender: TObject);
begin
randomize;
for i := 1 to 5 do
arr1[i] := random(10);
end;
And
procedure TForm1.b_arraycreate2Click(Sender: TObject);
begin
randomize;
for j := 1 to 5 do
arr2[j] := random(10);
end;
I know, I could put it in one procedure as well but doesn't matter now.
I want to compare these two. I wrote the following code:
procedure TForm1.b_comparisonClick(Sender: TObject);
var v:boolean;
begin
for i := 1 to 5 do begin
for j := 1 to 5 do begin
if arr1[i] = arr2[j]
then
begin
v:=true;
end
else
begin
v:=false;
end;
end;
end;
if v = true
then
begin
ShowMessage('Yes, there is a similarity! You can find number ' +IntToStr(arr1[i])+ ' in array 1, position ' +IntToStr(i)+ ' and also in array 2, position ' +IntToStr(j)+ '.');
end
else
begin
ShowMessage('No similarities... Generate new ones!');
end
end;
In my own words: I want to push a button and then there should be a message window with the information if there is one number (for example 7) which exists in array 1 and array 2. If yes, it should also write the position (index) of this number.
Unfortunately, this program doesn't work and I don't know why. It always shows "No similarities" (and don't worry about the creation of the arrays. I also have a label where I can test the content of the arrays every time).
Is there a (silly) mistake in my code here?
As explained already by MartynA in his comment, your algorithm is wrong. Your words are:
if there is one number which exists in array 1 and array 2
To see if it is so, you must scan all array1 and, for each number, see if it exists somewhere in array2.
So yes, you need two cycles, one nested in the other. As soon as you find a correspondence, you must stop. Or, if you want more results (find multiple duplicates), show a message instead of stopping - and go ahead. Third possibility (more complicated): when found, store the couple of indexes (without overwrite old results...) and go ahead. I will only show the first option:
procedure TForm1.b_comparisonClick(Sender: TObject);
var
i,j: integer;
v: boolean;
begin
v := false;
for i := 1 to 5 do begin
for j := 1 to 5 do begin
if arr1[i] = arr2[j] then begin
v := true;
break
end
end // inner, j
end; // outer, i
if v = true
then ShowMessage(.....)
else ShowMessage('No similarities...');
end; // proc comparison
I tried to respect your code a bit, there are a few possible "shortcuts"; for example, if v is a boolean variable, it is better to write if v then instead of if v=true then, and some others, like
v := arr1[i]=arr[j];
...or... the outer loop does not need begin+end.
******* BEWARE (see comment below about break)
To stop/exit from two nested cycle is not so simple... perhaps a goto... the code above works, but the break does little work.
******* second update, as described by comment below. IT DOES NOT WORK, because if the break does not exit BOTH loops, the outer index gets modified. The correct cycle using TWO breaks is as follows:
for i := 1 to 5 do begin
for j := 1 to 5 do begin
if arr1[i] = arr2[j] then begin
v := true;
break
end
end; // inner, j
if v then break
end; // outer, i
Sorry for the mistakes... :-)
I would prefer a GOTO to exit both loops: it is faster, single instruction, and more clear ("goto found" instead of a generic break). But GOTOs are not very popular... so I've been afraid!

Warning: function result variable of a managed type does not seem to be initialized

The task I have requires me to create two routines one of which reads in data from a terminal and the other outputs data to the terminal, and another two routines which utilize an array to loop through these two routines to perform them multiple times.
The issue I am having is that the terminal crashes after one run through of the ReadComputer function instead of looping multiple times. The compiler is also providing me the following warning:
"Warning: function result variable of a managed type does not seem to be initialized"
although after extensive research and due to the fact that no one uses pascal I cannot find a solution. Any help is much appreciated! :)
I have provided a copy of my code here for reference:
program CompupterProgram;
uses TerminalUserInput;
type
Computer = Record
id: integer;
manafacturer: String;
year: integer;
warranty: integer;
end;
type Computers = Array of Computer;
function ReadComputer(): Computer;
begin
ReadComputer.id := ReadInteger('PLease Enter Computer Id:');
ReadComputer.manafacturer := ReadString('PLease Enter Computer Manafacturer:');
ReadComputer.year := ReadInteger('PLease Enter Computer Year:');
ReadComputer.warranty := ReadInteger('PLease Enter Computer Warranty:');
result := ReadComputer;
end;
procedure WriteComputer(c: Computer);
begin
WriteLn('Computer ID: ', c.id);
WriteLn('Computer Manafacturer ', c.manafacturer);
WriteLn('Computer Year ', c.year);
WriteLn('Computer Warranty ', c.warranty);
ReadLn();
end;
function ReadAllComputers(count: Integer): Computers;
var i: Integer;
begin
for i := 0 to count do
begin
ReadAllComputers[i] := ReadComputer();
end;
result := ReadAllComputers;
end;
procedure WriteAllComputers(computerArray: Computers);
var i: Integer;
begin
for i:= 0 to (length(computerArray)) do
begin
WriteComputer(computerArray[i]);
end;
end;
procedure Main();
var computers: Array of Computer;
index: Integer;
begin
computers := ReadAllComputers(3);
WriteAllComputers(computers);
end;
begin
Main();
end.
Computers is a dynamic array, and you need to set its length before use in ReadAllComputers with SetLength().
All dynamic arrays are zero based, so you need to count from zero to Length(aDynArray)-1 in a couple of places. Or use the High(aDynArray) function to express the highest possible value of it's index.
Note: The Result use in ReadComputer is superfluous. Either use the function name or the Result variable to return the function result. The latter is to prefer, since code will be more clear.
In freepascal the Result variable is defined only in ObjFPC or Delphi mode.

Delphi TFloatField.DisplayFormat for numeric fields less than 1.0

This is my procedure.
procedure format_integer_field(Atable: TDataSet);
var i: integer;
begin
if Atable.Active then
if Atable.FieldCount > 0 then
with Atable do
begin
for i:= 0 to FieldCount-1 do
if (Fields[i] is TIntegerField) then
begin
(Fields[i] as TIntegerField).DisplayFormat := '###,###';
(Fields[i] as TIntegerField).EditFormat := '#';
end
else
if (Fields[i] is TFloatField) then
begin
(Fields[i] as TFloatField).DisplayFormat := '###,###.##';
(Fields[i] as TFloatField).EditFormat := '#.##';
end;
end;
end;
This is work fine until a number like "0.9" has been entered and result will be ".9".
How can I have thousand separator and zero before floating point that smaller than "1".
Try (Fields[i] as TFloatField).DisplayFormat := '##0,000.00';
As you did read in documentation at http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Default_Formatting_for_Numeric,_Date,_and_Time_Fields it says
Default formatting is performed by the following routines:
FormatFloat -- TFloatField, TCurrencyField
And how you did read in the following documentation pages
http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.FormatFloat
http://docwiki.embarcadero.com/Libraries/XE3/en/Data.DB.TNumericField.DisplayFormat
the documentation quotes
0 -> Digit placeholder. If the value being formatted has a digit in the position where '0' appears in the format string, then
that digit is copied to the output string. Otherwise, a '0' is
stored in that position in the output string.
# -> Digit placeholder. If the value being formatted has a digit in the position where '#' appears in the format string, then
that digit is copied to the output string. Otherwise, nothing is
stored in that position in the output string.
So by using "#" in the formatting pattern you tell Delphi "i do not need any digits (and thousands separators with them) in this place, but you might put them if you want" - and since Delphi does not want to put leading zeros - you don't have any. However, if you really need those digits and the thousands separator with them, you put "0" instead of "#" and that way you tell Delphi "the digits just need to be here, whether you want to put them or not"
The format you need is ###,##0.0#

creating array of text files with FPS

Is it possible that FPS program read user input and then creates a file with a name of that input and that read another input and so on and on..?
I have tried to create array of text file but got some problems.
First of all an error occurred that (some) access was denied and secondly, how to create a result file where variable is in the middle of the text file name (I've tried assign (Df,'rezult',i,'.txt'); i - here is changing variable, but in this way program does not work.
P.S. Sorry for my English language skills...
This is what I have tried:
program testing;
var
Df : text;
i:integer;
SomeUserInput:integer;
begin
for i:=1 to 10 do
begin
Assign(Df,'rez.txt'); Rewrite(Df);
Read(SomeUserInput);
WriteLn(Df,'some words + ',SomeUserInput);
Close(Df);
end;
Readln;
end.
(fixed the problem that first comment suggest, but now only last thing is written in my file, I wish to create 10 or more files)
If I understand correctly, you want to number the filename with your loop var.
This should work:
program testing;
Uses
SysUtils;
var
Df : text;
i:integer;
SomeUserInput:integer;
begin
for i:=1 to 10 do
begin
Assign(Df,'rez'+IntToStr(i)+'.txt');
Rewrite(Df);
ReadLn(SomeUserInput);
WriteLn(Df,'some words + ',SomeUserInput);
Close(Df);
end;
Readln;
end.

Free Pascal keep numbers in array

i don't know how to do this:
I want to do a program in pascal in which the user have to insert 90 numbers introduced by console and separated by a blank and keep them in a bidimensional array (10x9). Anyone knows how to implement this?
Thanks a lot.
var the_array:array[1..10] of array[1..9] of integer;
var i:integer; var j:integer;
...
i:=1; j:=1;
while i<=10 do begin
while j<=9 do begin
read(the_array[i,j]);
inc(j);
end;
j:=1;
inc(i);
end;
You just use two indices to iterate through an array while filling it from calling read().
You wrote that you use FreePascal, so you can make use of SScanF here.
This program lets you enter some lines of numbers that are separated by spaces.
After it's done, it prints the numbers.
I would not ever hand something like this over to end users though. Why not provide a graphical user interface instead?
program Project1;
uses
SysUtils;
const
Lines = 10;
type
TNumberArray = array[0..Lines-1,0..9] of integer;
procedure GetNumbers(var nums:TNumberArray);
var Line:Integer; s:String;
begin
for Line := Low(nums) to high(nums) do
begin
Write('Enter line ',Line, ': ');
ReadLn(S);
SScanf(s,'%d %d %d %d %d %d %d %d %d %d',
[
#nums[Line,0],
#nums[Line,1],
#nums[Line,2],
#nums[Line,3],
#nums[Line,4],
#nums[Line,5],
#nums[Line,6],
#nums[Line,7],
#nums[Line,8],
#nums[Line,9]
]
);
end;
end;
procedure ShowNumbers(nums:TNumberArray);
var Line,Col:Integer;
begin
for Line := Low(nums) to high(nums) do
begin
for Col:=Low(nums[Line]) to High(nums[Line]) do
Write(nums[Line,Col], ' ');
WriteLn;
end;
end;
var
Numbers: TNumberArray;
begin
WriteLn('Enter 10 numbers');
GetNumbers(Numbers);
ShowNumbers(Numbers);
WriteLn('Done. Press a key to continue.');
ReadLn;
end.
It's cleaner to parse the line using a TStringList, so that you don't have to hard-code the number of columns, but this should work.

Resources