pascal scale of notation - scale

I'm doing a procedure that reads numbers character by character.
procedure ReadLongint (var success : boolean; var result : longin);
var
c : char;
res : longint;
pos : integer;
begin
res := 0;
pos := 0;
repeat
read(c);
pos := pos + 1
until (c <> ' ') and (c <> #10);
while (c <> ' ') and (c <> #10) do
begin
if (c < '0') or (c > '9') then
begin
writeln('Unexpected ''', c, ''''' in pos: ', pos);
readln;
success := false;
exit
end;
res := res*10 + ord(c) - ord('0');
read(c);
pos := pos + 1
end;
result := res;
success := true
end;
I'm trying to make it with the ability to select any number systems up to 36.
procedure ReadLongint (var success : boolean; var result : longint; var notation : char);
var
c : char;
res : longint;
pos : integer;
begin
res := 0;
pos := 0;
repeat
read(c);
pos := pos + 1
until (c <> ' ') and (c <> #10);
while (c <> ' ') and (c <> #10) do
begin
if (notation > #48) and (notation < #58) then
begin
res := res*10 + ord(c) - ord('0');
end;
if (notation > #64) and (notation < #91) then
begin
res := res*10 + ord(c) - ord('0');?????????
end;
read(c);
pos := pos + 1;
end;
result := res;
success := true
end;
I'm trying to make it with the ability to select any number systems up to 36.
When choosing from 2 to 10, the reading algorithm is the same res := res*10 + ord(c) - ord('0');
But how to correctly read the number of systems of calculation, from A to Z?
Tell me, please.

Related

Error on running a pl/sql program to find sum of first 10 prime numbers

DECLARE
COUNT NUMBER;
prime NUMBER;
SUM NUMBER;
i NUMBER;
j NUMBER;
BEGIN
SUM := 2;
COUNT := 0;
prime := 1;
i := 3;
WHILE COUNT < 10
LOOP
j := i / 2;
FOR k IN 2 .. j
LOOP
IF i MOD j = 0
THEN
prime := 0;
END IF;
IF prime = 1
THEN
SUM := SUM + i;
COUNT := COUNT + 1;
END IF;
END LOOP;
i := i + 1;
END LOOP;
DBMS_OUTPUT.put_line (SUM);
END;
/
I'm trying to calculate the sum first 10 prime numbers. I wrote the above code but when I try to execute it, it throws an error:
ORA-06550: line 29, column 12: PLS-00103: Encountered the symbol
"+" when expecting one of the following: ( This error corresponds
to the line where I'm incrementing the value of sum.
COUNT and SUM are reserved keywords and must not be choosen as variable-names.
Add a prefix "v":
DECLARE
vCOUNT NUMBER;
prime NUMBER;
vSUM NUMBER;
i NUMBER;
j NUMBER;
BEGIN
vSUM := 2;
vCOUNT := 0;
prime := 1;
i := 3;
WHILE vCOUNT < 10
LOOP
j := i / 2;
FOR k IN 2 .. j
LOOP
IF i MOD j = 0
THEN
prime := 0;
END IF;
IF prime = 1
THEN
vSUM := vSUM + i;
vCOUNT := vCOUNT + 1;
END IF;
END LOOP;
i := i + 1;
END LOOP;
DBMS_OUTPUT.put_line (vSUM);
END;
/
Here a good article about naming-conventions in plsql:
https://www.guru99.com/pl-sql-identifiers.html
The error-message "expecting '('" shows that your db wants something like "SUM(someValue)".
An example would be something like this:
SELECT SUM(col1) FROM mytable

Pascal pointers not behaving as expected

For CodinGame I'm building a referee for a card game called War. Rules described here). TL;DR: the persons with the highest drawn card adds both cards to the bottom of his/her card stack.
I've build a linked-list in Pascal to hold the card stacks. But pascal pointers are not behaving as I expect:
For example, I give the program the following input (slightly modified):
9
8C
KD
AH
QH
3D
KD
AH
QH
6D
9
8D
2D
3H
4D
4S
2D
3H
4D
7H
The example output is:
nrCards: 5
Player1: 13 14 12 6
Player2: 2 3 4 7
Player1: 14 12 6
Player2: 3 4 7
Player1: 12 6
Player2: 4 7
Player1: 6
Player2: 7
Player1:
Player2: 6 7
2 5
I.e., the nextNode pointer of the last element is mostly not updated properly...
Code:
program Answer;
{$H+}
uses sysutils, math, strutils;
type
TNode = record
val : Int32;
nextNode : ^TNode;
end;
TNodePtr = ^TNode;
TNodePtrPtr = ^TNodePtr;
var
size1 : Int32;
size2 : Int32;
cards : Array of TNode;
player1first : TNodePtr = nil;
player1last : TNodePtr = nil;
player2first : TNodePtr = nil;
player2last : TNodePtr = nil;
winnerLast : TNodePtrPtr = nil;
i : Int32;
Line: String;
turns : Int32 = 0;
nrCards : Int32 = 1;
cardIt : TNodePtr = nil;
function ParseIn(i : Int32) : String;
begin
ParseIn := ExtractWord(i, Line, [' ']);
end;
function War(pl1it, pl2it : TNodePtr) : Int32;
var
nrCards : Int32 = 5;
i :Int32; // not reuse?
begin
for i := 0 to 3 do begin
pl1it := pl1it^.nextNode;
pl2it := pl2it^.nextNode;
if (pl1it = nil) or (pl2it = nil) then
exit(0);
end;
while (pl1it^.val = pl2it^.val) do begin
nrCards := nrCards + 4;
for i := 0 to 3 do begin
pl1it := pl1it^.nextNode;
pl2it := pl2it^.nextNode;
if (pl1it = nil) or (pl2it = nil) then
exit(0);
end;
end;
if pl1it^.val > pl2it^.val then
// player 1 wins
War := nrCards
else
// player 2 wins
War := -nrCards;
end;
begin
readln(Line);
size1 := StrToInt(ParseIn(1));
Setlength(cards, size1);
for i := 0 to size1-1 do begin
readln(Line);
//writeln(StdErr, Line);
case Line[1] of
'1' : cards[i].val := 10;
'J' : cards[i].val := 11;
'Q' : cards[i].val := 12;
'K' : cards[i].val := 13;
'A' : cards[i].val := 14;
else cards[i].val := Integer(Line[1])-48;
end;
if i = size1-1 then
cards[i].nextNode := nil
else
cards[i].nextNode := #cards[i+1];
end;
readln(Line);
size2 := StrToInt(ParseIn(1));
Setlength(cards, size1+size2);
for i := size1 to size1+size2-1 do begin
readln(Line);
//writeln(StdErr, Line);
case Line[1] of
'1' : cards[i].val := 10;
'J' : cards[i].val := 11;
'Q' : cards[i].val := 12;
'K' : cards[i].val := 13;
'A' : cards[i].val := 14;
else cards[i].val := Integer(Line[1])-48;
end;
if i = size1+size2-1 then
cards[i].nextNode := nil
else
cards[i].nextNode := #cards[i+1];
end;
player1first := #cards[0];
player1last := #cards[size1-1];
player2first := #cards[size1];
player2last := #cards[size1+size2-1];
// now for the game
while (player1first <> nil) and (player2first <> nil) do begin
if player1first^.val <> player2first^.val then begin
if player1first^.val > player2first^.val then begin
// player 1 wins
writeln(StdErr, 'Player1 wins');
winnerLast := #player1last;
end else begin
// player 2 wins
writeln(StdErr, 'Player2 wins');
winnerLast := #player2last;
end;
winnerLast^^.nextNode := player1first;
winnerLast^ := player1first;
player1first := player1first^.nextNode;
winnerLast^^.nextNode := player2first;
winnerLast^ := player2first;
player2first := player2first^.nextNode;
winnerLast^^.nextNode := nil;
end else begin
// war
nrCards := War(player1first, player2first);
writeln(StdErr, 'nrCards: ', nrCards);
if nrCards = 0 then
break;
if nrCards > 0 then begin
writeln(StdErr, 'Player1 wins');
winnerLast := #player1last;
end else begin
writeln(StdErr, 'Player2 wins');
winnerLast := #player2last;
nrCards := -nrCards;
end;
for i := 0 to nrCards-1 do begin
winnerLast^^.nextNode := player1first;
winnerLast^ := player1first;
player1first := player1first^.nextNode;
end;
for i := 0 to nrCards-1 do begin
winnerLast^^.nextNode := player2first;
winnerLast^ := player2first;
player2first := player2first^.nextNode;
end;
winnerLast^^.nextNode := nil;
end;
turns := turns + 1;
write(StdErr, 'Player1: ');
cardIt := player1first;
while cardIt <> nil do begin
write(StdErr, cardIt^.val, ' ');
cardIt := cardIt^.nextNode;
end;
writeln(StdErr, ' ');
write(StdErr, 'Player2: ');
cardIt := player2first;
while cardIt <> nil do begin
write(StdErr, cardIt^.val, ' ');
cardIt := cardIt^.nextNode;
end;
writeln(StdErr, ' ');
end;
// end game
if nrCards = 0 then
// equal
writeln('PAT')
else if player2first = nil then
// player 2 won
writeln('1 ',turns)
else
// player 2 won
writeln('2 ',turns);
flush(StdErr); flush(output); // DO NOT REMOVE
end.
I have a background in C/C++/C#, which could explain my coding style. The same program in C works correctly. IMHO I've literally translated it...
C code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct TNode {
int val;
struct TNode* nextNode;
};
int War(struct TNode* pl1It, struct TNode* pl2It) {
int nrCards = 5;
for (int i = 0; i < 4; ++i) {
pl1It = pl1It->nextNode;
pl2It = pl2It->nextNode;
if (pl1It == NULL || pl2It == NULL) {
return 0;
}
}
while (pl1It->val == pl2It->val) {
nrCards += 4;
for (int i = 0; i < 4; ++i) {
pl1It = pl1It->nextNode;
pl2It = pl2It->nextNode;
if (pl1It == NULL || pl2It == NULL) {
return 0;
}
}
}
if (pl1It->val > pl2It->val) {
// player 1 wins
return nrCards;
} else {
// player 2 wins
return -nrCards;
}
}
int main()
{
struct TNode cards[52];
int size1;
scanf("%d", &size1);
for (int i = 0; i < size1; ++i) {
char Line[4];
scanf("%s", Line);
if (Line[0] == '1') cards[i].val = 10;
else if (Line[0] == 'J') cards[i].val = 11;
else if (Line[0] == 'Q') cards[i].val = 12;
else if (Line[0] == 'K') cards[i].val = 13;
else if (Line[0] == 'A') cards[i].val = 14;
else cards[i].val = (int)Line[0] - 48;
if (i == size1 - 1) cards[i].nextNode = NULL;
else cards[i].nextNode = &cards[i + 1];
}
int size2;
scanf("%d", &size2);
for (int i = size1; i < size1 + size2; ++i) {
char Line[4];
scanf("%s", Line);
if (Line[0] == '1') cards[i].val = 10;
else if (Line[0] == 'J') cards[i].val = 11;
else if (Line[0] == 'Q') cards[i].val = 12;
else if (Line[0] == 'K') cards[i].val = 13;
else if (Line[0] == 'A') cards[i].val = 14;
else cards[i].val = (int)Line[0] - 48;
if (i == size1 + size2 - 1) cards[i].nextNode = NULL;
else cards[i].nextNode = &cards[i + 1];
}
struct TNode* player1first = &cards[0];
struct TNode* player1last = &cards[size1 - 1];
struct TNode* player2first = &cards[size1];
struct TNode* player2last = &cards[size1 + size2 - 1];
int nrOfCards = 1; // has to do with check
int turns = 0;
// now for the game
while (player1first != NULL && player2first != NULL) {
if (player1first->val != player2first->val) {
struct TNode** winnerLast = NULL;
if (player1first->val > player2first->val) {
// player 1 wins
fprintf(stderr, "Player 1 wins.\n");
winnerLast = &player1last;
} else {
// player 2 wins
fprintf(stderr, "Player 2 wins.\n");
winnerLast = &player2last;
}
(*winnerLast)->nextNode = player1first;
(*winnerLast) = player1first;
player1first = player1first->nextNode;
(*winnerLast)->nextNode = player2first;
(*winnerLast) = player2first;
player2first = player2first->nextNode;
(*winnerLast)->nextNode = NULL;
} else {
// war
fprintf(stderr, "War: ");
nrOfCards = War(player1first, player2first);
if (nrOfCards == 0) break;
struct TNode** winnerLast;
if (nrOfCards > 0) {
// Player 1 wins
fprintf(stderr, "Player 1 wins.\n");
winnerLast = &player1last;
} else {
// Player 2 wins
fprintf(stderr, "Player 2 wins.\n");
nrOfCards = -nrOfCards;
winnerLast = &player2last;
}
for (int i = 0; i < nrOfCards; ++i) {
(*winnerLast)->nextNode = player1first;
(*winnerLast) = player1first;
player1first = player1first->nextNode;
}
for (int i = 0; i < nrOfCards; ++i) {
(*winnerLast)->nextNode = player2first;
(*winnerLast) = player2first;
player2first = player2first->nextNode;
}
(*winnerLast)->nextNode = NULL;
}
turns = turns + 1;
}
// end game
if (nrOfCards == 0) {
// equal
printf("PAT\n");
} else if (player2first == NULL) {
// player 2 won
printf("1 %d\n", turns);
} else {
// player 2 won
printf("2 %d\n", turns);
}
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
return 0;
}
Can somebody explain why pascal pointers are behaving so differently, or what I am doing wrong?
I found the problem, which I do not fully understand yet.
The problem is caused by de use of dynamic array cards. That's also the big difference between the C code and the pascal code. I'm was doing
readln(Line);
size1 := StrToInt(ParseIn(1));
Setlength(cards, size1);
for i := 0 to size1-1 do begin
readln(Line);
[...]
end;
readln(Line);
size2 := StrToInt(ParseIn(1));
Setlength(cards, size1+size2);
for i := size1 to size1+size2-1 do begin
That second Setlength seems to have invalidated the last element of my initial array.. It seems player1last^.nextNode and cards[size-1].nextNode were no longer connected. I don't exactly understand what happened underwater... I can only guess that Setlength reallocates the memory and copies the contents thereby invalidating all existing pointers! If this is the case, this was not properly explained in the tutorials I found.
I could luckily fix this as there are a maximum of 52 cards, so by choosing a fixed-length array I could fix this.
cards : Array[0..51] of TNode;
EDIT:
OK I found a source giving me the answer Dynamic array - Free Pascal wiki
Although writing to elements of dynamic arrays does not create a new instance of the array (no copy-on-write as it exists for Ansistrings) using SetLength on such arrays does create a copy! So if 2 dynamic array variables point to the same array (one has been assigned to the other) they do not do so after using SetLength on one (or both) of them. After the SetLength() call the two variables are distinct arrays whose elements are independent from each other.

Pascal error: illegal qualifier

The code below is supposed to create a 3x4 matrix and and print the sum of all numbers each row. However, upon compiling it, I keep getting the following errors:
jdoodle.pas(26,25) Error: Illegal qualifier
jdoodle.pas(33,32) Error: Illegal qualifier
jdoodle.pas(41,32) Error: Illegal qualifier
jdoodle.pas(48,24) Error: Illegal qualifier
jdoodle.pas(56,4) Fatal: There were 4 errors compiling module, stopping
Fatal: Compilation aborted.
The lines in question are:
line 26: readln (A[i, j]);
line 33: B[i] := B[i] + A[i, j]
line 41: C[J] := C [J] + A[i,j]
line 48: write (A[i, j]:5);
Could anyone clarify this for me please? The main problem is, that I don't really know what that error code means. I have noticed that 'A[i,j]' is in all lines that are mentioned in the errors, but I just can't find out what is wrong with it.Any help would be greatly appreciated!
And here is my complete code:
Program Matrix (input, output);
const
ZEILENMAX = 3;
SPALTENMAX = 4;
type
tZeile = 1..ZEILENMAX;
tSpalte = 1..SPALTENMAX;
tMatrix = array[tZeile] of integer;
tZeilensumme = array [tZeile] of integer;
tSpaltensumme = array [tSpalte] of integer;
var
A : tMatrix;
B : tZeilensumme;
C : tSpaltensumme;
i : tZeile;
j : tSpalte;
begin
for i := 1 to ZEILENMAX do
for j := 1 to SPALTENMAX do
readln (A[i, j]);
for i := 1 to ZEILENMAX do
begin
B[i] := 0;
for j := 1 to SPALTENMAX do
B[i] := B[i] + A[i, j]
end;
for j := 1 to SPALTENMAX do
begin
C[j] := 0;
for i := 1 to Zeilenmax do
C[J] := C [J] + A[i,j]
end;
writeln;
for i := 1 to ZEILENMAX do
begin
for j := 1 to SPALTENMAX do
write (A[i, j]:5);
writeln (B[i]:10)
end;
writeln;
for j:= 1 to SPALTENMAX do
write (C[j]:5);
writeln
end.
Let us transcript the following piece of code:
const
ZEILENMAX = 3;
type
tZeile = 1..ZEILENMAX;
tMatrix = array[tZeile] of integer;
var
A : tMatrix;
it is equal to:
type
tZeile = 1..3;
tMatrix = array[tZeile] of integer;
var
A : tMatrix;
and this is similar to:
type
tMatrix = array[1..3] of integer;
var
A : tMatrix;
Members of array A can be accessed by a single index:
A[1] := 0; but not A[1,1] := 1 or alike.

Recursive permutation

So I'm trying to permute all possible n digit long numbers out of x long array/set of elements. I've come up with a code that does that, however the digits are the same, how do I prevent that from happening. Here's my come(Pascal):
program Noname10;
var stop : boolean;
A : array[1..100] of integer;
function check( n : integer ) : boolean;
begin
if n = 343 // sets the limit when to stop.
then check := true
else check := false;
end;
procedure permute(p,result : integer);
var i : integer;
begin
if not stop
then if p = 0 then
begin
WriteLn(result);
if check(result)
then stop := true
end
else for i := 1 to 9 do
begin
permute(p - 1, 10*result+i);
end;
end;
begin
stop := false;
permute(3,0);
readln;
end.
Here is the code in Prolog
permutate(As,[B|Cs]) :- select(B, As, Bs), permutate(Bs, Cs).
select(A, [A|As], As).
select(A, [B|Bs], [B|Cs]) :- select(A, Bs, Cs).
?- permutate([a,b,c], P).
Pascal is much harder.
Here is an usefull algorithm, you might want to use. But it is not tested, so you have to debug it yourself. So you have to know how the algorithm works.
The Bell Permutation algorithm: http://programminggeeks.com/bell-algorithm-for-permutation/
procedure permutate(var numbers: array [1..100] of integer; size: integer;
var pos, dir: integer)
begin
if pos >= size then
begin
dir = -1 * dir;
swap(numbers, 1, 2);
end
else if pos < 1 then
begin
dir = -1 * dir;
swap(numbers, size-1, size);
end
else
begin
swap(numbers, pos, pos+1);
end;
pos = pos + dir;
end;
begin
var a, b: integer;
a = 1; b = 1;
while true do
begin
permutate(A, 5, a, b);
printArray(A, 5);
end;
end.

Case statement with logic operators (< > =, etc) in Pascal

I'm having trouble making this work. Apparently, i can't use > or < in the case sentence, is there a workaround for this? Thanks!
case num of
0:
begin
cont_0 := cont_0 + 1;
end;
> 0:
begin
cont_pos := cont_pos + 1;
sum_pos := sum_pos + num;
end;
< 0:
begin
sum_neg := sum_neg + num;
end;
else;
end;
case Sign(num) of
-1: ...
0: ...
1: ...
end;
More readable than if ... else if ... else? You decide.
Don't use case then, why not use if?
if num = 0 then
cont_0 := cont_0 + 1;
if num > 0 then
BEGIN
cont_pos := cont_pos + 1;
sum_pos := sum_pos + num;
END
if num < 0 then
sum_neg := sum_neg + num;

Resources