Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Solution: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Basic Pascal Tutorial/Chapter 3/Solution

OldNewLine
1+3Ea - Solutions (author: Tao Yue, state: unchanged)
2+
3+**Solution to Fibonacci Sequence Problem**
4+
5+``` pascal
6+(* Author: Tao Yue
7+ Date: 19 July 1997
8+ Description:
9+ Find the first 10 Fibonacci numbers
10+ Version:
11+ 1.0 - original version
12+*)
13+
14+program Fibonacci;
15+
16+var
17+ Fibonacci1, Fibonacci2 : integer;
18+ temp : integer;
19+ count : integer;
20+
21+begin (* Main *)
22+ writeln ('First ten Fibonacci numbers are:');
23+ count := 0;
24+ Fibonacci1 := 0;
25+ Fibonacci2 := 1;
26+ repeat
27+ write (Fibonacci2:7);
28+ temp := Fibonacci2;
29+ Fibonacci2 := Fibonacci1 + Fibonacci2;
30+ Fibonacci1 := Temp;
31+ count := count + 1
32+ until count = 10;
33+ writeln;
34+
35+ (* Of course, you could use a FOR loop or a WHILE loop
36+ to solve this problem. *)
37+
38+end. (* Main *)
39+```
40+
41+**Solution to Powers of Two Problem**
42+
43+``` pascal
44+(* Author: Tao Yue
45+ Date: 13 July 2000
46+ Description:
47+ Display all powers of two up to 20000, five per line
48+ Version:
49+ 1.0 - original version
50+*)
51+
52+program PowersofTwo;
53+
54+const
55+ numperline = 5;
56+ maxnum = 20000;
57+ base = 2;
58+
59+var
60+ number : longint;
61+ linecount : integer;
62+
63+begin (* Main *)
64+ writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
65+ (* Set up for loop *)
66+ number := 1;
67+ linecount := 0;
68+ (* Loop *)
69+ while number <= maxnum do
70+ begin
71+ linecount := linecount + 1;
72+ (* Print a comma and space unless this is the first
73+ number on the line *)
74+ if linecount > 1 then
75+ write (', ');
76+ (* Display the number *)
77+ write (number);
78+ (* Print a comma and go to the next line if this is
79+ the last number on the line UNLESS it is the
80+ last number of the series *)
81+ if (linecount = numperline) and not (number * 2 > maxnum) then
82+ begin
83+ writeln (',');
84+ linecount := 0
85+ end;
86+ (* Increment number *)
87+ number := number * base;
88+ end; (* while *)
89+ writeln;
90+
91+ (* This program can also be written using a
92+ REPEAT..UNTIL loop. *)
93+
94+end. (* Main *)
95+```
96+
97+Note that I used three constants: the base, the number of powers to display on each line, and the maximum number. This ensures that the program can be easily adaptable in the future.
98+
99+Using constants rather than literals is a good programming habit to form. When you write really long programs, you may refer to certain numbers thousands of times. If you hardcoded them into your code, you'd have to search them out. Also, you might use the same value in a different context, so you can't simply do a global Search-and-Replace. Using a constant makes it simpler to expand the program.
100+
101+Also note that I used the `longint` type for the number variable. This is because to fail the test `number <= 20000`, `number` would have to reach `32768`, the next power of two after `16384`. This exceeds the range of the integer type: `-32768` to `32767`. (try it without `longint` and see what happens)
102+
103+---
104+*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/Solution", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history