How to resolve the algorithm Show ASCII table step by step in the Free Pascal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Show ASCII table step by step in the Free Pascal programming language

Table of Contents

Problem Statement

Show  the ASCII character set  from values   32   to   127   (decimal)   in a table format.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Show ASCII table step by step in the Free Pascal programming language

Source code in the free programming language

// The FPC (FreePascal compiler) discards the program header
// (except in ISO-compliant “compiler modes”).
program showAsciiTable(output);

const
	columnsTotal = 6;

type
	// The hash indicates a char-data type.
	asciiCharacter = #32..#127;
	asciiCharacters = set of asciiCharacter;

var
	character: asciiCharacter;
	characters: asciiCharacters;
	column, line: integer;
begin
	// characters needs to be initialized,
	// because the next `include` below
	// will _read_ the value `characters`.
	// Reading _unintialized_ values, however,
	// is considered bad practice in Pascal.
	characters := [];
	// `div` denotes integer division in Pascal,
	// that means the result will be an _integer_-value.
	line := (ord(high(asciiCharacter)) - ord(low(asciiCharacter)))
		div columnsTotal + 1;
	// Note: In Pascal for-loop limits are _inclusive_.
	for column := 0 to columnsTotal do
	begin
		// This is equivalent to
		//   characters := characters + […];
		// i.e. the union of two sets.
		include(characters, chr(ord(low(asciiCharacter)) + column * line));
	end;
	
	for line := line downto 1 do
	begin
		// the for..in..do statement is an Extended Pascal extension
		for character in characters do
		begin
			// `:6` specifies minimum width of argument
			// [only works for write/writeLn/writeStr]
			write(ord(character):6, ' : ', character);
		end;
		// emit proper newline character on `output`
		writeLn;
		
		// `characters` is evaluated prior entering the loop,
		// not every time an iteration finished.
		for character in characters do
		begin
			// These statements are equivalent to
			//   characters := characters + [character];
			//   characters := characters - [succ(character)];
			// respectively, but shorter to write,
			// i.e. less susceptible to spelling mistakes.
			exclude(characters, character);
			include(characters, succ(character));
		end;
	end;
end.


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the OCaml programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the OCaml programming language
You may also check:How to resolve the algorithm Loops/While step by step in the ActionScript programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Joy programming language
You may also check:How to resolve the algorithm String concatenation step by step in the C++ programming language