Why is my PASCAL program not running? - pascal

Program TypeofCreditCard;
Var
AppliName: array[1..99] of String;
SSnum: array[1..99] of Integer;
GSal: array[1..99] of Integer;
TSalD: array[1..99] of Integer;
Name, CC : String;
Rep,Exp, GS,NS,Sum,TSD , YS,SSN,i, ,PofIncome : integer ;
YS, C_Amt: Real
Begin
Writeln ( 'Enter applicants who applied for a type of credit card');
Readln (Rep,Exp,GS,YS,NS,Sum,TSD,SSN,CC,PofIncome) ;
While ( Name <> ' Stop ' ) do
Begin
NS:= GS-TSD ;
Sum:= Exp + Rep ;
PofIncome:=(NS * 0.45);
Begin
If ( GS >4000) AND ( CC = 'Bronze Card' ) then
YS:= GS * 12 ;
C_Amt := YS * 0.25;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN ;
GSal [i]:= GS ;
TSalD [i]:= TSD ;
End ;
Begin
If (GS >= 7500) AND (CC= 'Gold Card') then
YS:= GS * 12 ;
C_Amt:= YS * 0.3;
i:= i + 1 ;
AppliName [i]:= Name;
SSnum [i]:=SSN;
GSal [i]:=GS;
TSalD [i]:=TSD;
End;
Begin
If (GS>=10,000) AND ( CC = ' Platinum Card') then
YS := GS * 12;
C_Amt: = YS * 0.4;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN;
GSal [i]:=GS;
TSalD [i]:= TSD;
End if
End if
End if
End While
End.
I am using free IDE Pascal and it is saying that I have 2 errors. If there are anymore errors or you see anything strange please inform me. The error says : Fatal:Syntax Error, ";" expected but "identifier Writeln found - Source of error- Writeln ( 'Enter applicants who applied for a type of credit card');

There's a ; missing after YS, C_Amt: Real. Also after each then should be a beginand all End if and End while at the end should all be End;

Related

Turbo Pascal result is not visible

can you say to me, where is the error. I wanna write in console the content of the a[i] in every for run. The output could be 6 digits. There is compiler Alert:
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
...Program finished with exit code 0
program Hello;
type aha = array [1..6] of integer;
var
a: aha;
n,x,i: integer;
begin
a[1]:=2;
a[2]:=6;
a[3]:=4;
a[4]:=2;
a[5]:=4;
a[6]:=3;
n:=6;
x:=a[1];
for i:=2 to n do
begin
{
if (a[i-1]>= x) then
begin
a[i]:=a[i] - x div 2;
end;
else
begin
a[i]:=a[i] + x;
x:= x + mod x(a[i] + 1);
end;
writeln (a[i]);
}
end;
end.

“END” expected but “IF” found. Stalked with some problem in turbo pascal code

program calc;
var a,b,c,d:real;
Begin
write('a=');readln(a);
write('b=');readln(b);
write('c=');readln(c);
if a = 0 then
if b = 0 then
if c = 0 then
writeln('equation undetermined,S=R')
else
begin
d := b * b - 4 * a * c; <<<< missed ';'?
if (d >= 0) then
begin
writeln('x1=',(-b-sqrt(d))/(2* a):6:2 ); <<< missed ')' ?
writeln('x2=',(-b+sqrt(d))/(2* a):6:2 ); <<< missed ')' ?
end;
else
writeln ('Equation has no real solutions');
end;
readln;
End.
I think you want to do this:
Program Calc;
var a,b,c,d: Real;
Begin
Write('a='); ReadLn(a);
Write('b='); ReadLn(b);
Write('c='); ReadLn(c);
if (a = 0) or (b = 0) or (c = 0) then
WriteLn('equation undetermined,S=R')
else
Begin
d := b * b - 4 * a * c;
if (d >= 0) then
Begin
WriteLn('x1=', (-b - sqrt(d)) / (2 * a):6:2 );
WriteLn('x2=', (-b + sqrt(d)) / (2 * a):6:2 );
end;
else
WriteLn('Equation has no real solutions');
end;
ReadLn;
End.
if ...
then if ...
then ...
else ...
might also compile as
if ...
then if ...
then ...
else ...
instead use
if ...
then begin
if ...
then ...
end
else ...

Is it possible to write function and procedure within single program in PL/SQL without using packages?

