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 78: Exercise 4.3program c04e03;constN=10;typeintMatrix=ARRAY[1..N,1..N] OF integer;vari,j:integer;matA:intMatrix;begin(* 1- Reading the vec<strong>to</strong>r *)writeln(’Introduce the matrix.’);for i:=1 <strong>to</strong> N dofor j:=1 <strong>to</strong> N dobeginwrite(’Introduce the value at row ’,i,’ and col ’,j,’: ’);read(matA[i][j])end;(* 2- Displaying the values *)for i:=1 <strong>to</strong> N dobeginfor j:=1 <strong>to</strong> N dowrite(matA[i][j],’ ’);writelnendend.⊡Exercise 4.4. Create a program that reads a vec<strong>to</strong>r of N reals (being N a constant), then finds outits min value, its max value and its mean.□This exercise is solved by following a 2-step scheme. First, we need <strong>to</strong> read the whole array,for later going trough all the values. Note that the variables minVal and maxVal must be set<strong>to</strong> some value before analyzing the first position of the array. In this case we set them <strong>to</strong> themaximum and minimum value an integer can have, respectively, so that they are never taken asfinal values of the variables. Note that the when the first element of the array is checked, bothminVal and maxVal are set <strong>to</strong> its value.Program 79: Exercise 4.4 (version A)program c04e04;constN=10;typeintVec<strong>to</strong>r=ARRAY[1..N] OF integer;vari:integer;myVec<strong>to</strong>r:intVec<strong>to</strong>r;minVal,maxVal,sum:integer;mean:real;begin(* 1- Reading the vec<strong>to</strong>r *)writeln(’Introduce the vec<strong>to</strong>r of ’,N,’ elements, splitted by blank spaces.’);for i:=1 <strong>to</strong> N doread(myVec<strong>to</strong>r[i]);(* 2- Obtaining the information *)minVal:=32000;maxVal:=-32000;69

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

Saved successfully!

Ooh no, something went wrong!