How to fix run-time error 201 - freepascal

help me, please.
I don't know how to fix this error.
Program Polynomial;
type
arrayOfInt = Array[1..21] of Integer;
biggerArrayOfInt = Array[1..41] of Integer;
function isNumber(c : Char): Boolean;
var
res : Boolean;
code : Longint;
begin
code := Ord(c);
if ((code > 47) AND (code < 58)) then begin
res := true
end
else
begin
res := false;
end;
isNumber := res;
end;
function parsePolynomial(polynomial : String): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
number : Integer;
coef : Integer;
tmp : String;
i, j : Integer;
positive : Boolean;
numberPosition, numberLength : Integer;
expectX : Boolean;
begin
i := 1;
for j:=1 to Length(coeficients) do
begin
coeficients[j] := 0;
end;
while (true) do
begin
coef := 0;
number := 0;
positive := true;
expectX := true;
if(polynomial[i] = '-') then begin
positive := false;
i := i + 1;
end;
if(polynomial[i] = '+') then begin
i := i + 1;
end;
if(isNumber(polynomial[i])) then begin
numberPosition:= i;
while (isNumber(polynomial[i])) do
begin
i := i + 1;
end;
tmp := Copy(polynomial, numberPosition, i - numberPosition);
Val(tmp, number);
if(not positive) then begin
number := number * -1;
end;
if ((not(polynomial[i] = '*')) OR (i > length(polynomial))) then
begin
expectX := false;
end
else
begin
i:= i + 1;
end;
end
else
begin
if(positive) then begin
number := 1;
end
else
begin
number := -1;
end;
end;
if (expectX) then begin
if(not(polynomial[i] = 'x')) then begin
write('Bad input!');
exit;
end
else
begin
i := i + 1;
if (polynomial[i] = '^') then begin
i := i + 1;
if (not isNumber(polynomial[i])) then begin
write('Bad input!');
exit;
end;
numberPosition:= i;
while (isNumber(polynomial[i])) do
begin
i := i + 1;
end;
tmp := Copy(polynomial, numberPosition, i - numberPosition);
Val(tmp, coef);
end
else
begin
coef := 1;
end;
end;
end;
coeficients[coef + 1] := number;
if ((length(polynomial)) - 1 < i) then begin
break;
end;
end;
parsePolynomial := coeficients;
end;
function sumPolynomial(polynomial1, polynomial2 : array of Integer): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
i : Integer;
begin
for i := Length(polynomial1) downto 0 do
begin
coeficients[i+1] := polynomial1[i] + polynomial2[i];
end;
sumPolynomial := coeficients;
end;
function productOfPolynomial(polynomial1, polynomial2 : array of Integer): biggerArrayOfInt;
var
coeficients : Array[1..41] of Integer;
i, j : Integer;
begin
for j:=1 to Length(coeficients) do
begin
coeficients[j] := 0;
end;
for i := Length(polynomial1) downto 0 do
begin
for j := Length(polynomial2) downto 0 do
begin
coeficients[i+j+1] := coeficients[i+j+1] + polynomial1[i] * polynomial2[j];
end;
end;
productOfPolynomial := coeficients;
end;
function substractOfPolynomial(polynomial1, polynomial2 : array of Integer): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
i : Integer;
begin
for i := Length(polynomial1) downto 0 do
begin
coeficients[i+1] := polynomial1[i] - polynomial2[i];
end;
substractOfPolynomial := coeficients;
end;
procedure printPolynomial(polynomial: array of Integer);
var
i : Integer;
isFirst : Boolean;
isZero : Boolean;
begin
isFirst := true;
isZero := true;
for i := length(polynomial) downto 0 do
begin
if polynomial[i] <> 0 then begin
isZero := false;
if((not isFirst) AND (polynomial[i] > 0)) then begin
write('+');
end;
if((polynomial[i] = -1)) then begin
write('-');
end;
if(((polynomial[i] > 1) OR (polynomial[i] < -1)) OR ((i = 0) AND not(polynomial[i] = 0))) then begin
write(polynomial[i]);
if((i > 0)) then begin
write('*');
end;
end;
if(i > 0) then begin
write('x');
isFirst := false;
if (i > 1) then begin
write('^', i);
end;
end;
write()
end;
end;
if (isZero) then begin
write(0);
end;
end;
var
polynomial1, polynomial2, result: Array[1..21] of Integer;
polynomialInput: String;
begin
readln(polynomialInput);
polynomial1 := parsePolynomial(polynomialInput);
readln(polynomialInput);
polynomial2 := parsePolynomial(polynomialInput);
printPolynomial(sumPolynomial(polynomial1, polynomial2));
writeln('');
printPolynomial(substractOfPolynomial(polynomial1, polynomial2));
writeln('');
printPolynomial(productOfPolynomial(polynomial1, polynomial2));
end.

Runtime error 201 means range check error. Quick glance over the source code makes me suspect that somewhere some operation returns a value that doens't fit in integer range (-32786..32767 in FreePascal by default). Easiest solution would be to use a larger datatype for example longint (roughly between -2*10^9..2*10^9) or int64 (~9*10^18..9*10^18).
If you use command line compiler using -gl command line option would display line numbers in run-time error backtraces. This would make it easier for you to pinpoint the issue.

