13.07.2015 Views

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Iterative constructions3.4.3 On-screen matrix displayingThis problems consists of creating in the command line some sort of mosaic dependent uponsome value introduced by the user. Mostly, their resolution lies on finding out an expression able<strong>to</strong> represent the information <strong>to</strong> be displayed at every position of the matrix. If such expression isfound, we only have the need of using a double for statement <strong>to</strong> go through rows and columns.Exercise 3.31. Create a program that reads an integer N from the keyboard and writes a matrixof N × N elements so that at each position (i, j) the value i · j is displayed. That is, a matrix suchas1 2 3 . . . N2 4 6 . . . 2 · N3 6 9 . . . 3 · N. . . . . . . . . . . . . . .N 2 · N 3 · N . . . N 2□In this case the expression <strong>to</strong> be applied at each position is simply i · j, where i representsthe number of row and j the number of column. Note that we must write each cell with thecommand write, so that we stay in the same line until we finish each row.Program 71: Exercise 3.31program c03e31;varn:integer;i,j:integer;beginwrite(’Please input the value of n: ’);readln(n);for i:=1 <strong>to</strong> n dobeginfor j:=1 <strong>to</strong> n dowrite(’ ’,i*j:4,’ ’);writelnendend.⊡Exercise 3.32. Create a program that reads an integer N from the keyboard and writes a matrixof N × N elements so that at each position (i, j) the Manhattan distance <strong>to</strong> the main diagonal.That is, a matrix such as0 1 2 . . . N1 0 1 . . . N − 12 1 0 . . . N − 2 .. . . . . . . . . . . . . . .N N − 1 N − 2 . . . 0□The distance <strong>to</strong> the main diagonal is always the either the distance in horizontal or the distancein vertical (it is always the same). Hence, at each position (i, j) we must display either |i − j| or|j − i|.61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!