[Info-vax] ChatGPT solved it for me, again...

Arne Vajhøj arne at vajhoej.dk
Fri Feb 17 10:00:38 EST 2023


On 2/17/2023 9:54 AM, Slo wrote:
> Let's see how well it survives the round trip:
> Me: Convert this C code to Pascal
> ==================================
> Here's the Pascal equivalent of the provided C code:
> program power_supply_compatibility;
> uses sysutils;
> var
>    sys1, sys2: string;
>    watt1, watt2: integer;
> function get_wattage(sys: string): integer;
> begin
>    Randomize;
>    get_wattage := (Random(32767) mod 20) * 50 + 200;
> end;
> begin
>    WriteLn('Utility to check power supply compatibility.');
>    Write('Enter name of system #1: ');
>    ReadLn(sys1);
>    Write('Enter name of system #2: ');
>    ReadLn(sys2);
>    watt1 := get_wattage(sys1);
>    watt2 := get_wattage(sys2);
>    if watt1 = watt2 then
>      WriteLn(sys1, ' and ', sys2, ' can use the same power supply (', watt1, ' watt)')
>    else
>      WriteLn(sys1, ' and ', sys2, ' cannot use the same power supply as they use ', watt1, ' and ', watt2, ' watt');
> end.
> 
> Notes:
> The main differences between the C code and the Pascal code are:
>      In Pascal, the uses keyword is used to import the sysutils unit, which includes the Randomize function.
>      In Pascal, the Randomize function is used to initialize the random number generator.
>      In Pascal, the ReadLn function is used to read input from the user.
>      In Pascal, the WriteLn procedure is used to output text to the user.
>      In Pascal, the mod operator is used to compute the remainder of a division.
> ==================================

The builds with Delphi and FPC (but not with VMS Pascal).

But it managed to sneak in a logical bug.

Randomize should be called once and not in get_wattage.

Arne




More information about the Info-vax mailing list