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.

A Practical Introduction <strong>to</strong> <strong>Pascal</strong> Programming Languagethe leftmost part of the i-th line we have <strong>to</strong> include as many as i − 1 blank spaces. Then, afterthe first char we have <strong>to</strong> write as many black spaces as the length of the line minus 2 (the spaceoccupied by the chars) and 2 · (i − 1) (the number of blanks paces on the left and right of suchchars). Hence, for a line i we need length(str) − 2 ∗ i blank spaces.Program 104: Exercise 4.23program c04e23;uses math;constN=15;varstr:STRING[N];i,j:integer;begin(* Reading the string *)write(’Please input the string of, at most, ’,N,’ chars: ’);readln(str);(* Writing the matrix *)for i:=1 <strong>to</strong> ceil(length(str)/2) dobeginfor j:=1 <strong>to</strong> min(i-1,length(str)-i) dowrite(’ ’);write(str[i]);for j:=1 <strong>to</strong> length(str)-(2*i) dowrite(’ ’);if (abs( (i-1)-(length(str)-i))-1)>=0 thenwriteln(str[length(str)-i+1])elsewritelnendend.⊡Exercise 4.24. Create a program that reads a string and draws it as a bot<strong>to</strong>m-based pyramid.For example, the word computer must look like:computer□We can understand this problem as a derivation of the preceding one. Indeed, we can achievethe solution by tweaking the value of the variable used in the outer for iterative construction,and changing the <strong>to</strong> clause by down<strong>to</strong>.Program 105: Exercise 4.24program c04e24;uses math;constN=15;varstr:STRING[N];i,j:integer;begin(* Reading the string *)88

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

Saved successfully!

Ooh no, something went wrong!