Illegal Expression's Pascal (New Errors) - pascal

Program PaidUp;
const
size=30;
var
payment,totm1,totm2,totm3:real;
section1,section2,section3,i,j,idnum:integer;
IDNUMARR:array[1..999] of integer;
PAYMENTARR:array[1..size] of real;
Procedure InitialiseVariables;
{This procedure initialises all variables used in the program}
Begin
idnum:=0;
payment:=0;
totm1:=0;
totm2:=0;
totm3:=0;
section1:=0;
section2:=0;
section3:=0;
i:=0;
j:=0;
End; {Initialise Variables}
Procedure DeclareandInitialiseArrays;
{This procedure declares and initialises all arrays used in the program}
Begin
For i:=1 to size do
begin
IDNUMARR[i]:=0;
PAYMENTARR[i]:=0;
end; {ends for statment}
End; {Declare and Initialise Variables}
Procedure PutDataIntoArray;
{This procedure puts the data into the arrays}
Begin
while(idnum<>0) and (payment<>0) and (payment=1350) and (payment=1620) and (payment=1800) and (payment=1650) and (payment=1980) and (payment=2200) do
begin
writeln('Invalid value, please enter another value');
readln(idnum);
readln(payment);
end;{ends while statement}
j:=j+1;
IDNUMARR[j]:=idnum;
PAYMENTARR[j]:=payment;
End; {Put Data Into Array}
Procedure DetermineStatisticsInformation;
{This procedure determines which masqueraders belong to which group, tallys the total persons in a section and totals the amount of money paid in each section for costumes}
Begin
For j:=1 to size do
begin
if(PAYMENTARR[j]=1350) and (PAYMENTARR[j]=1650) then
begin
writeln('Masquerader with memid:idnum[j] belongs to section1');
section1:= section1+1;
totm1:= totm1+PAYMENTARR[j];
end;{ends if statement}
if(PAYMENTARR[j]=1620) and (PAYMENTARR[j]=1980) then
begin
writeln('Masquerader with memid:idnum[j] belongs to section2');
section2:= section2+1;
totm2:=totm2+PAYMENTARR[j];
end;{ends if statement}
if(PAYMENTARR[j]=1800) and (PAYMENTARR[j]=2200)then
begin
writeln('Masquerader with memid:idnum[j] belongs to section3');
section3:= section3+1;
totm3:=totm3+PAYMENTARR[j];
end;{ends if statement}
End; {Determine Statistics Information}
Procedure PrintResults;
{This procedure outputs all information}
Begin
writeln('The number of masqueraders in section 1 is:', section1);
writeln('The number of masqueraders in section 2 is:', section2);
writeln('The number of masqueraders in section 3 is:', section3);
writeln('Total Amount of money paid in section 1 is:', totm1);
writeln('Total Amount of money paid in section 2 is:', totm2);
writeln('Total Amount of money paid in section 3 is:', totm3);
End. {Print Results}
The errors i got were: 77 / 11 paidup.pas Error: Illegal expression
77 / 11 paidup.pas Error: Illegal expression
77 / 11 paidup.pas Fatal: Syntax error, ; expected but identifier PRINTRESULTS found

You are still missing an end statement to match the begin statement that follows For j:=1 to size do in procedure DetermineStatisticsInformation.
Also, your while condition while(idnum<>0) and (payment<>0) and (payment=1350) and (payment=1620) and (payment=1800) and (payment=1650) and (payment=1980) and (payment=2200) do in procedure PutDataIntoArray will never be true as it is impossible for payment to simultaneously be equal to 1350 and equal to 1620 etc...

Related

What is an exit code 201 in Free Pascal?

