EMPIRICAL DATA: Generating data with a Free Pascal program in Do the following o
ID: 3819498 • Letter: E
Question
EMPIRICAL DATA: Generating data with a Free Pascal program
in Do the following on the code below in the Pascal languageFree compiler :
- rewrite the same code but from your idea and same output
- Rephrase comments in your own language
------------------------------------------------------------------------
program Project1;
{* Declare an array to process intermediatory spins such as 100 *}
type
internal_storage = array[0..100] of Integer;
var
spinSize, ctrThirteen : Qword;
twiceCtr, twiceAverage : Real;
thriceCtr, thriceAverage, thriceTmp : Real;
spins : internal_storage;
ctr, index, thirteenFound : Integer;
ctrOdd, maxOdd, ctrEven, maxEven : Integer;
{* -----------------------------------------------------------------------------
Function to prompt the user for the size of simulation i.e
Number of spins to conduct for the current simulation.
----------------------------------------------------------------------------*}
Function GetSize : Qword;
var
aNum : Qword;
begin
Write ('How many times would you like to spin? (enter 0 to Stop): ');
readln(aNum);
GetSize := aNum;
end;
{* =========================== End of Function ===============================*}
{* -----------------------------------------------------------------------------
Function to count the occurrences of a number twice in a row, and return the
count to the caller. The function takes array of intermediatory spins as
Parameter
----------------------------------------------------------------------------*}
Function doTwiceInRow( data : internal_storage; tIndex : Qword ) : Real;
var
idx : Integer;
cnt : Integer;
tSpins: Real;
tSpinsTotal : Real;
tIdx : Qword;
begin
cnt := 0;
tSpins := 0;
tSpinsTotal := 0;
tIdx := tIndex;
for idx := 0 to 99 do
begin
tSpins := tSpins + 1;
tIdx := tIdx + 1;
if data[idx] = data[idx+1] then
begin
cnt := cnt + 1;
tSpinsTotal := tSpinsTotal + tSpins;
tSpins := 0;
Writeln('Number ', data[idx] , ' Appeared Twice at Spin No. ', tIdx);
end;
end;
if cnt <> 0 then
begin
doTwiceInRow := tSpinsTotal / cnt;
end
else doTwiceInRow := 0;
end;
{* =========================== End of Function ===============================*}
{* -----------------------------------------------------------------------------
Function to count the occurrences of a number thrice in a row, and return the
count to the caller. The function takes array of intermediatory spins as
Parameter
----------------------------------------------------------------------------*}
Function doThriceInRow( data : internal_storage; tIndex : Qword ) : Real;
var
idx : Integer;
cnt : Integer;
tSpins: Real;
tSpinsTotal : Real;
tIdx : Qword;
begin
cnt := 0;
tSpins := 0;
tSpinsTotal := 0;
tIdx := tIndex;
for idx := 0 to 98 do
begin
tSpins := tSpins + 1;
tIdx := tIdx + 1;
{* Matching 2 in a row *}
if data[idx] = data[idx+1] then
begin
{* Matching 3 in a row *}
if data[idx] = data[idx+2] then
begin
cnt := cnt + 1;
tSpinsTotal := tSpinsTotal + tSpins;
tSpins := 0;
Writeln('Number ', data[idx] , ' Appeared Thrice at Spin No. ', tIdx);
end;
end;
end;
if cnt <> 0 then
begin
doThriceInRow := tSpinsTotal / cnt;
end
else doThriceInRow := 0;
end;
{* =========================== End of Function ===============================*}
begin
spinSize := 1;
{* Top Level Loop to Interact and ask for the size of simulation *}
while spinSize <> 0 do
begin
Randomize; {* Random Seed *}
{* Initialize variables used for various house keeping tasks *}
spins[0] := 38; {* arbitrary number *}
index := 0;
twiceCtr := 0;
twiceAverage := 0;
thriceCtr := 0;
thriceAverage := 0;
ctrThirteen := 0;
thirteenFound := 0;
ctrOdd := 0;
ctrEven := 0;
maxOdd := 0;
maxEven := 0;
spinSize := GetSize();
if spinSize = 0 then break;
{* Loop till the end of Simulation *}
while index <= spinSize do
begin
{* Loop to fill array with intermediatory data to process *}
for ctr := 1 to 100 do
begin
{* Increase overall counter *}
index := index + 1;
{* Check if we still have spins to roll and store in array *}
if index <= spinSize then
begin
spins[ctr] := Random(37);
{* Measure Long Odd and Even Run *}
{* Ignore consider 0 *}
if spins[ctr] <> 0 then
begin
{* If we found an even number *}
if (spins[ctr] mod 2) = 0 then
begin
ctrEven := ctrEven + 1;
If (spins[ctr-1] mod 2) <> 0 then
begin
if ctrOdd > maxOdd then
begin
maxOdd := ctrOdd;
ctrOdd := 0;
end
end
end
else if (spins[ctr] mod 2) <> 0 then
begin
ctrOdd := ctrOdd + 1;
If (spins[ctr-1] mod 2) = 0 then
begin
if ctrEven > maxEven then
begin
maxEven := ctrEven;
ctrOdd := 0;
end
end
end
end
else {* zero appeared - reset both counters *}
begin
ctrOdd := 0;
ctrEven := 0;
end;
{* Check if 13 appeared twice *}
if thirteenFound = 0 then
begin
if spins[ctr-0] = 13 then
begin
if spins[ctr] = 13 then
begin
ctrThirteen := index;
thirteenFound := 1;
end;
end;
end;
end;
end;
{* Compute Average of twice appearance for current intermediatory run *}
twiceAverage := twiceAverage + doTwiceInRow(spins, index);
twiceCtr := twiceCtr + 1;
{* Compute Average of thrice appearance for current intermediatory run *}
thriceTmp := doThriceInRow(spins, index);
thriceAverage := thriceAverage + thriceTmp;
if thriceTmp > 0 then
begin
thriceCtr := thriceCtr + 1;
end
else thriceAverage := thriceAverage + 100;
spins[0] := spins[100]; {* Result of Last Spin *}
end;
{* Compute Average of twice appearance for current Overall run *}
twiceAverage := twiceAverage / twiceCtr;
{* Compute Average of thrice appearance for current Overall run *}
if thriceCtr > 0 then
begin
thriceAverage := thriceAverage / thriceCtr;
end
else
thriceAverage := 0;
Writeln();
Writeln();
Writeln('-------------------------------------------------------------');
Writeln('Statistics of Simulation');
Writeln('It takes ', twiceAverage:6:2 ,' Spins on Average for a number to appear Twice');
Writeln('It takes ', thriceAverage:6:2 ,' Spins on Average for a number to appear Thrice');
Writeln('It takes ', ctrThirteen , ' Spins for 13 to appear twice');
WriteLn('Long Run of Even Numbers ', maxEven);
Writeln('Longest Run of Odd Numbers ', maxOdd);
Writeln('-------------------------------------------------------------');
Writeln();
Writeln();
end;
writeln('Press Enter To Exit........');
Readln;
end.
Explanation / Answer
function Random(
l: LongInt
):LongInt;
function Random(
l: Int64
):Int64;
function Random: extended;
Randomize; { This way we generate a new sequence every time
the program is run}
Count:=0;
For i:=1 to 1000 do
If Random>0.5 then inc(Count);
Writeln ('Generated ',Count,' numbers > 0.5');
Writeln ('out of 1000 generated numbers.');
count:=0;
For i:=1 to 5 do
begin
write ('Guess a number between 1 and 5 : ');
readln(Guess);
If Guess=Random(5)+1 then inc(count);
end;
Writeln ('You guessed ',Count,' out of 5 correct.');
end.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.