Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Enumerated types, as array subscripts

To remind you, an enumerated type is a collection of identifiers which are ordered. Things that can become enumerated types include such things as numbers, letters, days of the week, months and so on. In Pascal numbers and letters, the ASCII characters are already taken care of, code, but in the case of days and months we would have to use enumerated types for creating our own. The section in chapter three goes into how to do this in detail, since here I am going to concentrate on showing you how to use them as array subscripts. Again subscripts indicate which element of a string or array is being inspected. In the next program I am going to declare a two-dimensional array with subscripts designating playing cards. I have chosen playing cards just so that I can demonstrate how whimsical and arbitrary your choice of subscript can be.

Program 5

PROGRAM random_card(INPUT,OUTPUT); { this program uses enumerated types as subscripts for an array and allows the user to almost select cards randomly. It is meant more as a demonstration program of how to declare arrays with enumerated types than a really good random card generating program } TYPE CARD = (ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN, EIGHT,NINE,TEN,JACK,QUEEN,KING); SUIT = (SPADES,CLUBS,HEARTS,DIAMONDS); CARD_ARRAY = ARRAY[ACE..KING,SPADES..DIAMONDS] OF INTEGER; VAR pack_of_cards : CARD_ARRAY; I : CARD; J : SUIT; key, choice : CHAR; BEGIN FOR I := ACE TO KING DO FOR J := SPADES TO DIAMONDS DO pack_of_cards[I,J] := 0; WRITELN('Hold down <return> or enter the @ '); WRITE('Character to choose'); READLN(key); I := ace; J := diamonds; REPEAT WHILE (key <> '@') DO BEGIN IF (I = KING) THEN I := ACE ELSE I := SUCC(I); IF (J = diamonds) THEN J := spades ELSE J := SUCC(J); READLN(key); END; pack_of_cards[I,J] := pack_of_cards[I,J] + 1; WRITE('Another choice : y/n '); READLN(choice); key := '0'; UNTIL (choice = 'n'); FOR I := ACE TO KING DO FOR J := SPADES TO DIAMONDS DO IF (pack_of_cards[I,J] > 0) THEN BEGIN WRITE('The card ',I,' of ',J,' was chosen : '); WRITELN(pack_of_cards[I,J],' times'); END; END. { program random_card }

Well that is about all you really need to know about multi-dimension arrays at this point.


 

Chapter 7

Writing Functions & Procedures

This chapter covers:

  • Writing Functions and Procedures.
  • Function and Procedure parameters.

In my opinion most students are introduced to the topic of functions and procedures far too early. Until your programs get more than a page or two long, or get involved with data structures such as arrays, which require repetitive operations such as searching, the benefit of writing your own functions and procedures is minimal. This being the case it is very difficult for the student to see the point of functions and procedures, when this topic is broached. In chapter three we took our first look at functions and procedures, these were the Pascal standard functions and procedures. These are defined internally, and are made available to the programmer without the need for the programmer to know exactly how they work. It is enough to know how to call these procedures and functions. As well as having these standard functions and procedures, Pascal allows the programmer to write their own functions and procedures. Probably the most beneficial reasons for writing your own functions and procedures are that;



  • You can parcel up repetitive code into one function or procedure and then call it many times.
  • It also helps to reduce or break down large programs into byte [sic] sized pieces. Ha Ha. Much like paragraphs and chapters do in books.
  • It is possible for programmers to share functions and procedures with other programmers.

Date: 2016-03-03; view: 721


<== previous page | next page ==>
Moving though multi-dimensioned arrays | Syntax of the FUNCTION heading
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)