I have tried to make a simple snake game with Free Pascal, when I started the programme, it drew the map exactly what I want but after that, I pressed the button that I have set to control the snake and it exited with exit code 201.
I don't know much about that exit code, could you explain me the problems of the programme? This is the longest program I have ever made with Pascal.
Here is the code:
uses crt;
type
ran=record
x:byte;
y:byte;
end;
var
f:ran;
s:array[1..1000] of ran;
i,j:longint;
st,l:byte;
function getkey:integer;
var
k:integer;
begin
k:=ord(readkey);
if k=0 then k:=-ord(readkey);
getkey:=k;
end;
procedure fa;
begin
randomize;
f.x:=random(98)+1;
f.y:=random(23)+1;
gotoxy(f.x,f.y);
writeln('o');
end;
procedure draw;
begin
gotoxy(1,1);
st:=1;
for i:=1 to 25 do begin
for j:=1 to 100 do write('X');
writeln
end;
gotoxy(st+1,st+1);
for i:=1 to 23 do begin
for j:=1 to 98 do write(' ');
gotoxy(st+1,i+2);
end;
end;
procedure sts;
begin
s[1].x:=19;
s[1].y:=6;
gotoxy(s[1].x,s[1].y);
writeln('#');
end;
procedure fa1;
begin
f.x:=29;
f.y:=5;
gotoxy(f.x,f.y);
writeln('o');
end;
procedure eat;
begin
if (s[1].x=f.x) and (s[1].y=f.y) then begin
l:=l+1;
fa;
end;
end;
function die:boolean;
begin
die:=false;
if (s[1].x=1) or (s[1].x=100) or (s[1].y=1) or (s[1].y=25) then
die:=true;
if l>=5 then
for i:=5 to l do
if (s[1].x=s[i].x) and (s[1].y=s[i].y) then
die:=true;
end;
procedure up;
begin
for i:=l downto 2 do begin
s[i].y:=s[i-1].y;
gotoxy(s[i].x,s[i].y);
writeln('+');
end;
gotoxy(s[l].x,s[l].y+1);
writeln(' ');
s[1].y:=s[1].y-1;
gotoxy(s[1].x,s[1].y);
writeln('#');
end;
procedure down;
begin
for i:=l downto 2 do begin
s[i].y:=s[i-1].y;
gotoxy(s[i].x,s[i].y);
writeln('+');
end;
gotoxy(s[l].x,s[l].y-1);
writeln(' ');
s[1].y:=s[1].y+1;
gotoxy(s[1].x,s[1].y);
writeln('#');
end;
procedure left;
begin
for i:=l downto 2 do begin
s[i].x:=s[i-1].x;
gotoxy(s[i].x,s[i].y);
writeln('+');
end;
gotoxy(s[l].x+1,s[l].y);
writeln(' ');
s[1].x:=s[1].x-1;
gotoxy(s[1].x,s[1].y);
writeln('#');
end;
procedure right;
begin
for i:=l downto 2 do begin
s[i].x:=s[i-1].x;
gotoxy(s[i].x,s[i].y);
writeln('+');
end;
gotoxy(s[l].x-1,s[l].y);
writeln(' ');
s[1].x:=s[1].x+1;
gotoxy(s[1].x,s[1].y);
writeln('#');
end;
procedure auto(k:integer);
begin
case k of
-72:up;
-80:down;
-75:left;
-77:right;
119:up;
115:down;
97:left;
100:right;
end;
end;
procedure ingame(t:integer);
var
d,e:boolean;
begin
repeat
auto(t);
d:=die;
if d=true then exit;
eat;
until (keypressed);
if keypressed then t:=getkey;
case t of
-72:up;
-80:down;
-75:left;
-77:right;
119:up;
115:down;
97:left;
100:right;
end;
eat;
d:=die;
if d=true then exit;
end;
procedure first;
var
k:integer;
begin
draw;
fa1;
sts;
if keypressed then k:=getkey;
ingame(k);
end;
BEGIN
clrscr;
first;
readln
END.
I googled this: 201 : range error, so you probably go out of array bounds. The only array s in indexed by variables that depend on l value (weird name, BTW). But I see a single place where you do changing (increment) this variable and don't see any l initialization. So you are using arbitrary starting value (here perhaps zero because l is global).
Note that you could discover this bug (and perhaps others) with simple debugging.
The code 201 seems to be explained for example here: Runtime Error 201 at fpc
Exactly why this happens in your code, I don't know.

I am trying to write a program for Armstrong number in PL/SQL.Can anyone tell me where i am wrong?

