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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data structuresProgram 84: Exercise 4.6 (Version B)program c04e06;constN=5;typeintVec<strong>to</strong>r=ARRAY[1..N] OF integer;vari:integer;A,B:intVec<strong>to</strong>r;begin(* Reading *)writeln(’Introduce the vec<strong>to</strong>r of ’,N,’ elements, splitted by blank spaces.’);for i:=1 <strong>to</strong> N dobeginread(A[i]);end;(* Accumulating *)B[1]:=A[1];for i:=2 <strong>to</strong> N doB[i]:=B[i-1]+A[i];(* Accumulating *)write(’The values in the vec<strong>to</strong>r are: ’);for i:=1 <strong>to</strong> N dowrite(B[i],’ ’);end.⊡Exercise 4.7. Create a program that reads a vec<strong>to</strong>r A of N reals (being N a constant), and createsa new vec<strong>to</strong>r so that at every position i it contains the sum of the values of A in the positionsi − 2, i − 1 and i.□This problem is similar <strong>to</strong> the previous, but the solution cannot be as elegant as that inProgram 84. The reason is that different positions of B, more specifically the first 3, cannot befilled in using the general schema, because for such positions there are not enough values on theleft. Still, the general solution for the problem resembles that in the previous exercise, exceptionmade that we need <strong>to</strong> remove one of the values from the count in B[i-1], since the value in theposition i-3 should not be taken in<strong>to</strong> account in the sum for a given B[i].Program 85: Exercise 4.7program c04e07;constN=10;typeintVec<strong>to</strong>r=ARRAY[1..N] OF integer;vari:integer;A,B:intVec<strong>to</strong>r;begin(* Reading *)writeln(’Introduce the vec<strong>to</strong>r of ’,N,’ elements, splitted by blank spaces.’);for i:=1 <strong>to</strong> N doread(A[i]);(* Accumulating *)B[1]:=A[1];(* Initial values *)for i:=2 <strong>to</strong> 3 do73

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

Saved successfully!

Ooh no, something went wrong!