CHAPTER-1



CHAPTER-9

HANDLING CHARACTERS

GENERAL

We have seen some simple examples of the use of the intrinsic character type. Armed with the further weapons of arrays and more advanced I/O facilities we can now tackle more interesting problems involving characters, or strings as they are often called.

9.1 Characters

To recap, a character constant is a string of characters enclosed in delimiters, which are either 'apostrophes' or "quotes". The delimiters are not part of the string. Character variables may be declared in the following ways:

CHARACTER ALPHA ! length of 1

CHARACTER (15) Name ! length of 15

CHARACTER Word*5 ! length of 5

Assignment is done as follows:

Name = "Bonaparte, N"

9.2 Bar Charts and Frequency

The first example utilizes an array and the A edit descriptor for printing characters. Suppose we want to analyse the results of a test written by a class of students. We would like to know how many students obtained percentage marks in the range 0-9,10-19, ..., 90-99. Each of these ranges is called a decile, numbered from zero for convenience. We also need to cater for the bright sparks who get 100 (the eleventh "decile"). Suppose the numbers of students who get marks in these ranges are as follows:

1 0 12 9 31 26 49 26 24 6 1

i.e. 12 obtained marks in the range 20-29. We need an array F(0:10), say, with 11 elements, where each element stores the number of students with marks in that particular range, e.g. F(2) should have the value 12. The following program prints a bar chart of the frequency distribution F, where each asterisk represents one student in that range:

INTEGER, DIMENSION(0:10) :: F = (/1, 0, 12, 9, 31, 26, 49, 26, 24, 6, 1 /)

10 FORMAT(I3, ‘ – ‘, I3, ‘ (‘, I3, ‘):’, 60Al )

20 FORMAT( ‘100’, 6X, ‘(‘, I3, ‘):’, 60Al )

DO I = 0, 10

IF (I < 10) THEN

PRINT 10, 10 * I, 10 * I + 9, F(I), (‘*’, J = 1, F(I))

ELSE

PRINT 20, F(I), (‘*’, J = 1, F(I))

END IF

END DO

END

Output:

0 - 9 ( 1):*

10 - 19 ( 0):

20 - 29 ( 12):************

30 - 39 ( 9):*********

40 - 49 ( 31):*******************************

50 - 59 ( 26):**************************

60 - 69 ( 49):*************************************************

70 - 79 ( 26):**************************

80 - 89 ( 24):************************

90 - 99 ( 6):******

100 ( 1):*

Note the absence of asterisks for the 10-19 decile. This is because F(l) has the value zero, so that the implied DO in the PRINT statement has a zero trip count when I has the value 1.

Of course, in a real situation, the frequencies will not be presented to you neatly on a plate. You are more likely to have a list of the actual marks. You should adapt the program to read a sample set of marks, in the range 0-100, and to convert them into frequencies. The basic mechanism is

READ(...) MARK

K = INT(MARK /10) ! K is the decile

F(K) = F(K) + 1 ! another mark in the Kth decile

3. Sorting Words

Characters may be compared in IF statements; this is the basis of alphabetic sorting. Each computer system has a collating sequence which specifies the intrinsic ordering of the available character set. The FORTRAN 95 standard requires only that

A ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download