I'm trying to calculate LCM using GCF but somehow i'm getting error saying "no function with name LCM exists in the scope". What can i do about this?. I think this error is because i'm writing procedure and function together..
create or replace FUNCTION gcf (
x IN INTEGER,
y IN INTEGER
) RETURN INTEGER IS
res INTEGER;
BEGIN
IF ( y = 0 ) OR MOD(y,x) = 0 THEN
RETURN x;
ELSIF ( x = 0 ) THEN
RETURN y;
ELSIF ( x < y ) THEN
res := gcf(y,x);
ELSE
res := gcf(y,MOD(x,y) );
END IF;
RETURN res;
END;
/
create or replace PROCEDURE lcm (
num1 IN INTEGER,
num2 IN INTEGER,
answer OUT INTEGER
) IS
BEGIN
IF num1 = 0 AND num2 = 0 THEN
answer := 0;
ELSE
answer := abs(num1 * num2) / gcf(num1,num2);
END IF;
END lcm;
/
DECLARE
c integer;
BEGIN
dbms_output.put_line(' LCM(8, 12)-> ' || lcm(8, 12,c) );
dbms_output.put_line(' LCM(38,150)-> ' || lcm(38,150,c) );
dbms_output.put_line(' LCM(16,50)-> ' || lcm(16,60,c) );
dbms_output.put_line(' LCM(16,60)-> ' || lcm(16,60,c) );
dbms_output.put_line(' LCM(48,99)-> ' || lcm(48,99,c) );
END;
/
You can't use a PL/SQL procedure like a function. You must simply run it without being part of any other expression, which will set the value of the out parameter c.
DECLARE
c integer;
BEGIN
lcm(8, 12,c);
dbms_output.put_line(' LCM(8, 12)-> ' || c);
lcm(38,150,c);
dbms_output.put_line(' LCM(38,150)->' || c);
lcm(16,60,c);
dbms_output.put_line(' LCM(16,50)-> ' || c);
lcm(16,60,c);
dbms_output.put_line(' LCM(16,60)-> ' || c);
lcm(48,99,c);
dbms_output.put_line(' LCM(48,99)-> ' || c );
END;
/
Create the procedure and function inside the procedure as follows:
create or replace PROCEDURE lcm (
num1 IN INTEGER,
num2 IN INTEGER,
answer OUT INTEGER
) IS
FUNCTION gcf (
x IN INTEGER,
y IN INTEGER
) RETURN INTEGER IS
res INTEGER;
BEGIN
IF ( y = 0 ) OR MOD(y,x) = 0 THEN
RETURN x;
ELSIF ( x = 0 ) THEN
RETURN y;
ELSIF ( x < y ) THEN
res := gcf(y,x);
ELSE
res := gcf(y,MOD(x,y) );
END IF;
RETURN res;
END;
BEGIN
IF num1 = 0 AND num2 = 0 THEN
answer := 0;
ELSE
answer := abs(num1 * num2) / gcf(num1,num2);
END IF;
END lcm;
/
Then call it:
DECLARE
c INTEGER;
BEGIN
lcm (8, 12, c);
DBMS_OUTPUT.put_line (' LCM(8, 12)-> ' || c);
lcm (38, 150, c);
DBMS_OUTPUT.put_line (' LCM(38,150)-> ' || c);
lcm (16, 60, c);
DBMS_OUTPUT.put_line (' LCM(16,50)-> ' || c);
lcm (16, 60, c);
DBMS_OUTPUT.put_line (' LCM(16,60)-> ' || c);
lcm (48, 99, c);
DBMS_OUTPUT.put_line (' LCM(48,99)-> ' || c);
END;
The Lowest Common Multiple should be a function (since it returns a single value):
CREATE FUNCTION lcm (
num1 IN INTEGER,
num2 IN INTEGER,
) RETURN INTEGER DETERMINISTIC
IS
BEGIN
IF num1 = 0 AND num2 = 0 THEN
RETURN 0;
END IF;
RETURN abs(num1 * num2) / gcf(num1,num2);
END lcm;
/
Then you can use the code:
BEGIN
dbms_output.put_line(' LCM(8, 12)-> ' || lcm(8, 12) );
dbms_output.put_line(' LCM(38,150)-> ' || lcm(38,150) );
dbms_output.put_line(' LCM(16,50)-> ' || lcm(16,60) );
dbms_output.put_line(' LCM(16,60)-> ' || lcm(16,60) );
dbms_output.put_line(' LCM(48,99)-> ' || lcm(48,99) );
END;
/

Pascal duplicate identifier

