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 81: Exercise 4.4 (version C)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;min,max,sum:integer;mean:real;beginwriteln(’Introduce the vec<strong>to</strong>r of ’,N,’ elements, splitted by blank spaces.’);(* Reading the first value *)read(myVec<strong>to</strong>r[1]);(* Setting the initial values *)min:=myVec<strong>to</strong>r[1];max:=myVec<strong>to</strong>r[1];sum:=myVec<strong>to</strong>r[1];(* Reading and processing the remaining values *)for i:=2 <strong>to</strong> N dobeginread(myVec<strong>to</strong>r[i]);if (myVec<strong>to</strong>r[i]max) thenmax:=myVec<strong>to</strong>r[i];sum:=sum+myVec<strong>to</strong>r[i]end;mean:=sum/N;writeln(’The max is ’,max,’, the min is ’,min,’, and the mean is ’,mean:3:3,’.’)end.⊡Exercise 4.5. Create a program that reads a vec<strong>to</strong>r of N integers (being N a constant), then findsout its standard deviation.□The difference between this exercise and the previous is the fact that we need at least twoiterations over the values of the array <strong>to</strong> compute the expected result. The reason is that, inorder <strong>to</strong> compute the standard deviation, we need <strong>to</strong> know the mean of the values. Hence, afirst pass through the vec<strong>to</strong>r has <strong>to</strong> be accomplish <strong>to</strong> compute the mean, followed by a secondone <strong>to</strong> compute the standard deviation.Program 82: Exercise 4.5program c04e05;constN=5;typeintVec<strong>to</strong>r=ARRAY[1..N] OF integer;vari:integer;myVec<strong>to</strong>r:intVec<strong>to</strong>r;mean,std:real;begin(* Reading *)71

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

Saved successfully!

Ooh no, something went wrong!