while N !=0
LOOP
R:=MOD(N,10);
R1:=power(R,3);
A:=A+R1;
N:=TRUNC(N/10);
END LOOP;
After this it comes IF N=A THEN
SYS.DBMS_OUTPUT.PUT_LINE(' number is armstrong ');
ELSE
SYS.DBMS_OUTPUT.PUT_LINE(' number is not an armstrong ');
Your problem is when you compare (N=A) because N is zero at the end of LOOP. Then you must compare A with the original number entered, (NOrig=A).
This procedure you can help:
create or replace procedure amstrong_number(pNumber int)
is
NOrig int:=0;
N int:=0;
R int:=0;
R1 int:=0;
A int:=0;
begin
NOrig:=pNumber;
N:=pNumber;
WHILE N!= 0
LOOP
R:=MOD(N,10);
R1:=POWER(R,3);
A:=A+R1;
N:=TRUNC(N/10);
END LOOP;
IF NOrig = A THEN
dbms_output.put_line(' number is amstrong ');
ELSE
dbms_output.put_line(' number is not an amstrong ');
END IF;
end;
Regards
In a comment you say you want to take user input from the keyboard. It is not clear how you plan to do that; in general, PL/SQL does not have facilities for interaction with end users, it is a language for processing on the server.
One way to have the user provide a number is to have a procedure with an in parameter. Below is one example of how you can do it. You must compile the procedure first (for example, I saved it in a script, "Armstrong.sql", and then I ran the command "start Armstrong" in SQL*Plus). Then the user can use the procedure, which I called is_Armstrong, from SQL*Plus or a graphical interface like SQL Developer or Toad, like this: exec is_Armstrong(...) where in parentheses is the user's input number.
I built in two application exception checks - for negative or non-integer numbers and for numbers that are too long (more than 20 digits). I wrote it for Armstrong numbers of arbitrary length (<= 20), limiting it to three digits doesn't save almost any work so why not make it general. There are other possible application exceptions - for example, what if the user inputs a string instead of a number? I didn't handle all the errors - the very last example below shows an unhandled one. Write your own error handling code for whatever else you want to handle explicitly.
Content of Armstrong.sql:
create or replace procedure is_Armstrong(p_input_number number)
as
l_string varchar2(20);
l_sum number := 0;
l_loop_counter number;
l_len number;
not_a_positive_integer exception;
number_too_large exception;
begin
l_string := to_char(p_input_number);
l_len := length(l_string);
if p_input_number <= 0 or p_input_number != floor(p_input_number) then
raise not_a_positive_integer;
elsif l_len > 20 then
raise number_too_large;
end if;
for l_loop_counter in 1..l_len
loop
l_sum := l_sum + power(to_number(substr(l_string, l_loop_counter, 1)), l_len);
end loop;
if p_input_number = l_sum then
dbms_output.put_line('Number ' || l_string || ' is an Armstrong number.');
else
dbms_output.put_line('Number ' || l_string || ' is not an Armstrong number.');
end if;
exception
when not_a_positive_integer then
dbms_output.put_line('Input number must be a positive integer.');
when number_too_large then
dbms_output.put_line('Input number must be no more than twenty digits.');
end;
/
Compiling the procedure:
SQL> start Armstrong.sql
Procedure created.
Elapsed: 00:00:00.03
Usage examples:
SQL> exec is_Armstrong(-33);
Input number must be a positive integer
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.08
SQL> exec is_Armstrong(1.5);
Input number must be a positive integer
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.08
SQL> exec is_Armstrong(1234512345123451234512345);
Input number must be no more than twenty digits
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
SQL> exec is_Armstrong(3);
Number 3 is an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong(100);
Number 100 is not an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong(371);
Number 371 is an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong('abc')
BEGIN is_Armstrong('abc'); END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 1
Elapsed: 00:00:00.00

logging line number in oracle

