Recent

Author Topic: Pascal  (Read 1302 times)

Dato39

  • Newbie
  • Posts: 1
Pascal
« on: October 25, 2020, 08:58:15 pm »
Recently I started my adventure with Pascal.  I need help, I have to write a program that gives 50% of the correct multiplication results and the rest of the cases a random result.
I'm counting on your help.  Greetings  ;)

MarkMLl

  • Hero Member
  • *****
  • Posts: 1447
Re: Pascal
« Reply #1 on: October 25, 2020, 09:28:55 pm »
How on Earth do you expect us to help if you don't show us your code?

Also it's usual to tell us what version of FPC/Lazarus you're running, what OS, and so on. There's plenty of FAQs around on how to ask questions in a way that's likely to get you a helpful answer (which, believe it or not, this is intended to be :-)

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

wildfire

  • Jr. Member
  • **
  • Posts: 75
Re: Pascal
« Reply #2 on: October 25, 2020, 09:32:43 pm »
Start by looking up randomize, random and looping structures.

That's the best information I can supply given the *very* limited specs. However doing it yourself will greatly help your learning experience.
Windows 10 64bit version 1809
Laz 2.0.0 FPC 3.0.4

dbannon

  • Hero Member
  • *****
  • Posts: 1211
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Pascal
« Reply #3 on: October 26, 2020, 07:00:57 am »
Err, just when is this assignment due in ?

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

inko

  • New member
  • *
  • Posts: 8
Re: Pascal
« Reply #4 on: October 26, 2020, 07:58:15 am »
You might try copypasta the instructions of your assignment followed by the code you have tried so far and your questions about particular points of code, errors, or problems.

440bx

  • Hero Member
  • *****
  • Posts: 2047
Re: Pascal
« Reply #5 on: October 26, 2020, 06:59:38 pm »
You might try copypasta ...
I'll contribute some spaghetti or lasagna to that effort.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Paolo

  • Jr. Member
  • **
  • Posts: 61
Re: Pascal
« Reply #6 on: October 26, 2020, 07:16:53 pm »
Quote
I'll contribute some spaghetti or lasagna to that effort.
I would like to add also maccheroni and cannelloni  :D :D :D

Thaddy

  • Hero Member
  • *****
  • Posts: 10572
Re: Pascal
« Reply #7 on: October 26, 2020, 08:14:01 pm »

winni

  • Hero Member
  • *****
  • Posts: 1992
Re: Pascal
« Reply #8 on: October 26, 2020, 09:02:14 pm »
Hi!

What the most people outside Italy don't know:

Spaghettis are the interior of  maccheroni.

Winni

Roland57

  • Full Member
  • ***
  • Posts: 108
Re: Pascal
« Reply #9 on: October 26, 2020, 09:15:48 pm »
Very interesting discussion and very good title.  :)

Kays

  • Full Member
  • ***
  • Posts: 229
  • Whasup!?
    • KaiBurghardt.de
Re: Pascal
« Reply #10 on: October 26, 2020, 10:06:53 pm »
Speaking of specs:
[…] the rest of the cases a random result. […]
Do you have to ensure the “random” result is not accidentally the correct result? I mean, there is a chance.
Yours Sincerely
Kai Burghardt

