25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

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.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 48<br />

k := (i+j) DIV 2;<br />

IF x < a[k] THEN j := k-1 END;<br />

IF a[k] < x THEN i := k+1 END<br />

UNTIL i > j<br />

Program C:<br />

i := 0; j := N-1;<br />

REPEAT<br />

k := (i+j) DIV 2;<br />

IF x < a[k] THEN j := k ELSE i := k+1 END<br />

UNTIL i > j<br />

Hint: All programs must terminate with a k = x, if such an element exists, or a k ≠ x, if there exists no<br />

element with value x.<br />

1.7. A company organizes a poll to determine the success of its products. Its products are records <strong>and</strong><br />

tapes of hits, <strong>and</strong> the most popular hits are to be broadcast in a hit parade. The polled population is to be<br />

divided into four categories according to sex <strong>and</strong> age (say, less or equal to 20, <strong>and</strong> older than 20). Every<br />

person is asked to name five hits. Hits are identified by the numbers 1 to N (say, N = 30). The results of the<br />

poll are to be appropriately encoded as a sequence of characters.<br />

Hint: use procedures Read <strong>and</strong> ReadInt to read the values of the poll.<br />

TYPE hit = INTEGER;<br />

reponse = RECORD name, firstname: Name;<br />

male: BOOLEAN;<br />

age: INTEGER;<br />

choice: ARRAY 5 OF hit<br />

END;<br />

VAR poll: Files.File<br />

This file is the input to a program which computes the following results:<br />

1. A list of hits in the order of their popularity. Each entry consists of the hit number <strong>and</strong> the number of<br />

times it was mentioned in the poll. Hits that were never mentioned are omitted from the list.<br />

2. Four separate lists with the names <strong>and</strong> first names of all respondents who had mentioned in first place<br />

one of the three hits most popular in their category.<br />

The five lists are to be preceded by suitable titles.<br />

References<br />

[1.1] O-.J. Dahl, E.W. Dijkstra, C.A.R. Hoare. Structured Programming. F. Genuys, Ed., New York,<br />

Academic Press, 1972.<br />

[1.2] C.A.R. Hoare, in Structured Programming [1.1], pp. 83-174.<br />

[1.3] K. Jensen <strong>and</strong> N. Wirth. PASCAL — User Manual <strong>and</strong> Report. Springer-Verlag, 1974.<br />

[1.4] N. Wirth. Program development by stepwise refinement. Comm. ACM, 14, No. 4 (1971), 221-27.<br />

[1.5] N. Wirth. Programming in Modula-2. Springer-Verlag, 1982.<br />

[1.6] N. Wirth. On the composition of well-structured programs. Computing Surveys, 6, No. 4, (1974)<br />

247-59.<br />

[1.7] C.A.R. Hoare. The Monitor: An operating systems structuring concept. Comm. ACM 17, 10 (Oct.

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

Saved successfully!

Ooh no, something went wrong!