I'm currently working on logging errors in a procedure. The goal of this procedure is to be called upon in exception handlers in the other packages in the DB and log the errors that each program encounters. below is my code.
CREATE OR REPLACE PROCEDURE APMS.test_procedure AS
procedure write_error_log (errcode number, errstr varchar2) is
pragma autonomous_transaction;
-- this procedure stays in its own new private transaction
begin
INSERT INTO error_log
(ora_err_tmsp,
ora_err_number,
ora_err_msg,
ora_err_line_no)
values (CURRENT_TIMESTAMP,
errcode,
errstr,
'line number');
COMMIT; -- this commit does not interfere with the caller's transaction.
end write_error_log;
BEGIN
INSERT INTO mockdata
VALUES ('data1', 'mockname', 'mockcity');
exception when others then
write_error_log(sqlcode,sqlerrm);
raise;
END test_procedure;
/
I'm currently just inducing an error in my mock_data table to log the error in the error_log table and see if its functional I just cant figure out how to log the line number column. I'm a complete beginner so any help would appreciated. Addiotionally, If anybody knows how I would be able to use this procedure in other packages/procedures to log the errors in other packages that would be awesome as well. I'm here to learn so any feedback is appreciated, I can further expand on this post if i'm not being clear.
Try using DBMS_UTILITY.FORMAT_ERROR_BACKTRACE. You can look here for more information.
Something like this should make works your code:
CREATE OR REPLACE PROCEDURE APMS.test_procedure AS
procedure write_error_log (errcode number, errstr varchar2,errline varchar2) is
pragma autonomous_transaction;
-- this procedure stays in its own new private transaction
begin
INSERT INTO error_log
(ora_err_tmsp,
ora_err_number,
ora_err_msg,
ora_err_line_no)
values (CURRENT_TIMESTAMP,
errcode,
errstr,
errline);
COMMIT; -- this commit does not interfere with the caller's transaction.
end write_error_log;
BEGIN
INSERT INTO mockdata
VALUES ('data1', 'mockname', 'mockcity');
exception when others then
write_error_log(sqlcode,sqlerrm,DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
raise;
END test_procedure;
Use the following to get the call stack:
dbms_utility.format_error_stack
dbms_utility.format_error_backtrace
For example,
SQL> declare
2 v1 integer := 1;
3 v2 integer := 0;
4 v3 integer;
5 procedure p1 (v1 in integer, v2 in integer, v3 out integer) is
6 begin
7 v3 := v1 / v2;
8 end;
9 procedure p2 (v1 in integer, v2 in integer, v3 out integer) is
10 begin
11 p1 (v1, v2, v3);
12 end;
13 begin
14 p2 (v1, v2, v3);
15 exception
16 when others then
17 dbms_output.put_line ('---------------------');
18 dbms_output.put_line ('This is what you record in log table:');
19 dbms_output.put (dbms_utility.format_error_stack);
20 dbms_output.put (dbms_utility.format_error_backtrace);
21 dbms_output.put_line ('---------------------');
22 raise;
23 end;
24 /
---------------------
This is what you record in log table:
ORA-01476: divisor is equal to zero
ORA-06512: at line 7
ORA-06512: at line 11
ORA-06512: at line 14
---------------------
declare
*
ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 22

The maximum in each row

I have a question for you. I need to write the maximum element in each line. For example, my table :
1 2 3 4
5 6 7 8
9 10 11 12
I want to get 4,8,12
I tried but no result:
Program Lab2;
type A=array[1..5,1..5] of integer;
var x:A;
i,j,s,max:integer;
Begin
writeln('Write date:');
for i:=1 to 5 do
for j:=1 to 5 do
read(x[i,j]);
for i:=1 to 5 do
for j:=1 to 5 do
begin
max:=x[i,1];
if (max<x[i,j]) then max:=x[i,j];
writeln(max);
end;
readln;
Please help me
end.
There are just three little mistakes:
1) if (max<x[i,j]) should be outside the second for loop, because you want initialize the max value only one time per row.
2) writeln(max); should be outside the second for loop, you want to print the value only one time per row.
3) read(x[i,j]); I reccomend to be readln (x[i,j]) because with read you only read one character, with readln you red characters till you find a new line character, and that will allow you to enter numbers with more than two digits.
This only make sense for strings, you can use read or readln with integers
Also I advice you to write the key word begin in the same line where you write a contol structure (for,while,if,etc), because in this way it's more similar to the C coding style convention, one of the most populars coding styles I guess. And also is better for you if you try to keep a similar coding style for any language.
so the code will be:
Program Lab2;
const SIZE=3;
type A=array [1..SIZE,1..SIZE] of integer;
var x:A;
i,j,max:integer;
Begin
writeln('Write date:');
for i:=1 to SIZE do begin
for j:=1 to SIZE do begin
readln(x[i,j]);
end;
end;
for i:=1 to SIZE do begin
max:=x[i,1];
for j:=1 to SIZE do begin
if (max<x[i,j]) then begin
max:=x[i,j];
end;
end;
writeln('the max value of the row ',i ,' is ',max);
end;
readln;
readln;
end.