Bart

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Pascal
« Reply #11 on: October 26, 2020, 11:55:26 pm »
I simply could not resist:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils, Classes;
  3.  
  4. type
  5.  
  6.   { TPseudoRandomMultiplicator }
  7.  
  8.   TPseudoRandomMultiplicator = class
  9.   strict private
  10.     FFirstNumber: integer;
  11.     FSecondNumber: integer;
  12.     FIncorrectnessFraction: Double;
  13.     FIncorrectnessMargin: Integer;
  14.   private
  15.     function CalculateResult: Int64;
  16.     function GetOffset: Integer;
  17.     function GetFirstNumber: Integer;
  18.     function GetIncorrectnessFraction: Double;
  19.     function GetIncorrectnessMargin: Integer;
  20.     function GetMultiplicationResult: Int64;
  21.     function GetSecondNumber: Integer;
  22.     procedure SetFirstNumber(AValue: Integer);
  23.     procedure SetIncorrectnessFraction(AValue: Double);
  24.     procedure SetIncorrectnessMargin(AValue: Integer);
  25.     procedure SetSecondNumber(AValue: Integer);
  26.   public
  27.     constructor Create;
  28.     property FirstNumber: Integer read GetFirstNumber write SetFirstNumber;
  29.     property SecondNumber: Integer read GetSecondNumber write SetSecondNumber;
  30.     property IncorrectnessFraction: Double read GetIncorrectnessFraction write SetIncorrectnessFraction;
  31.     property IncorrectnessMargin: Integer read GetIncorrectnessMargin write SetIncorrectnessMargin;
  32.     property MultiplicationResult: Int64 read GetMultiplicationResult;
  33.   end;
  34.  
  35. { TPseudoRandomMultiplicator }
  36.  
  37. function TPseudoRandomMultiplicator.CalculateResult: Int64;
  38. begin
  39.   Result := Int64(FirstNumber) * Int64(SecondNumber);
  40.   if (Random < IncorrectnessFraction) then
  41.   begin
  42.     Result := Result + GetOffset;
  43.   end;
  44. end;
  45.  
  46. function TPseudoRandomMultiplicator.GetOffset: Integer;
  47. begin
  48.   Result := Random(IncorrectnessMargin) + 1;
  49.   if (Random < 0.5) then
  50.     Result := -Result;
  51. end;
  52.  
  53. function TPseudoRandomMultiplicator.GetFirstNumber: Integer;
  54. begin
  55.   Result := FFirstNumber;
  56. end;
  57.  
  58. function TPseudoRandomMultiplicator.GetIncorrectnessFraction: Double;
  59. begin
  60.   Result :=  FIncorrectnessFraction;
  61. end;
  62.  
  63. function TPseudoRandomMultiplicator.GetIncorrectnessMargin: Integer;
  64. begin
  65.   Result := FIncorrectnessMargin;
  66. end;
  67.  
  68. function TPseudoRandomMultiplicator.GetMultiplicationResult: Int64;
  69. begin
  70.   Result := CalculateResult;
  71. end;
  72.  
  73. function TPseudoRandomMultiplicator.GetSecondNumber: Integer;
  74. begin
  75.   Result := FSecondNumber;
  76. end;
  77.  
  78. procedure TPseudoRandomMultiplicator.SetFirstNumber(AValue: Integer);
  79. begin
  80.   if (FirstNumber <> AValue) then
  81.     FFirstNumber := AValue;
  82. end;
  83.  
  84. procedure TPseudoRandomMultiplicator.SetIncorrectnessFraction(AValue: Double);
  85. begin
  86.   if (IncorrectnessFraction <> AValue) then
  87.     IncorrectnessFraction := AValue;
  88. end;
  89.  
  90. procedure TPseudoRandomMultiplicator.SetIncorrectnessMargin(AValue: Integer);
  91. begin
  92.   if (IncorrectnessMargin <> AValue) then
  93.     FIncorrectnessMargin := AValue;
  94. end;
  95.  
  96. procedure TPseudoRandomMultiplicator.SetSecondNumber(AValue: Integer);
  97. begin
  98.   if (SecondNumber <> AValue) then
  99.     FSecondNumber := AValue;
  100. end;
  101.  
  102. constructor TPseudoRandomMultiplicator.Create;
  103. begin
  104.   FFirstNumber := 0;
  105.   FSecondNumber := 0;
  106.   FIncorrectnessFraction := 0.5;
  107.   FIncorrectnessMargin := 50;
  108. end;
  109.  
  110. var
  111.   PRM: TPseudoRandomMultiplicator;
  112.   N, Code: Integer;
  113.   S: String;
  114.  
  115. begin
  116.   Randomize;
  117.   repeat
  118.     PRM := TPseudoRandomMultiplicator.Create;
  119.     writeln('Enter a zero for both numbers to quit');
  120.     repeat
  121.       write('Enter first number: ');
  122.       readln(S);
  123.       Val(S, N, Code);
  124.       if (Code <> 0) then
  125.         writeln('Invalid number: "',S,'", please try again.');
  126.     until (Code = 0);
  127.     PRM.FirstNumber := N;
  128.     repeat
  129.       write('Enter second number: ');
  130.       readln(S);
  131.       Val(S, N, Code);
  132.       if (Code <> 0) then
  133.         writeln('Invalid number: "',S,'", please try again.');
  134.     until (Code = 0);
  135.     PRM.SecondNumber := N;
  136.     if (PRM.FirstNumber <> 0) and (PRM.SecondNumber <> 0) then
  137.     begin
  138.       writeln('Multiplying ',PRM.FirstNumber,' by ',PRM.SecondNumber,' gives ',PRM.MultiplicationResult);
  139.       writeln;
  140.     end;
  141.   until (PRM.FirstNumber = 0) and (PRM.SecondNumber = 0);
  142. end.