Related

I Have an error in MySQL. Show me a syntaxt error in my query

I have this query in MySQL
create function is_prime(num int) returns boolean
begin
declare cont;
set cont = 2;
while(cont != num) do
if (num % cont = 0) then
return false;
end if;
cont = cont + 1;
end while;
return true;
end//
VSCode show a syntaxt error. I have checked each line but i dont look the error.
Thanks and sorry for my english.
Declaration has to have a datatype
Also you forgot to Set the increase
DELIMITER //
create function is_prime(num int) returns boolean
begin
declare cont INTEGER;
set cont = 2;
while(cont != num) do
if (num % cont = 0) then
return false;
end if;
SET cont := cont + 1;
end while;
return true;
end//

MySQL - Capital first letter of each word

Below is MySQL function to capitalize the first letter of every word in a string.
USE `db`$$
DROP FUNCTION IF EXISTS `UC_Words`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_Words`(str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET latin1
MODIFIES SQL DATA
DETERMINISTIC
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INT DEFAULT 1;
DECLARE BOOL INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET BOOL = 1;
ELSEIF BOOL=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET BOOL = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET BOOL = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END$$
DELIMITER ;
It's works fine for all Strings. But if I provide string "M R E" then its give "M R e" output.
plz suggest.
First execute below query
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_FIRST`(oldWord VARCHAR(255)) RETURNS varchar(255) CHARSET latin1
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2))
Then execute below query
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_Words`(oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL) RETURNS varchar(255) CHARSET latin1
BEGIN
SET #oldString := oldName;
SET #newString := "";
tokenLoop: LOOP
IF trimSpaces THEN SET #oldString := TRIM(BOTH " " FROM #oldString); END IF;
SET #splitPoint := LOCATE(delim, #oldString);
IF #splitPoint = 0 THEN
SET #newString := CONCAT(#newString, UC_FIRST(#oldString));
LEAVE tokenLoop;
END IF;
SET #newString := CONCAT(#newString, UC_FIRST(SUBSTRING(#oldString, 1, #splitPoint)));
SET #oldString := SUBSTRING(#oldString, #splitPoint+1);
END LOOP tokenLoop;
RETURN #newString;
END
Then after call function
SELECT UC_Words("this is for testing"," ", TRUE);
Output below
This Is For Testing

What are the correct terms to use to calculate the average value of running total that is within a procedure?

I'd like to calculate the average value of a running total up to that point of mathematical operations within a procedure. In particular, I'd like to divide by this average value that has gone into a running total. However, when I attempt it with the following code (relevant part is bolded below), I get an error that says:
invalid use of group function
It appears I can't try to calculate an aggregate of an aggregate in MySQL, but I'm not sure how else to do this:
I'd like to create a column that looks like the second one below (expressing the average of values up until that row:
conFIP average_conFIP_for_rows_passed_up_to_that_point
1 1
3 2
4 2.66
6 3.5
2 3.2
5 3.5
The relevant part of the code is: +(accum_cFIP/Count(conFIP)) which is part of
SET FI_pit := (((accum_HR*13)+(3*(accum_walks_a+accum_HBP))-(2*accum_K))/accum_ip)+(accum_cFIP/Count(accum_cFIP));
DROP PROCEDURE IF EXISTS starting_pitcher_stats_FIP1()
DELIMITER $$
CREATE PROCEDURE starting_pitcher_stats_FIP1()
BEGIN
DECLARE pit_id CHAR (10);
DECLARE gdate DATE;
DECLARE seq INT;
DECLARE YEARID INT;
DECLARE TEAM CHAR (3);
DECLARE HR INT;
DECLARE walks INT;
DECLARE HitBP INT;
DECLARE K INT;
DECLARE in_pit REAL;
DECLARE conFIP VARCHAR (255);
DECLARE accum_ip REAL;
DECLARE accum_walks_a REAL;
DECLARE accum_HR REAL;
DECLARE accum_HBP REAL;
DECLARE accum_K REAL;
DECLARE accum_cFIP REAL;
DECLARE FI_pit REAL;
DECLARE FIP_g REAL;
DECLARE prev_year YEAR(4);
DECLARE end_of_cursor BOOLEAN;
DECLARE no_table CONDITION FOR SQLSTATE '42S02';
DECLARE c1 CURSOR FOR
SELECT Starting_Pitcher, Game_Date, Game_Number, YEAR_ID, FLD_TEAM_ID, HR_a, walks_a, HBP, strike_outs, innings_pitched, cFIP
FROM starting_pitcher_game_log
ORDER BY Starting_Pitcher, Game_Date, Game_Number;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET end_of_cursor := TRUE;
SET end_of_cursor := FALSE; -- reset
SET prev_year := 0; -- reset control-break
OPEN c1;
fetch_loop: LOOP
FETCH c1 INTO pit_id, gdate, seq, YEARID, TEAM, HR, walks, HitBP, K, in_pit, conFIP;
IF end_of_cursor THEN
LEAVE fetch_loop;
END IF;
-- check control-break conditions
IF YEAR(gdate) != prev_year THEN
SET accum_ip := 0.0;
SET accum_walks_a := 0;
SET accum_HR := 0;
SET accum_HBP := 0;
SET accum_K := 0;
SET accum_cFIP := 0;
SET prev_year := YEAR(gdate);
END IF;
SET accum_ip := accum_ip + in_pit;
SET accum_walks_a := accum_walks_a + walks;
SET accum_HR:= accum_HR + HR;
SET accum_HBP := accum_HBP + HitBP;
SET accum_K := accum_K + K;
SET accum_cFIP := accum_cFIP + conFIP;
IF (accum_ip) = 0 OR (in_pit)=0 THEN
SET FI_pit := 0;
SET FIP_g := 0;
ELSE
SET FI_pit := (((accum_HR*13)+(3*(accum_walks_a+accum_HBP))-(2*accum_K))/accum_ip)+conFIP;
SET FIP_g := (((HR*13)+(3*(walks+hitBP))-(2*K))/in_pit)+conFIP;
END IF;
UPDATE starting_pitcher_stats
SET std_FIP = FI_pit,
FIP_game = FIP_g
WHERE Starting_Pitcher = pit_id
AND Game_Date = gdate
AND Game_Number = seq;
END LOOP;
CLOSE c1;
END
$$
DELIMITER ;

Why my Sql Script does not work?

I am newbie to MySQL
I am trying to generate random data and put it into table 'data'
But SQL shell says that my sql code have syntax error
But I can't find why
DECLARE #counter smallint;
DECLARE #A BOOLEAN;
DECLARE #B BOOLEAN;
DECLARE #C int(4);
DECLARE #LABEL BOOLEAN;
SET #counter = 1;
WHILE #counter < 100
BEGIN
IF 0.5 > RAND()
SET #A = TRUE;
ELSE
SET #A = FALSE;
IF 0.5 > RAND()
SET #B = TRUE;
ELSE
SET #B = FALSE;
SET #C = RAND() * 10
SET #LABEL = #A ^ #B OR #LABEL > 5
INSERT INTO data (A,B,C,LABEL) VALUES (#A,#B,#C,#LABEL)
#count = #count +1
END
It says that I have syntax problem
from declaring variables
can you help me?
Is missing some syntax words:
IF... THEN ... ELSE ... END IF;
WHILE ... DO ... END WHILE;
Is important understand that user variables are written as #var_name, so user-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client session are automatically freed when that client exits. In this case, I think that is not necessary.
SET, either = or := can be used as the assignment operator.
Try this:
delimiter //
CREATE PROCEDURE TEST()
BEGIN
DECLARE counter smallint;
DECLARE A BOOLEAN;
DECLARE B BOOLEAN;
DECLARE C int(4);
DECLARE LABEL BOOLEAN;
SET counter = 1;
WHILE counter < 100 DO
IF 0.5 > RAND() THEN
SET A = TRUE;
ELSE
SET A = FALSE;
END IF;
IF 0.5 > RAND() THEN
SET B = TRUE;
ELSE
SET B = FALSE;
END IF;
SET C = RAND() * 10;
IF A ^ B OR LABEL > 5 THEN
SET LABEL = TRUE;
ELSE
SET LABEL = FALSE;
END IF;
INSERT INTO data (A,B,C,LABEL) VALUES (A,B,C,LABEL);
SET counter = counter +1;
END WHILE;
END//
try this,
DECLARE #counter smallint,
DECLARE #A BOOLEAN,
DECLARE #B BOOLEAN,
DECLARE #C int(4),
DECLARE #LABEL BOOLEAN;

Using function name by variable in pl/sql

I am working on PL/SQL code, My purpose is to declare function with definition then assign it to variable then call it using that variable.
declare
func varchar2(50);
function add(a NUMBER,b NUMBER)
return BOOLEAN is
c NUMBER;
begin
c := a+b;
dbms_output.put_line(c);
end add;
begin
func :='add'; or func :=add;
if (func(10,20)=false) then
dbms_output.put_line('false statement');
else
dbms_output.put_line('true statement');
end if;
end;
But this is not working. So I am not getting how to assign function to variable.
You should build an anonymous block with the function call you need and then call it with EXECUTE IMMEDIATE. You should know how many arguments the function has though.
Something like this:
DECLARE
func VARCHAR2(20);
value1 NUMBER := 10;
value2 NUMBER := 20;
plsql_block VARCHAR2(500);
out_value NUMBER;
BEGIN
func := 'add';
plsql_block := 'BEGIN :v := ' || func || '(:v1,:v2); END;';
EXECUTE IMMEDIATE plsql_block USING OUT out_value, IN value1, value2;
IF out_value > 0 THEN
dbms_output.put_line('TRUE statement');
ELSE
dbms_output.put_line('FALSE statement');
END IF;
END;