small problem In Pascal , Could you please help? - pascal

I write this program with pascal
which ask the user to enter two arrays and constant value which is K
the program muli the K with arrays .
and then save the answer in new array
and do some operation in new array
addition << work well
Subtraction << also work
BUT the problem in Multi << I am trying to ask the user to enter a new array and do Muti but still there is a problem.
ALSO
I want these operation repeated until the user press exit <<< I could not do this options because i am not perfect with pascal .
I would be grateful if you could help me
This is My Code
program BST6;
const maxN=100;maxM=100;
type mat=array[1..maxN,1..maxM]of integer;
var A,B,c:mat;
n,m,l,s,i,j,k:integer;
ch : char;
procedure readMat(var A:mat;var m,n:integer);
begin
for i:=1 to m do
for j:=1 to n do
begin
write('mat[',i,',',j,']=');
readln(A[i,j]);
end;
end;
procedure writeMat(A:mat;m,n:integer);
begin
for i:=1 to m do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end;
function multK(A:mat;k:integer):mat;
begin
for i:=1 to n do
for j:=1 to m do
begin
B[i,j]:= K*A[i,j];
end;
multK:=B;
end;
function minus(A,B:mat):mat;
begin
for i:=1 to m do
for j:=1 to n do
C[i,j]:=A[i,j]-B[i,j];
minus:=C;
end;
function plus(A,B:mat):mat;
begin
for i:=1 to m do
for j:=1 to n do
C[i,j]:=A[i,j]+B[i,j];
plus:=C;
end;
function mult(A,B:mat;m,l,n:integer):mat;
begin
for i:=1 to m do
for j:=1 to n do
for k:=1 to l do
c[i,j]:=c[i,j]+A[i,k]*B[k,j];
mult:=C;
end;
begin
write('input m<=',maxM,'.. m=' );readln(m);
write('input n<=',maxN,'.. n=');readln(n);
readMat(A,m,n);
writeln('input the const K');readln(k);
B:=multK(A,K);
writeln('The matrix A : ');
writeMat(A,m,n);
writeln('The matrix B=K*A : ');
writeMat(B,m,n);
writeln('choose the operation + , - or * ');
readln(ch);
case ch of
'+' : c:=plus(A,B);
'-' : c:=minus(A,B);
'*' : begin
writeln('input m<=',maxM,'input l<=',maxN);readln(m,l);readMat(A,m,l);
writeln('input l<=',maxN);readln(n);readMat(B,l,n);
c:=mult(A,B,m,l,n);
end;
end;
writeMat(c,m,n);
readln;
end.

First of all having global one letter variables which collide with function parameters with the same name is insane.
Why does multK modify the global variable B as a sideeffect?
Why does minus modify the global variable C as a sideeffect?
Why global integers as for index variables?
And mult is even worse: It doesn't only modify C as a sideeffect, but it assumes C contains meaningful values beforehand. I think it needs to initialize C to all zeros beforehand.
My guess is some of your side effects interfere in strange ways. But I don't want to think it through. Refactor your code first. In particular learn how and when to use local variables.

Related

Pascal error: left side cannot be assigned when trying to compile

I got this error when trying to compile.
left side cannot be assign to, ./number 21,22 in pastebin
here is my code
Program urut;
Uses Wincrt;
Const N = 5;
data: Array [1..N] Of Integer = (2,4,5,3,1);
Var
j,k,temp : Integer;
Begin
Clrscr;
Writeln ('Data sebelum diurutkan');
For j:=1 To N Do
Begin
Writeln('data[' ,j, ']= ',data [j]);
End;
For j:=1 To N-1 Do
Begin
For k :=N Downto j+1 Do
Begin
If data[k] < data[k-1] Then
Begin
temp := data[k];
data[k] := data[k-1]; //left side cannot be assigned to
data[k-1] := temp; //left side cannot be assigned to
End;
End;
End;
Writeln;
Writeln ('Data setelah diurutkan ');
For j:=1 To N Do
Begin
Writeln ('data[' ,j, '] = ',data[j]);
End;
Writeln;
End.
sorry for uncorrectly pattern post
, thank you so much.
Like tom Tom Brunberg says, my array is const, it can't be changed. Therefore I need to remove that const.
So it should be
data: Array [1..5] Of Integer = (value);
without const, and put it under var with another variable

Pascal array definition error

