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.

A Practical Introduction <strong>to</strong> <strong>Pascal</strong> Programming Language⊡counter:=0;prevIntIsEven:=false;repeat(* 1- Reading the next value *)read(n);(* 2- Updating the other variables *)if (prevIntIsEven and (n mod 2=1)) thencounter:=counter+1;prevIntIsEven:=(n mod 2=1)until(n=0);writeln(’The situation happens ’,counter,’ times.’)end.Exercise 3.26. Write a program that reads a integers until a zero (0) is found and checks whetherthere exist some subsequence of 3 positions so that the sum of their elements is equal <strong>to</strong> 0.□The only complexity in this problem comes from remembering, at each step of the sequence,the value of the previous three values. Apart from that, we only need <strong>to</strong> sum the up each time<strong>to</strong> see if the result is equal <strong>to</strong> 0.⊡Program 66: Exercise 3.26program c03e26;constMIN_INTEGER=-32500;varn:integer;prevInt,prevPrevInt:integer;result:boolean;beginwriteln(’Please input the sequence of integers ended by zero (0): ’);prevInt:=MIN_INTEGER;prevPrevInt:=MIN_INTEGER;result:=false;repeat(* 1- Reading the next value *)read(n);(* 2- Updating the other variables *)if (prevInt+prevPrevInt+n=0) thenresult:=true;prevPrevInt:=prevInt;prevInt:=nuntil(n=0);if (result) thenwriteln(’The situation happens at least once.’)elsewriteln(’The situation does not happen in the sequence.’)end.Exercise 3.27. Write a program that reads a sequence of integers and computes the number ofsubsequences of 3 positions so that their values are sorted in increasing order.□This problem is very similar <strong>to</strong> the one in the Exercise 3.26, exception made that we are nowchecking the ordering of the values, instead of their sum.58

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

Saved successfully!

Ooh no, something went wrong!