Recent

Author Topic: pre and postcondition  (Read 2201 times)

chickpea

  • Newbie
  • Posts: 3
pre and postcondition
« on: February 20, 2018, 12:22:50 pm »
Hi there,

absolute beginner here.

Got this piece of code and have to phrase pre and postcondition as well as commenting what the program does.

As a precondition I got a > 0.

If I enter test value a = 7 the program calculates b = 111, for a = 10 b is 1010.

I can't figure out what the program does so any help would be appreciated.

Thanks and best




Code: Pascal  [Select][+][-]
  1. program Whathappens (input, output);
  2.  
  3.   var
  4.   a,b,c: integer;
  5.  
  6. begin
  7.   b := 0;
  8.   c := 1;
  9.   writeln ('Bitte a eingeben');
  10.   readln(a);
  11.   while a>0 do
  12.   begin
  13.   b := b + c*(a mod 10);
  14.   a := a div 2;
  15.   c := 10 * c;
  16.   end;
  17.   writeln(b);
  18. end.
« Last Edit: February 20, 2018, 03:36:14 pm by Martin_fr »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: pre and postcondition
« Reply #1 on: February 20, 2018, 01:43:55 pm »
If you do not know how to use built in debugger, then you should become one. Try to imagine that you are the computer and that you need to execute this program line by line. Iterate through the while loop by hand and write down every new variable assignment that you calculate in your head. Then look at the final output. If you have ever heard of binary system you will understand what the output shows.

EDIT: To understand each step you need to know what MOD and DIV do. You should learn how to find that info your self, but this time I did it for you:
http://wiki.freepascal.org/Mod
http://wiki.freepascal.org/Div

The same goes for WriteLn and ReadLn:
https://www.freepascal.org/docs-html/rtl/system/readln.html
https://www.freepascal.org/docs-html/rtl/system/writeln.html
« Last Edit: February 20, 2018, 01:50:01 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: pre and postcondition
« Reply #2 on: February 20, 2018, 03:37:37 pm »
I took the liberty to edit your post, and insert [ code ] tags.

Use the button with the # symbol to highlight your code in future.
You also may want to indent it...

 

TinyPortal © 2005-2018