home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / T_LESSON.ZIP / TP-50D < prev   
Encoding:
Text File  |  1989-05-08  |  37.0 KB  |  1,153 lines

  1.  
  2. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  72
  3.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  4.  
  5.  
  6.  
  7.           
  8.  
  9.           TURBO-LESSON 12.  A FUNCTION TO DETECT ERRORS
  10.  
  11.           OBJECTIVES - In this lesson, you will learn about:
  12.  
  13.           1.  Error detection
  14.           2.  Using a predefined function
  15.           3.  Writing your own function
  16.  
  17.  
  18.                                  1.  Error Detection.
  19.  
  20.           In the previous lesson,  you found that some input values caused a
  21.           problem.  The function used in PROG11A calculated the cube of a
  22.           number entered.  If the result was outside the range of valid
  23.           integers the result is wrong but Pascal doesn't alert you to the
  24.           problem.
  25.  
  26.           If you write programs for others to use, you will have to deal with
  27.           the problem of ERRORS.
  28.  
  29.           There are several approaches to error handling:
  30.  
  31.           (1) Error detection before it happens - prevent the occurrence of
  32.           the error.
  33.  
  34.           (2) Error detection when it happens - take corrective action.
  35.  
  36.           (3) Ignore the error - let the program bomb!
  37.  
  38.           The 3rd is not usually acceptable - but may be o.k. in the early
  39.           stages of program development since you, the programmer, can fix
  40.           the problem.  You may also find alternate ways to program for the
  41.           same result while avoiding the possibility of the error.
  42.  
  43.           The 2nd, error detection when it happens, will be explored later.
  44.           Input/Output errors are typical examples of this class of errors.
  45.  
  46.           In this lesson, you will find ways to detect a problem and prevent
  47.           its occurrence.
  48.  
  49.           ##### DO:
  50.  
  51.           In PROG12, examine FUNCTION Cube.
  52.  
  53.           The function has been expanded to detect integers which are too
  54.           small or too large to produce a valid integer cube.  If a number is
  55.           entered which would cause an error, the result is set to 0 instead
  56.           of the erroneous result.
  57.  
  58.           ##### DO:
  59.  
  60.           Run PROG12 several times using the following values for input:
  61.  
  62. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  73
  63.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  64.  
  65.  
  66.  
  67.           3,  -3,  31, 32, -32, -33, 0
  68.  
  69.           Were all the results as expected?  The inputs, 32 and -33, would
  70.           produce cubes out of the range of valid integers, so these two
  71.           should have given results of 0.
  72.  
  73.           What about 0 as an input?  Did you get the correct result?
  74.  
  75.           Can you determine whether a result of 0 is valid  (0 input) or
  76.           invalid (input of < -32 or > 31)?
  77.  
  78.           Later in this lesson you will write your own function to deal with
  79.           this problem!
  80.  
  81.  
  82.                            2.  Using a Predefined Function.
  83.  
  84.           Pascal provides many functions and procedures which are already
  85.           defined.
  86.  
  87.           Some advantages of using predefined subprograms:
  88.  
  89.           (1)  The subprogram is already debugged.
  90.  
  91.           (2)  The subprogram doesn't take up room in your program.
  92.  
  93.           (3)  You can spend your time on more interesting programming, no
  94.           need to "reinvent the wheel".
  95.  
  96.           To use a predefined function, you have to know:
  97.  
  98.           (1)  The name of the function
  99.  
  100.           (2)  What goes in  (what values do you provide as input?)
  101.  
  102.           (3)  What comes out  (what result is associated with the function
  103.           name?)
  104.  
  105.           The absolute value function, ABS, can be used in PROG12 to
  106.           illustrate the use of a predefined function.
  107.  
  108.           What goes in              What comes out
  109.  
  110.               3 --------> [ABS] --------> 3
  111.  
  112.              -5 --------> [ABS] --------> 5
  113.  
  114.           The absolute value function provides a positive number of the same
  115.           magnitude as the positive or negative number input to the function.
  116.  
  117.           ##### DO:
  118.  
  119.           
  120.  
  121. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  74
  122.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  123.  
  124.  
  125.  
  126.           Add the following statement after the ReadLn(No) statement in
  127.           PROG12:
  128.                WriteLn('Absolute value: ', ABS(No) );
  129.  
  130.           Run the program several times with both positive and negative
  131.           numbers.
  132.  
  133.           NOTE: THERE ARE OFTEN SEVERAL WAYS TO DO THE SAME THING IN
  134.           PROGRAMMING.  YOU SHOULD BE LOOKING FOR WAYS TO DECIDE WHICH OF
  135.           SEVERAL PROGRAMMING SOLUTIONS IS BETTER IN A GIVEN CASE.
  136.  
  137.           The next exercise demonstrates a way to use ABS in the error
  138.           detection problem.
  139.  
  140.           ##### DO:
  141.  
  142.           Change the first line of the IF statement in the FUNCTION Cube to:
  143.                IF ABS(Number) > 31
  144.  
  145.           Test the program with several values.
  146.  
  147.           How would you decide whether to use the function, ABS, in the main
  148.           program or in the function, Cube?
  149.  
  150.           Is the action accomplished by ABS of interest to you in getting the
  151.           cube of a number?  If not, it should probably be pushed out of the
  152.           main program and into the subprogram.
  153.  
  154.  
  155.                             3.  Writing Your Own Function.
  156.  
  157.           Now, it's your turn.  Another approach to the error detection
  158.           problem uses a second function, which you are about to write!
  159.  
  160.           Give the function the name:        Has_Valid_Cube
  161.  
  162.           The function will have one input:  Number  of type Integer
  163.  
  164.           The type of the function will be:  Boolean
  165.  
  166.           What the function does:
  167.  
  168.           If Number would produce a valid cube, the function, Has_Valid_Cube,
  169.           will have the value, TRUE.
  170.  
  171.           If Number would produce an error, Has_Valid_Cube will have the
  172.           value, FALSE.
  173.  
  174.           ##### DO:
  175.  
  176.           Write the function, Has_Valid_Cube.  Place it before the main
  177.           program.  It can be either before or after the function, Cube.
  178.  
  179. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  75
  180.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  181.  
  182.  
  183.  
  184.           Look at FUNCTION Cube if you need help with the form of the
  185.           function declaration or the IF statement needed.
  186.  
  187.           ##### DO:
  188.  
  189.           In the main program, replace the WriteLn which references Cube with
  190.           the following:
  191.  
  192.           IF Has_Valid_Cube (No)
  193.             THEN
  194.               WriteLn('The cube is: ', Cube(No) )
  195.             ELSE
  196.               BEGIN
  197.                 WriteLn('The cube of ',No,' is outside the integer range');
  198.                 WriteLn('in this version of Pascal.');
  199.               END;
  200.           
  201.  
  202.           Test the program with several positive and negative values and
  203.           values which would cause erroneous cubes.
  204.  
  205.           (If you have trouble writing the function, PROG12A is available as
  206.           a sample.  Don't check PROG12A until you have given it a try on
  207.           your own!)
  208.  
  209.           Are there any other improvements you want to make to PROG12?
  210.  
  211.           FUNCTION Cube still checks for invalid inputs.  Is this still
  212.           necessary?
  213.  
  214.           ##### DO:
  215.  
  216.           Change FUNCTION Cube so that it does no error checking, just
  217.           calculates the cube of the number input.
  218.  
  219.           Test the program with several values including 0.
  220.  
  221.           Note that there is no longer any ambiguity when the result is a
  222.           cube of 0.
  223.  
  224. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  76
  225.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  226.  
  227.  
  228.  
  229.           
  230.  
  231.           TURBO-LESSON 13.  STRINGS
  232.  
  233.           OBJECTIVES - In this lesson, you will learn about:
  234.  
  235.           1.  Strings
  236.           2.  String replacement statement
  237.           3.  Predefined string function, LENGTH
  238.  
  239.  
  240.                                      1.  Strings.
  241.  
  242.           You have already seen some Pascal strings in the WriteLn statements
  243.           of earlier lessons.
  244.                
  245.  
  246.           WriteLn('This is a string.');
  247.  
  248.           It is often convenient to store strings as variables or constants.
  249.  
  250.           A string constant may be defined in the CONST section:
  251.  
  252.           CONST   String_1  =  'TURBO-LESSONS';
  253.  
  254.           String variables must be declared in the VAR section.  The form of
  255.           the declaration is:
  256.  
  257.           VAR    First_Name : String[12];
  258.  
  259.           This sets up storage for a variable named First_Name which can
  260.           store a string up to 12 characters long.
  261.  
  262.  
  263.                            2.  String Replacement Statement.
  264.  
  265.           The replacement statement for strings is:
  266.  
  267.           String_Name := (string expression);
  268.  
  269.           ##### DO:
  270.  
  271.           Examine PROG13.  Notice the following:
  272.  
  273.           A string constant, S_Test is given the value 'Test String' in the
  274.           CONST declaration section.
  275.  
  276.           Several string variables are defined in the VAR section.
  277.  
  278.           ##### DO:
  279.  
  280.           Run the program.
  281.  
  282. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  77
  283.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  284.  
  285.  
  286.  
  287.           What happens when an attempt is made to store too long a string in
  288.           a string variable?
  289.  
  290.           ##### DO:
  291.  
  292.           Add the following to the program:
  293.  
  294.           S5 := S_Test;
  295.           WriteLn(S5);
  296.  
  297.           Run the program.
  298.  
  299.           How many characters of S5 are printed?
  300.  
  301.           ##### DO:
  302.  
  303.           Modify WriteLn(S5)
  304.             to:  WriteLn('[', S5, ']');
  305.  
  306.           Run the program.  How many characters of S5 are printed?
  307.  
  308.           DEBUGGING NOTE:  When working with strings, you may find it helpful
  309.           to print some kind of marker before and after a string to help
  310.           "see" the occurrences of the character, blank.
  311.  
  312.           You have seen what happens when storing 'Test String' in too short
  313.           a variable:  S3 holds 'Tes',  S8 holds 'Test Str'.
  314.  
  315.           What happens when a string is stored in a variable that is larger
  316.           than needed?  Are blanks added?
  317.  
  318.           ##### DO:
  319.  
  320.           Modify the WriteLn(S14) to bracket S14 (like you did above with S5)
  321.           and run the program.
  322.  
  323.           How many characters of S14 were printed?
  324.  
  325.           Were extra blanks added?    (More on this later.)
  326.  
  327.           ##### DO:
  328.  
  329.           Look at PROG13A.  (Don't forget you can Zoom to full screen with F5
  330.           to get a better look at the program in the Edit window.)
  331.  
  332.           WriteLn(S8[I]);
  333.  
  334.           Notice the use of the square brackets in the statement above.  This
  335.           is a way to refer to a specific character in a string.
  336.  
  337.           S8[2] means the 2nd character in the string, S8.
  338.  
  339.           NOTE: SQUARE BRACKETS ARE USED IN TWO DIFFERENT WAYS WITH STRINGS.
  340.           WHEN DECLARING VARIABLES, THE BRACKETS ENCLOSE THE MAXIMUM LENGTH
  341.  
  342. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  78
  343.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  344.  
  345.  
  346.  
  347.           OF THE STRING.  IN PROCESSING STATEMENTS, THE NUMBER IN THE
  348.           BRACKETS DESIGNATE A PARTICULAR CHARACTER IN A STRING.
  349.  
  350.           ##### DO:
  351.  
  352.           Run the program, using 2 as position number.
  353.  
  354.           Try 5 as an input.  What character was stored as the 5th character
  355.           of S8?
  356.  
  357.           The string stored in S8, 'TURBO', is 5 characters long but S8 is 8
  358.           characters long.
  359.  
  360.           What characters, if any, are stored in S8[6], S8[7], and S8[8]?
  361.  
  362.           ##### DO:
  363.  
  364.           Run the program with input values of 6, 7, and 8.
  365.  
  366.           What characters were printed?
  367.  
  368.           ##### DO:
  369.  
  370.           Terminate the program by entering -1 as position number.
  371.  
  372.           ##### DO:
  373.  
  374.           Change the first statement as the first statement in the BEGIN END
  375.           block from S8:= S_Test;
  376.                   to S8 := '12345678';
  377.  
  378.           Run the program again, using 6, 7, and 8 as input.
  379.  
  380.           What do you conclude about "unused" positions in a string?
  381.  
  382.           Before you are prompted to enter the "position number", S8 is
  383.           printed.
  384.  
  385.           Do positions 6, 7, 8 of the string print?   Why?
  386.  
  387.  
  388.                         3.  Predefined String Function, LENGTH.
  389.  
  390.           Because the length of a string is often needed in processing, the
  391.           function, LENGTH, has been provided for that purpose.
  392.  
  393.           ##### DO:
  394.  
  395.           Add the following statement after the UNTIL statement:
  396.                WriteLn('Length of string: ', LENGTH(S8) );
  397.  
  398.           Run the program.
  399.  
  400.           ##### DO:
  401.  
  402. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  79
  403.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  404.  
  405.  
  406.  
  407.           Just before the END, insert:
  408.  
  409.           FOR I := 1 to LENGTH(S8) DO
  410.              WriteLn('Position ', I:2, ':  ',S8[I]);
  411.  
  412.           Run the program.
  413.  
  414.           ##### DO:
  415.  
  416.           Change the statement
  417.           
  418.                S8 := '12345678'
  419.            to: S8 := 'OK';
  420.  
  421.           Run the program.
  422.  
  423.           Also try 'Wake Up' as a value for S8.
  424.  
  425. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  80
  426.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  427.  
  428.  
  429.  
  430.           
  431.  
  432.           TURBO-LESSON 14.  INTRODUCTION TO PROCEDURES
  433.  
  434.           OBJECTIVES - In this lesson, you will learn about:
  435.  
  436.           1.  PROCEDURE declaration
  437.           2.  Using a Procedure
  438.           3.  Using Parameters
  439.           4.  A counter with error checking
  440.  
  441.  
  442.                               1.  PROCEDURE Declaration.
  443.  
  444.           The PROCEDURE subprogram is similar to the FUNCTION subprogram
  445.           introduced in an earlier lesson.   The form of the declaration is:
  446.  
  447.                PROCEDURE Add(No_1, No_2 : Integer; VAR Sum : Integer);
  448.  
  449.                BEGIN
  450.                  Sum := No_1 + No_2;
  451.                END;
  452.  
  453.           Add is the name of the Procedure.
  454.  
  455.           No_1, No_2, and Sum are integer variables called "parameters".
  456.  
  457.           VAR in front of Sum indicates that this parameter is a "two-way
  458.           street".  It can receive data from the calling program, and return
  459.           data to the calling program.  No_1 and No_2 can only receive data
  460.           from the calling program.
  461.  
  462.           The BEGIN END block defines the processing performed by the
  463.           procedure:
  464.  
  465.           Add the value in the memory location, No_1, to the value in memory
  466.           location, No_2, and place the result in the memory location, Sum.
  467.  
  468.  
  469.                                 2.  Using a Procedure.
  470.  
  471.           A reference to this procedure in the main program (or another
  472.           procedure or function) would have the following form:
  473.  
  474.           Add(2, Count, Adjusted_Count);
  475.  
  476.           The procedure is "called" or utilized by simply using its name as a
  477.           statement.  (When you define a Procedure or Function, you are
  478.           adding additional "statements" to your programming language.)
  479.  
  480.           Notice that there are three "parameters" here, and three in the
  481.           Procedure declaration.  The three here are associated with the ones
  482.           in the declaration by position.
  483.  
  484. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  81
  485.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  486.  
  487.  
  488.  
  489.           The first parameter here, the integer, 2, provides input to the
  490.           first parameter of the procedure, No_1.
  491.  
  492.           The second, the variable, Count, provides input to the second
  493.           parameter of the procedure, No_2.
  494.  
  495.           The third, Adjusted_Count, provides input to the third parameter of
  496.           the procedure, Sum.
  497.  
  498.           Since Sum is declared VAR, variable, it also provides output back
  499.           to Adjusted_Count after the procedure does its calculation.
  500.  
  501.           In the Main Program     In Procedure Add
  502.  
  503.                          2 ----------> No_1    { When Procedure      }
  504.                      Count ----------> No_2    {    is called        }
  505.             Adjusted_Count ----------> Sum     {                     }
  506.           --------------------------------------------------------------
  507.             Adjusted_Count <---------- Sum     { When Procedure ends }
  508.  
  509.           ##### DO:
  510.  
  511.           Inspect PROG14.
  512.  
  513.           ##### DO:
  514.  
  515.           Add the following to the watch window:  Adjusted_Count, Count, Sum,
  516.           No_1, No_2.
  517.  
  518.           ##### DO:
  519.  
  520.           Press F7 to begin tracing.
  521.  
  522.           Notice the Procedure variables, No_1, No_2, and Sum are "Unknown
  523.           identifiers" at this point.  Count and Adjusted_Count have
  524.           "leftover" values--whatever happens to be in the memory assigned to
  525.           these 2 variables.
  526.  
  527.           ##### DO:
  528.  
  529.           Trace the Write and ReadLn statements.  Enter 4.  Stop when the ADD
  530.           statement is highlighted.
  531.  
  532.           Watch carefully what happens in the next step, when you trace the
  533.           ADD statement.  No_1, the first "parameter" in the Procedure ADD
  534.           should receive the value 2,  which is the first value listed in the
  535.           ADD statement in the main program.  No_2 should receive 4, the
  536.           value of Count.  Sum should receive the value of Adjusted_Count.
  537.  
  538.           ##### DO:
  539.  
  540.           Press F7 once to trace the ADD statement.
  541.  
  542.           ##### DO:
  543.  
  544. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  82
  545.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  546.  
  547.  
  548.  
  549.           Trace the Sum:=No_1 + No_2 statement.
  550.  
  551.           Which variables changed?
  552.  
  553.           ##### DO:
  554.  
  555.           Trace the rest of the program.   Watch the values of No_1, N0_2,
  556.           and Sum when the program goes from the procedure ADD back to the
  557.           main program.
  558.  
  559.           ##### DO:
  560.  
  561.           Add the following statement as the first statement in the main
  562.           program:
  563.  
  564.           Adjusted_Count := 10;
  565.  
  566.           Run the program.
  567.  
  568.           Does it make any difference what value is stored in Adjusted_Count
  569.           before Procedure Add is referenced?  Look at the procedure - is
  570.           Adjusted_Count used as an input for the calculation, or only as a
  571.           result?
  572.  
  573.  
  574.                                  3.  Using Parameters.
  575.  
  576.           You have already been using parameters, but in this section, you
  577.           will see a little more of the power and flexibility of parameters.
  578.  
  579.           ##### DO:
  580.  
  581.           Change the Add statement in the main program to:
  582.  
  583.           Add(2, 2, Adjusted_Count);
  584.  
  585.           Run the program.    Is the result as expected?
  586.  
  587.           ##### DO:
  588.  
  589.           Change the Add to:
  590.                Add(Count, Count, Adjusted_Count);
  591.  
  592.           Run the program.   Any surprizes?
  593.  
  594.           ##### DO:
  595.  
  596.           Change the Add to:
  597.                     Add(2, 3, 4);
  598.  
  599.           Run the program.   What happened?
  600.  
  601.           The compiler refused to accept 4 as a variable identifier.
  602.  
  603. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  83
  604.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  605.  
  606.  
  607.  
  608.           The VAR preceding Sum in the procedure declaration puts a limit on
  609.           the corresponding parameter in the calling program: it has to be a
  610.           variable location to receive the value of Sum when the procedure
  611.           finishes its calculation.
  612.  
  613.           The first two parameters in the procedure, No_1 and No_2, are used
  614.           only for input to the procedure - they do not provide any output
  615.           back to the corresponding parameters in the calling program.
  616.  
  617.           For this reason, the first two parameters in the calling program
  618.           are less restricted.  They can be constants, variables, or
  619.           expressions, as long as they are of the same type, integer, as the
  620.           corresponding parameters in the procedure.
  621.  
  622.           ##### DO:
  623.  
  624.           Change the Add statement:
  625.  
  626.           Add(4, 2 * Count + 5, Adjusted_Count);
  627.  
  628.           Run the program.   Does the expression work o.k. as a parameter?
  629.  
  630.           ##### DO:
  631.  
  632.           Change the Add to:
  633.                Add(Count, Count, Count);
  634.  
  635.           Also change the WriteLn to print the value of Count instead of
  636.           Adjusted_Count.
  637.  
  638.           Run the program.   Any problems?
  639.  
  640.  
  641.                           4.  A Counter with Error Checking.
  642.  
  643.           You could use the Procedure Add as a counter by using the following
  644.           call:
  645.  
  646.           Add(Count, 1, Count);
  647.  
  648.           The procedure would add 1 to Count and put the result in Count.
  649.  
  650.           You could accomplish the same thing with the statement:
  651.                Count := Count + 1;
  652.  
  653.           So why bother to use the procedure?
  654.  
  655.           What if Count reaches the upper limit of the integer range, 32767?
  656.  
  657.           In the main program you could expand the counting statement to:
  658.           
  659.  
  660.           IF Count < 32767
  661.              THEN
  662.  
  663. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  84
  664.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  665.  
  666.  
  667.  
  668.                Count := Count + 1
  669.              ELSE
  670.                WriteLn('Counter reached upper limit');
  671.           
  672.  
  673.           PROGRAMMING NOTE: HOW DO YOU DECIDE WHAT GOES INTO THE MAIN
  674.                             PROGRAM AND WHAT TO PUT IN SUBPROGRAMS?
  675.           
  676.                             FROM THE FOLLOWING DISCUSSION, TRY TO FORMULATE
  677.                             AT LEAST ONE RULE FOR MAKING THIS DECISION.
  678.  
  679.           This looks a little messy, maybe you should use a procedure to get
  680.           this out of the main program.
  681.  
  682.           Not a bad reason, but there's a more important reason:
  683.  
  684.           Ask yourself, "How much of the processing in the IF statement above
  685.           is of interest in the main program?"
  686.  
  687.           Probably only the fact that a count is being incremented.  The
  688.           error checking and how it is done is probably of little interest
  689.           and just clutters up the main program.
  690.  
  691.           ##### DO:
  692.  
  693.           Write your own procedure to increment the counter, Count.
  694.  
  695.           Call the procedure, Increment.
  696.  
  697.           Check for the upper limit of the integer range.  (The IF statement
  698.           above would be one way.)
  699.  
  700.           Note that only one parameter is needed, preceded by VAR.
  701.  
  702.           ##### DO:
  703.  
  704.           In the main program, add the following to check out your procedure:
  705.  
  706.           FOR I := 1 to 10 DO
  707.             BEGIN
  708.               Increment(Count);
  709.               WriteLn('Count = ', Count);
  710.             END;
  711.  
  712.           Run the program using the following values as input for count:
  713.  
  714.           0,  3,  -34, 32760
  715.  
  716.           (PROG14A is provided in case you need help.)
  717.  
  718.           You could also write a procedure, Decrement, to decrease a counter.
  719.           Note that the error checking would be checking the lower limit, -
  720.           32768, or perhaps 0 depending on how you intended to use the
  721.           counter.
  722.  
  723. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  85
  724.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  725.  
  726.  
  727.  
  728.           
  729.  
  730.           TURBO-LESSON 15.  INTERACTIVE SCREEN HANDLING
  731.  
  732.           OBJECTIVES - In this lesson, you will learn about:
  733.  
  734.           1.  Setting up a Data Entry Screen
  735.           2.  Being nice to users - ClrScr
  736.           3.  Getting around the screen - GotoXY
  737.           4.  Screen messages and accepting user input
  738.  
  739.  
  740.                           1.  Setting up a Data Entry Screen.
  741.  
  742.           For most computer processing applications you will need to provide
  743.           for entry of data.  This is one of the points where your programs
  744.           interact with the person using the program.
  745.  
  746.           How your programs are viewed by those using them will depend on how
  747.           well you manage the user-computer interaction on the screen.
  748.  
  749.           In this lesson you will try some of the basic techniques of screen
  750.           handling for data entry.
  751.  
  752.           NOTE: Some of the programs have the statement
  753.           
  754.                          Repeat Until KeyPressed;
  755.           
  756.           as the last statement.  This statement serves to keep the program
  757.           from ending until you press a key to end the program.  Anytime you
  758.           see this statement at the end of a program, remember that you must
  759.           press a key to end the program.  The reason for adding this
  760.           statement is to allow you to view the output screen after the
  761.           program runs.  Without it, the Edit screen covers up most of the
  762.           output screen as soon as the program is done.  In this lesson you
  763.           need to see the whole output screen.
  764.           
  765.  
  766.           ##### DO:
  767.  
  768.           Run PROG15.
  769.  
  770.           Take a look at the program to see how this screen was produced.
  771.  
  772.           ##### DO:
  773.  
  774.           Experiment with PROG15.
  775.  
  776.           Run the program after each of the following:
  777.  
  778.           (1)  Add or delete spaces in the WriteLn statements to move the
  779.           various items.
  780.  
  781. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  86
  782.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  783.  
  784.  
  785.  
  786.           (2)  Line the prompts up on the left.  You may want to keep the
  787.           colons in a vertical column after you move the prompts.
  788.  
  789.  
  790.                            2.  Being Nice to Users - ClrScr.
  791.  
  792.           Pascal provides a predefined procedure to clear the screen.
  793.  
  794.           Screen interaction will go smoother if unnecessary items are
  795.           removed when no longer needed.
  796.  
  797.           ##### DO:
  798.  
  799.           Add the following statement as the first statement in the main
  800.           program:
  801.  
  802.           WriteLn('This is something leftover from previous processing');
  803.  
  804.           ##### DO:
  805.  
  806.           Run the program.
  807.  
  808.           How does the data entry screen look now?
  809.  
  810.           There are often things left on the screen that need to be cleared
  811.           away for a better screen presentation.
  812.  
  813.           The procedure, ClrScr, will clear the screen.
  814.  
  815.           Where should you put ClrScr, in the main program, or in the
  816.           procedure?
  817.  
  818.           Right!  In the procedure, because clearing the screen is really
  819.           just a part of printing the entry screen.
  820.  
  821.           ##### DO:
  822.  
  823.           At the beginning of the Procedure, Print_Entry_Screen, add:
  824.                ClrScr;
  825.           
  826.  
  827.           Run the program.   Is the "leftover" message gone?
  828.  
  829.           NOTE: YOU SHOULD ALWAYS CLEAR THE SCREEN AS NEEDED.  EARLIER
  830.           VERSIONS OF TURBO CLEARED THE SCREEN AT THE BEGINNING OF THE
  831.           PROGRAM, BUT THAT IS THE TYPE OF THING YOU SHOULD NOT DEPEND ON.
  832.           WHAT IF THE NEXT VERSION CHANGES?
  833.  
  834.  
  835.                         3.  Getting Around the Screen - GotoXY.
  836.  
  837.           Cursor positioning is done with the predefined procedure, GotoXY.
  838.  
  839.           To find out how it works, try PROG15A.
  840.  
  841. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  87
  842.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  843.  
  844.  
  845.  
  846.           ##### DO:
  847.  
  848.           Examine PROG15A, then run it a few times using the following values
  849.           for X and Y:
  850.  
  851.                               X   Y
  852.                               1  20
  853.                              40   1
  854.                              70  23
  855.  
  856.           Does GotoXY work the way you expected?
  857.  
  858.           If that is the way you expected it to work, no problem.
  859.  
  860.           If you, like me, find that X and Y seem to be reversed, you can
  861.           either learn to use GotoXY as is, or write a procedure to make it
  862.           work the way you want it to!
  863.  
  864.           ##### DO:
  865.  
  866.           Load PROG15B.
  867.  
  868.           The following procedure has been added before the main BEGIN END
  869.           block:
  870.  
  871.           PROCEDURE Locate(X, Y : Integer);
  872.           BEGIN
  873.             GotoXY(Y, X);  { Note the reversed Y, X here }
  874.           END;
  875.  
  876.           Also notice that the GotoXY(X, Y) statement in the main program has
  877.           been changed to:
  878.                Locate(X, Y);
  879.  
  880.           Run the program several times using the values:
  881.  
  882.                             X    Y
  883.                             1   50
  884.                            10    1
  885.                            23   70
  886.  
  887.           My own choice is to use GotoXY as is, but if you work in both
  888.           Pascal and Basic at the same time, you might want some procedure
  889.           like Locate, to make the cursor positioning work the same in both.
  890.  
  891.           Now you know how to get around the screen.   You're on your way
  892.           toward friendly input screens!
  893.  
  894. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  88
  895.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  896.  
  897.  
  898.  
  899.           
  900.  
  901.           TURBO-LESSON 16.  REAL NUMBERS
  902.  
  903.           OBJECTIVES - In this lesson, you will learn about:
  904.  
  905.           1.  Range of Real Numbers
  906.           2.  Input/Output of Real Numbers
  907.           3.  Calculations with Real Numbers
  908.           4.  Calculations with Integers and Real Numbers
  909.  
  910.  
  911.                                1.  Range of Real Numbers
  912.  
  913.           For business processing (dollar amounts), and scientific
  914.           processing, integers alone are not adequate.  Decimal numbers and
  915.           sometimes numbers in scientific notation are needed.
  916.  
  917.           TURBO provides Real Numbers with 11 significant digits of precision
  918.           in the range:  1E-38 to 1E+38.   TURBO also has several other types
  919.           of Real numbers with other ranges and precision.
  920.  
  921.           ##### DO:
  922.  
  923.           Set the lower window to Output and run PROG16.
  924.  
  925.           Enter 444.333222111 and examine the result presented in scientific
  926.           notation.
  927.  
  928.           How many digits are retained before the E?  (I counted 11,
  929.           including digits on both sides of the decimal point.)
  930.  
  931.           This is the 11 significant digits of precision.  Note that the last
  932.           1 you entered was dropped.
  933.  
  934.  
  935.                            2.  Input/Output of Real Numbers.
  936.  
  937.           You will need to know what to expect with various combinations of
  938.           input and output of real numbers.
  939.  
  940.           ##### DO:
  941.  
  942.           Run PROG16.
  943.  
  944.           Enter 444.333222111 and study the various outputs.
  945.  
  946.           When the real number, A, is output without any formatting,
  947.           scientific notation is used:
  948.           
  949.                4.4433322211 are the significant digits.
  950.                E+02   means multiply by 10 to the 2nd power to get the
  951.           number.
  952.  
  953. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  89
  954.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  955.  
  956.  
  957.  
  958.           If you want the number presented in some other form, you can add
  959.           the :w:d formatting to the name of the variable to print.
  960.           
  961.                :w   Width of the print field
  962.                :d   Decimal positions to print
  963.  
  964.           WriteLn(A:10:2);  This statement would print the number, A, in a
  965.           print field 10 characters wide, with 2 decimal positions.
  966.  
  967.           Look at the outputs on the screen again.  The formats used are
  968.           printed at the left.  Square brackets are printed to show the width
  969.           of the field and where the number is printed (left or right-
  970.           justified).
  971.  
  972.           :-w   Width of print field, left-justify the number.
  973.  
  974.           Notice the use of the minus sign to force printing of the number at
  975.           the left of the field.
  976.  
  977.           ##### DO:
  978.  
  979.           Trace the program.
  980.  
  981.           What happens to the total width of the print field when numbers are
  982.           printed left-justified?
  983.  
  984.           ##### DO:
  985.  
  986.           Run PROG16 with 3.4567 as input.
  987.  
  988.           What happens when a print format is specified which will not hold
  989.           all of the significant digits?   Is the number rounded?
  990.  
  991.  
  992.                           3.  Calculations with Real Numbers.
  993.  
  994.           ##### DO:
  995.  
  996.           In PROG16 change the ReadLn(A) to ReadLn(B).
  997.  
  998.           Add after ReadLn(B):
  999.           
  1000.                A := B + C;
  1001.  
  1002.           The result printed will be the sum of B, which you enter, and C
  1003.           which is a constant, -2.0.
  1004.  
  1005.           Run the program with 2.34 as input.
  1006.  
  1007.           Are the results as expected?
  1008.  
  1009.           In scientific calculations, very large, and very small numbers are
  1010.           sometimes needed.  Can you enter these in a convenient form without
  1011.           a long string of zeros?
  1012.  
  1013. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  90
  1014.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1015.  
  1016.  
  1017.  
  1018.           The radius of the earth is 6370000 meters.
  1019.                This is the same as      6.37 times 100000
  1020.                Which is the same as     6.37 times 10 to the 6th power
  1021.                Which may be entered as  6.37E6 or 6.37E06 or 6.37E+6 or
  1022.           6.37E+06.
  1023.  
  1024.           ##### DO:
  1025.  
  1026.           Change ReadLn(B) back to ReadLn(A) and remove the next statement,
  1027.           A:=B+C;
  1028.  
  1029.           ##### DO:
  1030.  
  1031.           Run the program with 6.37E6 as input.
  1032.  
  1033.           Try the other 3 forms listed above.
  1034.  
  1035.           Are the results the same in all cases?
  1036.  
  1037.           ##### DO:
  1038.  
  1039.           Run the program with 6370000 as input.
  1040.  
  1041.           Does it make any difference whether the number is input in
  1042.           scientific notation or in the usual form?
  1043.  
  1044.           ##### DO:
  1045.  
  1046.           Run the program with 6.37E7 as input.
  1047.  
  1048.           What happens when the result is too large for the format?  (Count
  1049.           the character positions used to print A:10:2.  Include the decimal
  1050.           point as one position).
  1051.  
  1052.           ##### DO:
  1053.  
  1054.           Try 6.37E18 as input.
  1055.  
  1056.           For future reference, remember this possibility where the number is
  1057.           too large.  Several numbers printed on the same line may not be in
  1058.           the right place if one number is too large and "pushes" the others
  1059.           farther right on the print line.
  1060.  
  1061.           ##### DO:
  1062.  
  1063.           Run the program with 6.37E-6 as input.
  1064.  
  1065.           Notice the unformatted output is correct, but the formatted output
  1066.           shows nothing but zeros.
  1067.  
  1068.           The result, A, is 0.00000637, with significant digits too far to
  1069.           the right to show up in the formatted output.
  1070.  
  1071.           ##### DO:
  1072.  
  1073. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  91
  1074.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1075.  
  1076.  
  1077.  
  1078.           Change the WriteLn format, A:10:2 to A:10:6.  (Notice there are two
  1079.           A:10:2's in the statement, the first is a screen message, the
  1080.           second is the actual format.)
  1081.  
  1082.           Run the program with the following values:
  1083.           
  1084.                6.37E6,  6.37E-6,  6.37E-4
  1085.  
  1086.  
  1087.                    4.  Calculations with Integers and Real Numbers.
  1088.  
  1089.           What happens when you mix Integers and Real numbers in
  1090.           calculations?
  1091.  
  1092.           ##### DO:
  1093.  
  1094.           Get a copy of the original PROG16 and make the following changes:
  1095.  
  1096.           Change ReadLn(A); to ReadLn(I);
  1097.  
  1098.           Change the prompt to Write('Enter an Integer: ');
  1099.  
  1100.           Add after the ReadLn statement:
  1101.           
  1102.                J := I + C;
  1103.  
  1104.           Run the program.
  1105.  
  1106.           What results did you get?
  1107.  
  1108.           The "Type mismatch" refers to J.
  1109.  
  1110.           Since the calculation involves both an integer, I, and a real
  1111.           number, C, the result cannot be stored in an integer variable.  If
  1112.           the result had significant digits after the decimal, they would be
  1113.           lost when stored as an integer.
  1114.  
  1115.           ##### DO:
  1116.  
  1117.           Change the calculation to:
  1118.           
  1119.                A := I + C;
  1120.  
  1121.           Run the program.
  1122.  
  1123.           NOTE: ON YOUR OWN, YOU MAY WANT TO EXPERIMENT WITH THE WRITELN
  1124.           FORMATS.  CHANGE THE FORMATS TO VARIOUS VALUES AND TRY THEM WITH A
  1125.           VARIETY OF INPUTS.
  1126.  
  1127. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  92
  1128.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1129.  
  1130.  
  1131.  
  1132.           
  1133.  
  1134.           A CHALLENGE
  1135.  
  1136.           One of the objectives stated at the beginning of these lessons was
  1137.           to "provide the basics you need to get started with Pascal and
  1138.           build your confidence and enthusiasm to continue learning on your
  1139.           own".
  1140.  
  1141.           I want to challenge you now to write some programs of your own.
  1142.           Right away!  Don't wait--get started.  If you make mistakes, Turbo
  1143.           Pascal has some great tools to help you correct them.
  1144.  
  1145.           You may be surprized how soon you will be writing very useful
  1146.           programs.
  1147.  
  1148.           Watch for the next TURBO-LESSONS.  Be sure to let me know what
  1149.           lessons you would like:  More Turbo Pascal, QuickBasic, C, word
  1150.           processors, spreadsheets, something else?
  1151.  
  1152.           Thanks for your support of TURBO-LESSONS and Shareware.
  1153.