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

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
2Fa - Solution (author: Tao Yue, state: unchanged)
 
2Fa - Solution (author: Tao Yue, state: unchanged)
<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"> </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"> </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="#000000">  5:</font><font color="#cc0000"> </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><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">2.0</font><font color="#cc0000"> </font><font color="#cc0000">-</font><font color="#cc0000"> </font><font color="#cc0000">read</font><font color="#cc0000"> </font><font color="#cc0000">in</font><font color="#cc0000"> </font><font color="#cc0000">data</font><font color="#cc0000"> </font><font color="#cc0000">from</font><font color="#cc0000"> </font><font color="#cc0000">keyboard</font>
+
      1.0 - original version
<font color="#000000">  8:</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
      2.0 - read in data from keyboard
<font color="#000000">  9:</font>
+
*)
<font color="#000000">  10:</font> <font color="#006699"><strong>program</strong></font> SumAverage<font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  11:</font>
 
<font color="#000000">  12:</font> <font color="#006699"><strong>const</strong></font>
 
<font color="#000000">  13:</font>  NumberOfIntegers <font color="#000000"><strong>=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  14:</font>
 
<font color="#000000">  15:</font> <font color="#006699"><strong>var</strong></font>
 
<font color="#000000">  16:</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>
 
<font color="#000000">  17:</font>  Sum <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  18:</font>  Average <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  19:</font>
 
<font color="#000000">  20:</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="#000000">  21:</font>  <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Enter</font><font color="#ff00cc"> </font><font color="#ff00cc">the</font><font color="#ff00cc"> </font><font color="#ff00cc">first</font><font color="#ff00cc"> </font><font color="#ff00cc">number:</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  22:</font>  readln <font color="#000000"><strong>(</strong></font>A<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  23:</font>  <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Enter</font><font color="#ff00cc"> </font><font color="#ff00cc">the</font><font color="#ff00cc"> </font><font color="#ff00cc">second</font><font color="#ff00cc"> </font><font color="#ff00cc">number:</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  24:</font>  readln <font color="#000000"><strong>(</strong></font>B<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  25:</font>  <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Enter</font><font color="#ff00cc"> </font><font color="#ff00cc">the</font><font color="#ff00cc"> </font><font color="#ff00cc">third</font><font color="#ff00cc"> </font><font color="#ff00cc">number:</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  26:</font>  readln <font color="#000000"><strong>(</strong></font>C<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  27:</font>  <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Enter</font><font color="#ff00cc"> </font><font color="#ff00cc">the</font><font color="#ff00cc"> </font><font color="#ff00cc">fourth</font><font color="#ff00cc"> </font><font color="#ff00cc">number:</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  28:</font>  readln <font color="#000000"><strong>(</strong></font>D<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  29:</font>  <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Enter</font><font color="#ff00cc"> </font><font color="#ff00cc">the</font><font color="#ff00cc"> </font><font color="#ff00cc">fifth</font><font color="#ff00cc"> </font><font color="#ff00cc">number:</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  30:</font>  readln <font color="#000000"><strong>(</strong></font>E<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  31:</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>
 
<font color="#000000">  32:</font>  Average <font color="#000000"><strong>:=</strong></font> Sum <font color="#000000"><strong>/</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  33:</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>
 
<font color="#000000">  34:</font>  writeln<font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  35:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number1:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> A<font color="#000000"><strong>:</strong></font><font color="#ff0000">8</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  36:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number2:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> B<font color="#000000"><strong>:</strong></font><font color="#ff0000">8</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  37:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number3:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> C<font color="#000000"><strong>:</strong></font><font color="#ff0000">8</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  38:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number4:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> D<font color="#000000"><strong>:</strong></font><font color="#ff0000">8</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  39:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Number5:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> D<font color="#000000"><strong>:</strong></font><font color="#ff0000">8</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  40:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">================</font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  41:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Sum:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> Sum<font color="#000000"><strong>:</strong></font><font color="#ff0000">12</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  42:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Average:</font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> Average<font color="#000000"><strong>:</strong></font><font color="#ff0000">10</font><font color="#000000"><strong>:</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  43:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>
 
  
 +
program SumAverage;
 +
 +
const
 +
  NumberOfIntegers = 5;
 +
 +
var
 +
  A, B, C, D, E : integer;
 +
  Sum : integer;
 +
  Average : real;
 +
 +
begin    (* Main *)
 +
  write ('Enter the first number: ');
 +
  readln (A);
 +
  write ('Enter the second number: ');
 +
  readln (B);
 +
  write ('Enter the third number: ');
 +
  readln (C);
 +
  write ('Enter the fourth number: ');
 +
  readln (D);
 +
  write ('Enter the fifth number: ');
 +
  readln (E);
 +
  Sum := A + B + C + D + E;
 +
  Average := Sum / 5;
 +
  writeln ('Number of integers = ', NumberOfIntegers);
 +
  writeln;
 +
  writeln ('Number1:', A:8);
 +
  writeln ('Number2:', B:8);
 +
  writeln ('Number3:', C:8);
 +
  writeln ('Number4:', D:8);
 +
  writeln ('Number5:', E:8);
 +
  writeln ('================');
 +
  writeln ('Sum:', Sum:12);
 +
  writeln ('Average:', Average:10:1);
 +
end.
 +
</delphi>
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
 
|[[Programming_Assignment_2|previous]]   
 
|[[Programming_Assignment_2|previous]]   

Revision as of 15:22, 5 January 2010

2Fa - Solution (author: Tao Yue, state: unchanged) <delphi> (* Author: Tao Yue

  Date:      19 June 1997
  Description:
     Find the sum and average of five predefined numbers
  Version:
     1.0 - original version
     2.0 - read in data from keyboard
  • )

program SumAverage;

const

  NumberOfIntegers = 5;

var

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

begin (* Main *)

  write ('Enter the first number: ');
  readln (A);
  write ('Enter the second number: ');
  readln (B);
  write ('Enter the third number: ');
  readln (C);
  write ('Enter the fourth number: ');
  readln (D);
  write ('Enter the fifth number: ');
  readln (E);
  Sum := A + B + C + D + E;
  Average := Sum / 5;
  writeln ('Number of integers = ', NumberOfIntegers);
  writeln;
  writeln ('Number1:', A:8);
  writeln ('Number2:', B:8);
  writeln ('Number3:', C:8);
  writeln ('Number4:', D:8);
  writeln ('Number5:', E:8);
  writeln ('================');
  writeln ('Sum:', Sum:12);
  writeln ('Average:', Average:10:1);

end. </delphi>

previous contents next