I'ld love to see him submit that  O:-)

Bart

440bx

  • Hero Member
  • *****
  • Posts: 2047
Re: Pascal
« Reply #12 on: October 27, 2020, 01:57:47 am »
I'ld love to see him submit that  O:-)

Bart
He'd probably get a failing grade for having someone do his homework for him then, he blame you for it.  ;)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Pascal
« Reply #13 on: October 27, 2020, 10:56:57 am »
It needs a refinement, so that the error marging depends on the expected result.
If 6*3 gives you 52, it's to obvious the answer is wrong.
Also if the right answer is odd, the wrong answer should be odd as well.
And it might crash if you enter MaxInt for both values?

So, Dato39 still has lots of improvements to add, so he can claim it's his own code.

B.t.w. the code I posted is released under the LGPL 2.0 with linking exception (like Lazarus's LCL).
That means that Dato39 must publish his alterations if he submits his code  :)

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Pascal
« Reply #14 on: October 27, 2020, 11:45:40 am »
Improved version.
  • Odd or Even is preserved
  • Sign is preserved
  • Zero as a result will not happen if none of the operants is zero
  • Overflow detection
  • Offsett is sort of relative to correct answer
  • You can enter "MaxInt" or "LowInt"

Code: Pascal  [Select][+][-]
  1. {$Mode ObjFpc}
  2. {$H-} //no longstrings needed for this job
  3. uses
  4.   SysUtils, Classes;
  5.  
  6. type
  7.  
  8.   { TPseudoRandomMultiplicator }
  9.  
  10.   TPseudoRandomMultiplicator = class
  11.   strict private
  12.     FFirstNumber: integer;
  13.     FSecondNumber: integer;
  14.     FIncorrectnessFraction: Double;
  15.     FIncorrectnessMargin: Integer;
  16.   private
  17.     function CalculateResult: Int64;
  18.     function CheckAdditionOverflow(ALeft, ARight: Int64): Boolean;
  19.     function GetOffset(ARelativeTo: Int64): Integer;
  20.     function GetFirstNumber: Integer;
  21.     function GetIncorrectnessFraction: Double;
  22.     function GetIncorrectnessMargin: Integer;
  23.     function GetMultiplicationResult: Int64;
  24.     function GetSecondNumber: Integer;
  25.     procedure SetFirstNumber(AValue: Integer);
  26.     procedure SetIncorrectnessFraction(AValue: Double);
  27.     procedure SetIncorrectnessMargin(AValue: Integer);
  28.     procedure SetSecondNumber(AValue: Integer);
  29.   public
  30.     constructor Create;
  31.     property FirstNumber: Integer read GetFirstNumber write SetFirstNumber;
  32.     property SecondNumber: Integer read GetSecondNumber write SetSecondNumber;
  33.     property IncorrectnessFraction: Double read GetIncorrectnessFraction write SetIncorrectnessFraction;
  34.     property IncorrectnessMargin: Integer read GetIncorrectnessMargin write SetIncorrectnessMargin;
  35.     property MultiplicationResult: Int64 read GetMultiplicationResult;
  36.   end;
  37.  
  38. { TPseudoRandomMultiplicator }
  39.  
  40. function TPseudoRandomMultiplicator.CalculateResult: Int64;
  41. var
  42.   Offset: Integer;
  43.   TempResult: Int64;
  44. begin
  45.   Result := Int64(FirstNumber) * Int64(SecondNumber);
  46.   if (Random < IncorrectnessFraction) then
  47.   begin
  48.     Offset := GetOffset(Result);
  49.     if CheckAdditionOverflow(Result, Offset) then
  50.       Offset := -Offset;
  51.     TempResult := Result + Offset;
  52.     // sign should always be correct, so errors aren't that obvious
  53.     if ((TempResult < 0) and (Result > 0)) or
  54.        ((TempResult > 0) and (Result < 0)) then
  55.        TempResult := -TempResult;
  56.     // if none of the operands is zero, the answer cannot be zero
  57.     if (TempResult = 0) and (Result <> 0) then
  58.     begin
  59.       if (Result > 0) then
  60.       begin
  61.         repeat
  62.           Inc(TempResult)
  63.         until (TempResult <> Result);
  64.       end
  65.       else
  66.       begin
  67.         repeat
  68.           Dec(TempResult)
  69.         until (TempResult <> Result);
  70.       end;
  71.     end;
  72.     Result := TempResult;
  73.   end;
  74. end;
  75.  
  76. function TPseudoRandomMultiplicator.CheckAdditionOverflow(ALeft, ARight: Int64): Boolean;
  77. begin
  78.   if ((ALeft >= 0) and (ARight <= 0)) or
  79.      ((ALeft <= 0) and (ARight >= 0)) then
  80.      Result := False
  81.   else
  82.   begin
  83.     if (ALeft < 0) and (ARight < 0) then
  84.     begin
  85.       Result := (Low(Int64) - ALeft) >= ARight;
  86.     end
  87.     else
  88.     begin
  89.       Result := (High(Int64) - ALeft) >= ARight;
  90.     end;
  91.   end;
  92. end;
  93.  
  94. function TPseudoRandomMultiplicator.GetOffset(ARelativeTo: Int64): Integer;
  95. begin
  96.   if (Abs(ARelativeTo) > Abs(IncorrectnessMargin)) then
  97.     Result := Random(IncorrectnessMargin) + 1
  98.   else
  99.     Result := Random(3) + 1;
  100.   if (Odd(ARelativeTo) and not Odd(Result)) or
  101.      (Odd(Result) and not Odd(ARelativeTo)) then
  102.      Dec(Result);
  103.   if (Result = 0) then
  104.     Inc(Result, 2);
  105.   if (Random < 0.5) then
  106.     Result := -Result;
  107. end;
  108.  
  109. function TPseudoRandomMultiplicator.GetFirstNumber: Integer;
  110. begin
  111.   Result := FFirstNumber;
  112. end;
  113.  
  114. function TPseudoRandomMultiplicator.GetIncorrectnessFraction: Double;
  115. begin
  116.   Result :=  FIncorrectnessFraction;
  117. end;
  118.  
  119. function TPseudoRandomMultiplicator.GetIncorrectnessMargin: Integer;
  120. begin
  121.   Result := FIncorrectnessMargin;
  122. end;
  123.  
  124. function TPseudoRandomMultiplicator.GetMultiplicationResult: Int64;
  125. begin
  126.   Result := CalculateResult;
  127. end;
  128.  
  129. function TPseudoRandomMultiplicator.GetSecondNumber: Integer;
  130. begin
  131.   Result := FSecondNumber;
  132. end;
  133.  
  134. procedure TPseudoRandomMultiplicator.SetFirstNumber(AValue: Integer);
  135. begin
  136.   if (FirstNumber <> AValue) then
  137.     FFirstNumber := AValue;
  138. end;
  139.  
  140. procedure TPseudoRandomMultiplicator.SetIncorrectnessFraction(AValue: Double);
  141. begin
  142.   if (IncorrectnessFraction <> AValue) then
  143.     IncorrectnessFraction := AValue;
  144. end;
  145.  
  146. procedure TPseudoRandomMultiplicator.SetIncorrectnessMargin(AValue: Integer);
  147. begin
  148.   if (IncorrectnessMargin <> AValue) then
  149.     FIncorrectnessMargin := AValue;
  150. end;
  151.  
  152. procedure TPseudoRandomMultiplicator.SetSecondNumber(AValue: Integer);
  153. begin
  154.   if (SecondNumber <> AValue) then
  155.     FSecondNumber := AValue;
  156. end;
  157.  
  158. constructor TPseudoRandomMultiplicator.Create;
  159. begin
  160.   FFirstNumber := 0;
  161.   FSecondNumber := 0;
  162.   FIncorrectnessFraction := 0.5;
  163.   FIncorrectnessMargin := 50;
  164. end;
  165.  
  166.  
  167. function AskNumber(IsFirst: Boolean): Integer;
  168. const
  169.   FirstOrSecond: Array[Boolean] of String = ('second','first');
  170. var
  171.   S: String;
  172.   ErrorCode: Integer;
  173. begin
  174.   repeat
  175.     write('Enter ',FirstOrSecond[IsFirst],' number');
  176.     if IsFirst then
  177.       write (' ');
  178.     write(': ');
  179.     readln(S);
  180.     if (CompareText(S,'MaxInt') = 0) then
  181.     begin
  182.       Result := MaxInt;
  183.       ErrorCode := 0;
  184.     end
  185.     else
  186.     begin
  187.       if (CompareText(S,'LowInt') = 0) then
  188.       begin
  189.         Result := Low(Integer);
  190.         ErrorCode := 0;
  191.       end
  192.       else
  193.       begin
  194.       Val(S, Result, ErrorCode);
  195.       if (ErrorCode <> 0) then
  196.         writeln('Invalid number: "',S,'", please try again.');
  197.       end;
  198.     end;
  199.   until (ErrorCode = 0);
  200. end;
  201.  
  202. var
  203.   PseudoRandomMultiplicator: TPseudoRandomMultiplicator;
  204.  
  205. begin
  206.   Randomize;
  207.   repeat
  208.     try
  209.       PseudoRandomMultiplicator := TPseudoRandomMultiplicator.Create;
  210.       PseudoRandomMultiplicator.FirstNumber := AskNumber(True);
  211.       PseudoRandomMultiplicator.SecondNumber := AskNumber(False);
  212.       if (PseudoRandomMultiplicator.FirstNumber <> 0) and (PseudoRandomMultiplicator.SecondNumber <> 0) then
  213.       begin
  214.         try
  215.         writeln('Multiplying ',PseudoRandomMultiplicator.FirstNumber,' by ',PseudoRandomMultiplicator.SecondNumber,' gives ',PseudoRandomMultiplicator.MultiplicationResult);
  216.         except
  217.           on E: Exception do
  218.           begin
  219.             writeln('There was an error in the multiplication process:');
  220.             writeln(E.ClassName,': ',E.Message);
  221.           end;
  222.         end;
  223.         writeln;
  224.       end;
  225.     finally
  226.       PseudoRandomMultiplicator.Free;
  227.     end;
  228.   until (PseudoRandomMultiplicator.FirstNumber = 0) and (PseudoRandomMultiplicator.SecondNumber = 0);
  229.   writeln('Bye.');
  230. end.

Bart

 

TinyPortal © 2005-2018