How does this program to count vowels work?

I want to understand this code, especially PROCEDURE
PROGRAM vowels;
USES crt;
{Program that counts the number of vowels in a sentence}
CONST space=' ';
maxchar=80;
TYPE vowel=(a,e,i,o,u);
VAR buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
PROCEDURE initialize;
VAR ch:vowel;
BEGIN
FOR ch:=a TO u DO
BEGIN
vowelcount[ch]:=0;
END;
END;
PROCEDURE textinput;
VAR index:integer;
BEGIN
writeln('Input a sentence');
FOR index:=1 TO maxchar DO
IF eoln THEN buffer[index]:=space
ELSE read(buffer[index]);
readln;
END;
PROCEDURE analysis;
VAR index:integer;
ch:vowel;
BEGIN
index:=1;
WHILE index<>maxchar+1 DO
BEGIN
IF buffer[index] IN ['a','e','i','o','u'] THEN
BEGIN
CASE buffer[index] OF
'a':ch:=a;
'e':ch:=e;
'i':ch:=i;
'o':ch:=o;
'u':ch:=u;
END;
vowelcount[ch]:=vowelcount[ch]+1;
END;
index:=index+1;
END;
END;
PROCEDURE vowelout;
VAR ch:vowel;
BEGIN
clrscr;
writeln;
writeln(' a e i o u');
FOR ch:=a TO u DO
write(vowelcount[ch]:4);
writeln;
END;
BEGIN
initialize;
textinput;
analysis;
vowelout;
END;
Overall: Okay this code is counting the number of vowels supplied in the input string.
Lets Begin....
TYPE vowel=(a,e,i,o,u); VAR
buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
This code is defining a list of the vowels in english (a,e,i,o,u).
PROCEDURE initialize; VAR ch:vowel;
BEGIN FOR ch:=a TO u DO BEGIN
vowelcount[ch]:=0; END; END;
It then defines a variable to collect the number of each vowel, called vowelcount. That variable is an array, looks sort of like this:
vowelcount[a]=0;
vowelcount[e]=0;
vowelcount[i]=0; #... etc
Then the procedure "Analysis" is defined. This takes the input from the screen (which will be called later on in the program) and steps through each letter in the input.
WHILE index<>maxchar+1 DO BEGIN IF
buffer[index] IN ['a','e','i','o','u']
THEN BEGIN CASE buffer[index] OF
'a':ch:=a; 'e':ch:=e; 'i':ch:=i;
'o':ch:=o; 'u':ch:=u; END;
If any of those letters happens to be in the list of letters than matches a vowel, then it will add one to the number in the vowelcount array above. (vowelcount[ch]:=vowelcount[ch]+1) where ch is the matched letter. As you can see this is only triggered if it is a valid vowel (IF buffer[index] IN ['a','e','i','o','u'] )
Finally. The main code of the program, or what is actually run:
BEGIN clrscr; writeln; writeln(' a e i
o u'); FOR ch:=a TO u DO
write(vowelcount[ch]:4); writeln; END;
BEGIN initialize; textinput; analysis;
vowelout; END.
This basically strings the application together, starting by clearing the screen (in a dos prompt) and then outputting the vowels onto the screen. It then adds some formatting and outputs the current count of vowelcount (as above).
It will then request your input and finally it will output the contents of vowelcount again, which has been updated with the vowelcounts from the input you made.

Resources