I am trying to run this pl/sql code but apparently it is getting stuck in the loop. I think the logic is correct.
DECLARE
v_num1 NUMBER := 1;
v_num2 NUMBER := 1;
v_total NUMBER := 0;
BEGIN
WHILE v_num2 < 6 LOOP
v_total := v_num1 + v_num2;
DBMS_OUTPUT.PUT_LINE(v_num1 || ' + ' || v_num2 || ' = ' || v_total);
IF v_num2 = 5 THEN
v_num2 := 1;
v_num1 := v_num1 + 1;
ELSE
v_num2 := v_num2 + 1;
END IF;
END LOOP;
END;
Well, you keep resetting v_num2 to 1 every time it gets to 5, so v_num2 will always be less than 6 and the loop will run forever.
IF v_num2 = 5 THEN
v_num2 := 1;
v_num1 := v_num1 + 1;
ELSE
v_num2 := v_num2 + 1;
END IF;
You're looping WHILE v_num2 < 6 LOOP
but when v_num2 is 5, then you assign it 1
v_num2 := 1;
so it's never equalling 6 and hence will loop forever.
Related
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.
I have written down a code in pl/sql but output is not showing
having trouble with this code.
declare
num number(20):= 0;
val number(10):= 5;
temp number(10):= 0;
function factorial (n1 in number)
return number
is
fact number(10):= 1;
begin
temp:= n1;
loop
if fact <=0 then
exit;
else
fact := fact *temp;
temp:=temp-1;
end if;
end loop;
return fact;
end;
begin
num:= factorial(val);
dbms_output.put_line('Factorial of ' ||val||' is '||num);
end;
Output:- Factorial of 5 is 0
The line:
if fact <=0 then
should be:
if temp <=0 then
You don't really need the 'else' part, because the 'if' exits the loop anyway; so you could do:
loop
if temp <=0 then
exit;
end if;
fact := fact *temp;
temp:=temp-1;
end loop;
Or avoid the explicit check by using a while loop:
while temp > 0 loop
fact := fact *temp;
temp:=temp-1;
end loop;
... etc.
db<>fiddle
You are checking fact <= 0 rather than checking temp <= 0 and you should also declare the temp variable locally to the function:
declare
val number(10):= 5;
function factorial (n1 in number)
return number
is
fact number(10) := 1;
temp number(10) := n1;
begin
loop
if temp <=0 then
exit;
else
fact := fact *temp;
temp:=temp-1;
end if;
end loop;
return fact;
end;
begin
dbms_output.put_line('Factorial of ' ||val||' is '|| factorial(val) );
end;
/
Or, more simply:
declare
val number(10):= 5;
function factorial (n1 in number)
return number
is
fact number(10) := 1;
BEGIN
FOR temp IN 2 .. n1
LOOP
fact := fact *temp;
END LOOP;
RETURN fact;
END;
begin
dbms_output.put_line('Factorial of ' ||val||' is '|| factorial(val) );
end;
/
db<>fiddle here
I am writing a program that rolls two dice and uses an array of counters to show how many times each total is shown. My code compiles fine and does run but for some reason the first two counters are never increased, and seems to print in numeric ascending order - which to me sounds like a problem with my random number function. But I cannot quite see where I have gone wrong. It also never iterates the correct amount of times, about half in fact. Here is my code.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Numerics.Discrete_Random;
-- procedure main - begins program execution
procedure main is
dice1, dice2, diceTotal : Integer;
type Count_Array is array(1 .. 11) of Integer;
intArray : Count_Array;
-- function returnRand - produces a random number and returns it
function returnRand return Integer is
type magicNumber is new Integer range 2 .. 12;
package Rand_Number is new Ada.Numerics.Discrete_Random(magicNumber);
use Rand_Number;
theNumber : magicNumber;
g : Generator;
begin
Reset(g);
theNumber := Random(g);
return Integer(theNumber);
end returnRand;
-- procedure rollDice - rolls two dice 36000 times and tallys the totals
procedure rollDice(dice1 : out Integer; dice2 : out Integer;
diceTotal : out Integer; intArray : in out Count_Array) is
begin
for I in 1 .. 36000 loop
dice1 := returnRand;
dice2 := returnRand;
diceTotal := dice1 + dice2;
if diceTotal = 2 then
intArray(1) := intArray(1) + 1;
elsif diceTotal = 3 then
intArray(2) := intArray(2) + 1;
elsif diceTotal = 4 then
intArray(3) := intArray(3) + 1;
elsif diceTotal = 5 then
intArray(4) := intArray(4) + 1;
elsif diceTotal = 6 then
intArray(5) := intArray(5) + 1;
elsif diceTotal = 7 then
intArray(6) := intArray(6) + 1;
elsif diceTotal = 8 then
intArray(7) := intArray(7) + 1;
elsif diceTotal = 9 then
intArray(8) := intArray(8) + 1;
elsif diceTotal = 10 then
intArray(9) := intArray(9) + 1;
elsif diceTotal = 11 then
intArray(10) := intArray(10) + 1;
elsif diceTotal = 12 then
intArray(11) := intArray(11) + 1;
end if;
end loop;
end rollDice;
-- procedure printResults - prints out the totals of each dice throw
procedure printResults(intArray : in Count_Array) is
begin
Put_Line("Dice Total Tally");
for I in Count_Array'Range loop
-- Set_Col(2);
-- Put(integer'image(I));
-- Set_Col(23);
Put(integer'image(intArray(I)));
New_Line;
end loop;
end printResults;
begin
New_Line;
intArray := (0,0,0,0,0,0,0,0,0,0,0);
rollDice(dice1, dice2, diceTotal, intArray);
printResults(intArray);
New_Line;
end main;
You roll two D11s (range 2..12), not D6s, so when you add them together, you get a range of 4..24 instead of the expected 2..12.
If you used the type system to model the problem, this would have been detected:
type Count_Array is array(2 .. 12) of Integer;
and then your if..elsif construct could be just:
intArray(diceTotal) := intArray(diceTotal) + 1;
Change your magicNumber to a 1..6 range to roll a D6 instead.
Also, there's no need to reset the Generator every time you roll a die. Once is sufficient.
Edit:
You could also go much further with the type system, and do something like this:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
procedure Main is
Number_Of_Dice : constant := 2;
type Die_Range is new Positive range 1 .. 6;
subtype Sums_Range is Die_Range'Base
range Number_Of_Dice*Die_Range'First..Number_Of_Dice*Die_Range'Last;
type Count_Array is array(Sums_Range) of Natural;
package Random_Die is new Ada.Numerics.Discrete_Random(Die_Range);
Die : Random_Die.Generator;
function Roll(Die : Random_Die.Generator) return Die_Range
renames Random_Die.Random;
procedure Roll_Dice(Sums : in out Count_Array) is
Dice_Total : Sums_Range'Base;
begin
for I in 1 .. 36_000 loop
Dice_Total := Roll(Die);
for I in 1..Number_Of_Dice-1 loop
Dice_Total := Dice_Total + Roll(Die);
end loop;
Sums(Dice_Total) := Sums(Dice_Total) + 1;
end loop;
end Roll_Dice;
procedure Print_Results(Sums : in Count_Array) is
begin
Put_Line("Dice Total Tally");
for I in Sums'Range loop
Put(Sums(I)'Image);
New_Line;
end loop;
end Print_Results;
Sums : Count_Array;
begin
Random_Die.Reset(Die);
New_Line;
Sums := (others => 0);
Roll_Dice(Sums);
Print_Results(Sums);
New_Line;
end Main;
Notice how easy this code will be to maintain when the requirements change, for example the number of dice cast per roll, or the type of dice (D4, D6, D8, D12, etc)
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
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;