I am a 1st grade student and I got a task to complete, but cannot understand how to avoid errors that I got. Could you please help me?
Here is my code:
PROGRAM LAB5MAS (INPUT,OUTPUT);
CONST
n=5;
m=6;
VAR
A: array[1..n,1..m] of Integer;
R: array[m-1] of Integer;
i,j: Integer;
max: Integer;
BEGIN
Randomize;
for i:=1 to n do
for j:=1 to m do
a[i,j]:=random(10);
Writeln('a=,');
for i:=1 to n do
BEGIN
for j:=1 to m do
write(a[i,j]:4);
writeln;
END;
for i:=1 to n do
for j:=1 to m-1 do
R[i,j]:=abs(A[i,j]-A[i,j+1]);
END.
for i:=1 to n do
BEGIN
max:=R[i,1];
for j:=1 to m-1 do
if R[i,j] > max then
max:=R[i,j];
Writeln(max);
END;
for i:=1 to n do
BEGIN
for j:=1 to m do
write(a[i,j]:4);
writeln;
END.
And the errors that I got:
Compiling C:\Users\Nadia\Desktop\qqwww\laba.pas
laba.pas(7,17) Error: Error in type definition
laba.pas(24,7) Error: Illegal qualifier
laba.pas(26,1) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted
How to fix this?
And how to compute the biggest value of subtraction between nearest elements in array A(5,6) and write this result to array B?
Thanks in advance!
R: array[m-1] of Integer;
This is wrong, you need to specify a range of values, e.g. "R: array[0..m-1] of Integer;"
R[i,j]:=abs(A[i,j]-A[i,j+1]);
You are trying to treat a one dimesntional array as a multidimensional array.
You also seem to have screwed up your "END"s, I think what you have in that regard is syntatically correct but almost certainly not what you intended (iirc everything after the first "END." is ignored)

Pascal, reading unknown number of integers

My question is how can I read some number of integers that user enters on standard input, and place them in array.However I don't how many numbers user will enter and i can't ask him that? User enters numbers in one line.
Okay i have just one more answer i would like to add.Thanks all for your help this is code written based on suggestions.I added a line for writting array backwards just for you can see that it has readed well.
program backo;
var niz:array [1..100] of integer;
n, i:integer;
begin
i:=1;
writeln('enter elements of array');
read(niz[i]);
while not eoln do
begin
i:=i+1;
read(niz[i]);
end;
for n:=i downto 1 do
writeln(niz[n]);
end.
Ok, based on comments there is three ways demonstrated:
program readmultiint;
{$mode objfpc}{$H+}
uses
StrUtils;
const
CMaxValues = 3;
var
s: string;
darr: array of Integer;
sarr: array [0..CMaxValues-1] of Integer;
i, cnt: Integer;
begin
// Dynamic array using WordCount
Writeln('Enter values:');
Readln(s);
cnt := WordCount(s, StdWordDelims);
SetLength(darr, cnt); // Allocate room for values
for i := 0 to cnt - 1 do
Val(ExtractWord(i + 1, s, StdWordDelims), darr[i]);
for i in darr do
Writeln(i);
// Dynamic array usin EOLN
SetLength(darr, 0);
Writeln('Enter values:');
while not eoln do
begin
SetLength(darr, Length(darr) + 1); // Expand array for next value
Read(darr[High(darr)]);
end;
Readln; // Read <Enter> itself
for i in darr do
Writeln(i);
// Static array
cnt := 0;
Writeln('Enter values:');
while (not eoln) and (cnt < CMaxValues) do // Reads not more then CMaxValues values
begin
Read(sarr[cnt]);
Inc(cnt);
end;
Readln; // Read <Enter> itself
for i := 0 to cnt-1 do
Writeln(sarr[i]);
end.
Feel free to use one of them or provide your own :)
PS: Some readings:
Dynamic arrays
Val procedure
for-in loop

oracle - PLS-00103 & ORA -06550 on procedures

I'm getting the above error while trying to run a simple PL/SQL program that uses procedures. I dont know what went wrong, please help out.
declare
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
end;
begin
x integer:=121;
y integer;
palindrome(x,y);
dbms_output.put_line(y);
end;
/
ERROR at line 2:
ORA-06550: line 2, column 2: PLS-00103: Encountered the symbol
"CREATE" when expecting one of the following: begin function pragma
procedure subtype type current cursor delete exists prior
Modifications to your code.
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
dbms_output.put_line(y);
end;
/
Execute that procedure
declare
y number;
begin
palindrome(133,y);
end;
You only need DECLARE when you're creating an anonymous PL/SQL block that contains variable declarations. When you're creating a named procedure/function/package, then your CREATE OR REPLACE ... statement takes the place of the DECLARE. Anything between that statement and the corresponding BEGIN is known as the declaration section.
It's not especially clear if you're trying to create a named procedure that you can then call with another PL/SQL anonymous block, in which case you'd do:
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
end;
/
declare
x integer := 121;
y integer;
begin
palindrome(x,y);
dbms_output.put_line(y);
end;
/
or if you're trying to declare a procedure inside an anonymous PL/SQL, in which case you'd do:
declare
x integer := 121;
y integer;
procedure palindrome (x in number, y out number) is
i integer;
j integer;
k integer := 0;
begin
i := x;
while i > 0
loop
j := mod(i,10);
k := k*10 + j;
i := i/10;
end loop;
y := k;
end palindrome;
begin
palindrome(x,y);
dbms_output.put_line(y);
end;
/

Why is function in this program repeating? Pascal

Made a program for finding greatest common divisor, but for some reason the function is repeating.
var p,q:integer;
function nod(x,y: integer):integer;
begin
while y<>0 do nod(y,x mod y);
result:=x;
end;
begin
write('Enter two integers: ');
read(p,q);
write(nod(p,q));
end.
You are not updating the y variable, and the loop never ends.
Maybe:
var p,q:integer;
function nod(x,y: integer):integer;
begin
while y<>0 do
begin
y:= nod(y,x mod y);
end;
result:=x;
end;
begin
write('Enter two integers: ');
read(p,q);
write(nod(p,q));
end.

Resources