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.
Related
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!
This question already has an answer here:
Lazarus readln doesn't read the variable
(1 answer)
Closed 4 years ago.
I have a small code where I want to initialize an array of records by fields and then just output this records on screen.
Data types:
type
grade = 1..5;
Person = record
Name: string[16];
isMale: boolean;
grades: array [1..6] of grade;
end;
var
Table: array [1..10] of Person;
R: Person;
N,J,I: Integer;
Part of code with initialization and output:
readln(n);
if N>10 then N:=10; if N<1 then N:=1;
for I:=1 to N do begin
R:=Table[I];
//write('Gender?'); readln(j); R.isMale:=j>=0; <= This works just fine
write('Name? '); readln(R.Name);
write('Gender? '); readln(j); R.isMale:=j>=0;
write('Grades? '); for j:=1 to 6 do read(R.grades[J]); writeln;
end;
for I:=1 to N do begin
R:=Table[I];
write(I,' ', R.Name,' ',R.isMale);
end;
When I enter info about first person it works fine, but then every other person's name input is skipped (output is "Name? Gender? ). If I switch entering boolean and string, code works correct, but that's not a logic order.
Why is this happening?
At the end of the loop, you should assign the record to the array. Note that, unlike with classes, assigning a record copies the data in the record, it does not reference the record. So instead of what you have, rather do:
for I := 1 to N do
begin
//write('Gender?'); readln(j); R.isMale:=j>=0; <= This works just fine
write('Name? ');
readln(R.Name);
write('Gender? ');
readln(j);
R.isMale := j >= 0;
write('Grades? ');
for j := 1 to 5 do
read(R.grades[J]);
readln(R.grades[6]); // readln reads the end-of-line too.
writeln;
Table[I] := R; // copy the data from R into the table
end;
That way, the data from the record R is copied into the table. There is no need to copy R from the table at the beginning of the loop, as the table is empty anyway.
Unlike with classes, with records like this, you could do the following too:
write('Name? ');
readln(Table[I].Name);
write('Gender? ');
readln(j);
Table[I].isMale := j >= 0;
// etc...
And in the final loop:
Writeln(I, ' ', Table[I].Name, ' ', Table[I].IsMale);
without using R at all.
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.
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).
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.