Dear Sir,

I have got the answer of the following question but I do not understand why it has been done in this way:

Write a program that asks the user for Heads or Tails, then simulates a toss of a coin and then tells the user whether they have guessed correctly.

Question:-

First, draw an activity chart that matches the pseudocode provided below.
Convert the pseudocode to Delphi:

DISPLAY a title for the program
Issue the statement that stops your random numbers always coming out as zero
INPUT guess
Generate a random number less than 2 call this computer
IF computer = 0 THEN
SET coin = h
ELSE
SET coin = t
OUTPUT Computer tossed a Tail
ENDIF

IF coin = guess THEN
OUTPUT You won
ELSE
OUTPUT You lost
ENDIF

Delay the program so that you can see the results

``````program HeadTaili;

{\$APPTYPE CONSOLE}

uses
SysUtils;

var
guess: integer;
flip, throw: char;
begin
randomize;
writeln('Welcome to the guessing game');
writeln('Please guesses the outcome of a coin');
guess:=random(2);
if (guess=0) then
begin
throw:='h'
end
else
begin
throw:='t'
end;
if (flip=throw) then
begin
writeln('You win');
end
else
begin
writeln('You lose');
end;
end.``````
2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by FlamingClaw
``````{
1,DISPLAY a title for the program
2,Issue the statement that stops your random numbers always coming out as zero
4,INPUT guess
5,Generate a random number less than 2 call this computer
6,IF computer = 0 THEN
7,SET coin = h
9,ELSE
10,SET coin = t
11,OUTPUT Computer tossed a Tail
12,ENDIF
13,IF coin = guess THEN
14,OUTPUT You won
15,ELSE
16,OUTPUT You lost
17,ENDIF
18,Delay the program so that you can see the results
}

{\$APPTYPE CONSOLE}

Uses
SysUtils;

Var guess,     {choice of the user,one character}
coin:Char;{the coin,cause it will contain a character 'H' or 'T'}
computer:Integer;{cause this var stores a number between 0 or 1}

Begin {the main program}
{
1,DISPLAY a title for the program
}
WriteLn('Welcome to the Head or Tail Game!');
WriteLn('---------------------------------');
{
2,Issue the statement that stops your random
numbers always coming out as zero
}
Randomize;
{
}
Write('Please select h or t : ');
{
4,INPUT guess
}
{
5,Generate a random number less than 2 call this computer
}
computer:=Random(2);
{
6,IF computer = 0 THEN
}
If (computer = 0) Then Begin
{
7,SET coin = h
}
coin:='h';
{
}
End {first part of IF}
{
9,ELSE
}
Else Begin
{
10,SET coin = t
}
coin:='t';
{
11,OUTPUT Computer tossed a Tail
}
WriteLn('Computer tossed a Tail!');
{
12,ENDIF
}
End;
{
13,IF coin = guess THEN
}
If (coin = guess) Then
{
14,OUTPUT You won
}
WriteLn('You won!!')
{
15,ELSE
16,OUTPUT You lost
17,ENDIF
}
Else WriteLn('You lost!!');
{
18,Delay the program so that you can see the results
}