program Noname4;
function minutes (Amin, Bmin :integer) : integer;
function time (Aval, Bval :integer) : integer;
begin
if (0 <= Aval) and (Bval < 24) then
time :=Bval - Aval;
if (0 <= Amin) and (Bmin < 60) then
minutes :=Bmin - Amin;
end;
var Aval, Amin, Bval, Bmin, n , x , i , y :integer;
duom, rez : text;
begin
readln(Aval, Amin, Bval, Bmin );
writeln(time(Aval, Bval));
writeln(minutes(Amin, Bmin));
readln;
assign(duom, 'Duomenys2.txt');
Reset(duom);
Readln(duom, n );
assign(rez, 'rezultatai2.txt');
rewrite(rez);
for i := 1 to n do
begin
Readln(duom, Aval, Amin, Bval, Bmin);
x := time(Aval, Bval);
y := minutes (Amin, Bmin);
writeln(rez, x);
writeln(rez, y);
end;
close(duom);
close(rez);
end.
Hello, I am getting a error (duplicate identifier, identifier already defined in line 2). It shows red in the ( duom, rez : text; ) line. Can't find out why
program Noname4;
function minutes (Amin, Bmin :integer) : integer;
function time (Aval, Bval :integer) : integer;
begin
if (0 <= Aval) and (Bval < 24) then
time :=Bval - Aval;
if (0 <= Amin) and (Bmin < 60) then
minutes :=Bmin - Amin;
end;
var Aval, Bval, n , x , i , y :integer;
duom, rez : text;
begin
assign(duom, 'Duomenys2.txt');
Reset(duom);
Readln(duom, n );
assign(rez, 'Rezultatai2.txt');
rewrite(rez);
for i := 1 to n do
begin
Readln(duom, Aval, Bval, Amin, Bmin);
x := time(Aval, Bval);
y := minutes(Amin, Bmin);
writeln(rez, x);
writeln(rez, y);
end;
close(duom);
close(rez);
end;
begin
end.
I did what you said, the program now works fine except it doesn't write the the answer in 'Rezultatai2.txt' file.
change:
var Aval, Amin, Bval, Bmin, n , x , i , y :integer;
duom, rez : text;
to:
var Aval, Bval, n , x , i , y :integer;
duom, rez : text;
they are already defined as variables passed into your minute function

How can I improve Oracle performance when performing a user defined Statistics function?

