Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Solution"

From Lazarus wiki
Jump to navigationJump to search
Line 2: Line 2:
  
 
Here's one way to solve the programming assignment in the previous section.
 
Here's one way to solve the programming assignment in the previous section.
<font color="#000000">  1: </font><font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Author:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Tao</font><font color="#cc0000"> </font><font color="#cc0000">Yue</font>
+
<delphi>
<font color="#000000">  2: </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Date:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">19</font><font color="#cc0000"> </font><font color="#cc0000">June</font><font color="#cc0000"> </font><font color="#cc0000">1997</font>
+
(* Author:   Tao Yue
<font color="#000000">  3: </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Description:</font>
+
  Date:     19 June 1997
<font color="#000000">  4: </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Find</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">sum</font><font color="#cc0000"> </font><font color="#cc0000">and</font><font color="#cc0000"> </font><font color="#cc0000">average</font><font color="#cc0000"> </font><font color="#cc0000">of</font><font color="#cc0000"> </font><font color="#cc0000">five</font><font color="#cc0000"> </font><font color="#cc0000">predefined</font><font color="#cc0000"> </font><font color="#cc0000">numbers</font>
+
  Description:
<font color="#990066">  5: </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Version:</font>
+
      Find the sum and average of five predefined numbers
<font color="#000000">  6: </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">1.0</font><font color="#cc0000"> </font><font color="#cc0000">-</font><font color="#cc0000"> </font><font color="#cc0000">original</font><font color="#cc0000"> </font><font color="#cc0000">version</font>
+
  Version:
<font color="#000000">  7: </font><font color="#cc0000">*)</font>
+
      1.0 - original version
<font color="#000000">  8: </font>
+
*)
<font color="#000000">  9: </font><font color="#006699"><strong>program</strong></font> SumAverage<font color="#000000"><strong>;</strong></font>
+
 
<font color="#990066">  10: </font>
+
program SumAverage;
<font color="#000000">  11: </font><font color="#006699"><strong>const</strong></font>
+
 
<font color="#000000">  12: </font>  NumberOfIntegers <font color="#000000"><strong>=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
+
const
<font color="#000000">  13: </font>
+
  NumberOfIntegers = 5;
<font color="#000000">  14: </font><font color="#006699"><strong>var</strong></font>
+
 
<font color="#990066">  15: </font>  A<font color="#000000"><strong>,</strong></font> B<font color="#000000"><strong>,</strong></font> C<font color="#000000"><strong>,</strong></font> D<font color="#000000"><strong>,</strong></font> E <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
var
<font color="#000000">  16: </font>  Sum <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  A, B, C, D, E : integer;
<font color="#000000">  17: </font>  Average <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font>
+
  Sum : integer;
<font color="#000000">  18: </font>
+
  Average : real;
<font color="#000000">  19: </font><font color="#006699"><strong>begin</strong></font>   <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
 
<font color="#990066">  20: </font>  A <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">45</font><font color="#000000"><strong>;</strong></font>
+
begin    (* Main *)
<font color="#000000">  21: </font>  B <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">7</font><font color="#000000"><strong>;</strong></font>
+
  A := 45;
<font color="#000000">  22: </font>  C <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">68</font><font color="#000000"><strong>;</strong></font>
+
  B := 7;
<font color="#000000">  23: </font>  D <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">2</font><font color="#000000"><strong>;</strong></font>
+
  C := 68;
<font color="#000000">  24: </font>  E <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">34</font><font color="#000000"><strong>;</strong></font>
+
  D := 2;
<font color="#990066">  25: </font>  Sum <font color="#000000"><strong>:=</strong></font> A <font color="#000000"><strong>+</strong></font> B <font color="#000000"><strong>+</strong></font> C <font color="#000000"><strong>+</strong></font> D <font color="#000000"><strong>+</strong></font> E<font color="#000000"><strong>;</strong></font>
+
  E := 34;
<font color="#000000">  26: </font>  Average <font color="#000000"><strong>:=</strong></font> Sum <font color="#000000"><strong>/</strong></font> NumberOfIntegers<font color="#000000"><strong>;</strong></font>
+
  Sum := A + B + C + D + E;
<font color="#000000">  27: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number</font><font color="#ff00cc"> </font><font color="#ff00cc">of</font><font color="#ff00cc"> </font><font color="#ff00cc">integers</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> NumberOfIntegers<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  Average := Sum / NumberOfIntegers;
<font color="#000000">  28: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number1</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> A<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number of integers = ', NumberOfIntegers);
<font color="#000000">  29: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number2</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> B<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number1 = ', A);
<font color="#990066">  30: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number3</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> C<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number2 = ', B);
<font color="#000000">  31: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number4</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> D<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number3 = ', C);
<font color="#000000">  32: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number5</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> E<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number4 = ', D);
<font color="#000000">  33: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Sum</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> Sum<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln ('Number5 = ', E);
<font color="#000000">  34: </font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Average</font><font color="#ff00cc"> </font><font color="#ff00cc">=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> Average<font color="#000000"><strong>)</strong></font>
+
  writeln ('Sum = ', Sum);
<font color="#990066">  35: </font><font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>     <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
  writeln ('Average = ', Average)
 +
end.    (* Main *)  
 +
</delphi>
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"

Revision as of 15:05, 5 January 2010

1Ha - Solution (author: Tao Yue, state: unchanged)

Here's one way to solve the programming assignment in the previous section. <delphi> (* Author: Tao Yue

  Date:      19 June 1997
  Description:
     Find the sum and average of five predefined numbers
  Version:
     1.0 - original version
  • )

program SumAverage;

const

  NumberOfIntegers = 5;

var

  A, B, C, D, E : integer;
  Sum : integer;
  Average : real;

begin (* Main *)

  A := 45;
  B := 7;
  C := 68;
  D := 2;
  E := 34;
  Sum := A + B + C + D + E;
  Average := Sum / NumberOfIntegers;
  writeln ('Number of integers = ', NumberOfIntegers);
  writeln ('Number1 = ', A);
  writeln ('Number2 = ', B);
  writeln ('Number3 = ', C);
  writeln ('Number4 = ', D);
  writeln ('Number5 = ', E);
  writeln ('Sum = ', Sum);
  writeln ('Average = ', Average)

end. (* Main *) </delphi>

previous contents next