An associate of mine converted a javascript inverse chi-square routine to Oracle. The good news is that it returns the same results as the javascript routine. The bad news is that where it takes 1.5 seconds to return the result in IE or Chrome, it takes 23 seconds in Oracle. Of that 23 seconds >99% is CPU time.
There are two loops in the routines, an outer loop that executes 36 times for the values we are testing with, and an inner loop that runs 10,753 times for the each iteration of the outer loop. It does the same loop in both JS as it does in Oracle. For each iteration of the inner loop it executes an EXP function and an LN function, both of which are intrinsic in both languages.
I have compiled the Oracle code both Interpreted as well as Native, with little if any change (.045 second difference).
I have three questions;
Why is Oracle so slow/how can I improve it?
Is there an intrinsic inverse chi square function in Oracle.
Does anyone have an inverse chi-square function that does not require the iterative looping (or not as much) as the one I am using?
A bonus question is;
Does anyone have a routine that computes Confidence Intervals in PL/SQL, or a language that could easily be converted to PL/SQL?
As requested, here is the code, which is a bit long (The main routine is CRITCHI for testing P=0.975 and DF=21507.38);
BIGX Number :=20;
FUNCTION POZ(Z IN NUMBER) RETURN NUMBER IS
Y NUMBER;
X NUMBER;
W NUMBER;
Z_MAX NUMBER;
XXX NUMBER;
BEGIN
Z_MAX:=6.0;
IF (Z=0) THEN
X:= 0.0;
ELSE
Y := 0.5 * ABS(Z);
IF (Y >= (Z_MAX * 0.5)) THEN
X:= 1.0;
ELSIF (y < 1.0) THEN
W:= Y * Y;
X:= ((((((((0.000124818987 * W
- 0.001075204047) * W + 0.005198775019) * W
- 0.019198292004) * W + 0.059054035642) * W
- 0.151968751364) * W + 0.319152932694) * W
- 0.531923007300) * W + 0.797884560593) * Y * 2.0;
ELSE
Y:= Y-2.0;
Y:= (((((((((((((-0.000045255659 * Y
+ 0.000152529290) * Y - 0.000019538132) * Y
- 0.000676904986) * Y + 0.001390604284) * Y
- 0.000794620820) * Y - 0.002034254874) * Y
+ 0.006549791214) * Y - 0.010557625006) * Y
+ 0.011630447319) * Y - 0.009279453341) * Y
+ 0.005353579108) * Y - 0.002141268741) * Y
+ 0.000535310849) * Y + 0.999936657524;
END IF;
END IF;
IF (Z>0.0) THEN
XXX:=((X + 1.0) * 0.5);
ELSE
XXX:= ((1.0 - x) * 0.5);
END IF;
RETURN XXX;
END POZ;
FUNCTION EX(X IN NUMBER) RETURN NUMBER IS
BEGIN
IF (x < -BIGX) THEN
RETURN 0;
ELSE
RETURN EXP(X);
END IF;
END EX;
FUNCTION POCHISQ(X IN NUMBER, DF IN NUMBER) RETURN NUMBER IS
A NUMBER;
Y NUMBER;
S NUMBER;
E NUMBER;
C NUMBER;
Z NUMBER;
X1 NUMBER;
EVEN BOOLEAN; /* True if df is an even number */
LOG_SQRT_PI NUMBER := 0.5723649429247000870717135; /* log(sqrt(pi)) */
I_SQRT_PI NUMBER := 0.5641895835477562869480795; /* 1 / sqrt(pi) */
b1 PLS_INTEGER;
b2 PLS_INTEGER;
e1 PLS_INTEGER;
e2 PLS_INTEGER;
BEGIN
b1 := DBMS_UTILITY.GET_TIME();
b2 := DBMS_UTILITY.GET_CPU_TIME();
X1:=X;
IF (X1 <= 0.0 OR DF < 1) THEN
RETURN 1.0;
END IF;
A:= 0.5 * X1;
EVEN:= (MOD(DF,2)=0);
IF (DF > 1) THEN
Y := ex(-A);
END IF;
IF EVEN THEN
S:=Y;
ELSE
S:=(2.0 * poz(-sqrt(X1)));
END IF;
IF (DF > 2) THEN
X1:= 0.5*(DF-1.0);
IF EVEN THEN
Z:=1.0;
ELSE
Z:=0.5;
END IF;
IF (A > BIGX) THEN
IF EVEN THEN
E:=0.0;
ELSE
E:=LOG_SQRT_PI;
END IF;
C:= LN(A);
/* Timming snippet */
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '0-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '0-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
/* End of Timming snippet */
WHILE (Z <= X1)
LOOP
E:= LN(Z) + E;
S:=S+EX(C * Z - A - E);
Z:=Z+1.0;
END LOOP;
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '1-GET_TIME elapsed = ' || e1 || ' hsecs. Z= ' || Z );
--DBMS_OUTPUT.PUT_LINE( '1-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN S;
ELSE
IF EVEN THEN
E:=1.0;
ELSE
E:=(I_SQRT_PI / sqrt(A));
END IF;
C:= 0.0;
WHILE (Z <= X1)
LOOP
E:= E * (A / Z);
C:= C + E;
Z:=Z+ 1.0;
END LOOP;
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '2-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '2-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN C * Y + S;
END IF;
ELSE
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '3-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '3-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN S;
END IF;
END POCHISQ;
/* CRITCHI -- Compute critical chi-square value to
produce given p. We just do a bisection
search for a value within CHI_EPSILON,
relying on the monotonicity of pochisq(). */
FUNCTION CRITCHI(P IN NUMBER, DF IN NUMBER) RETURN NUMBER IS
CHI_EPSILON NUMBER:= 0.000001; /* Accuracy of critchi approximation */
CHI_MAX NUMBER:= 99999.0; /* Maximum chi-square value */
minchisq NUMBER:= 0.0;
maxchisq NUMBER:= CHI_MAX;
chisqval NUMBER;
dummy_count number := 0;
BEGIN
IF (p <= 0.0) THEN
RETURN maxchisq;
ELSE
IF (p >= 1.0) THEN
RETURN 0.0;
END IF;
END IF;
chisqval:= df / sqrt(p); /* fair first value */
WHILE ((maxchisq - minchisq) > CHI_EPSILON)
LOOP
if (pochisq(chisqval, df) < p) THEN
maxchisq:= chisqval;
ELSE
minchisq:= chisqval;
END IF;
chisqval:= (maxchisq + minchisq) * 0.5;
dummy_count := dummy_count + 1;
END LOOP;
--DBMS_OUTPUT.PUT_LINE('chisqval = ' || chisqval);
RETURN chisqval;
END CRITCHI;
For the benefit of future seekers, who might not have the patience to trawl through all the comments, the following optimizations were applied to the program to make it run fast.
All the numeric variables were declared as BINARY_INTEGER(#). Find out more.
Functions were declared as deterministic.
Functions were compiled into native C.
(#) On more modern versions of the database PLS_INTEGER is preferred (simply because BINARY_INTEGER is old and deprecated - it gets converted to PLS_INTEGER under the covers) .
NB - if the OP or #AlexPoole would care to write a similar response, I'll happily upvote their answer and delete this one.

Resources