home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-08 | 37.0 KB | 1,153 lines |
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 72
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 12. A FUNCTION TO DETECT ERRORS
-
- OBJECTIVES - In this lesson, you will learn about:
-
- 1. Error detection
- 2. Using a predefined function
- 3. Writing your own function
-
-
- 1. Error Detection.
-
- In the previous lesson, you found that some input values caused a
- problem. The function used in PROG11A calculated the cube of a
- number entered. If the result was outside the range of valid
- integers the result is wrong but Pascal doesn't alert you to the
- problem.
-
- If you write programs for others to use, you will have to deal with
- the problem of ERRORS.
-
- There are several approaches to error handling:
-
- (1) Error detection before it happens - prevent the occurrence of
- the error.
-
- (2) Error detection when it happens - take corrective action.
-
- (3) Ignore the error - let the program bomb!
-
- The 3rd is not usually acceptable - but may be o.k. in the early
- stages of program development since you, the programmer, can fix
- the problem. You may also find alternate ways to program for the
- same result while avoiding the possibility of the error.
-
- The 2nd, error detection when it happens, will be explored later.
- Input/Output errors are typical examples of this class of errors.
-
- In this lesson, you will find ways to detect a problem and prevent
- its occurrence.
-
- ##### DO:
-
- In PROG12, examine FUNCTION Cube.
-
- The function has been expanded to detect integers which are too
- small or too large to produce a valid integer cube. If a number is
- entered which would cause an error, the result is set to 0 instead
- of the erroneous result.
-
- ##### DO:
-
- Run PROG12 several times using the following values for input:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 73
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- 3, -3, 31, 32, -32, -33, 0
-
- Were all the results as expected? The inputs, 32 and -33, would
- produce cubes out of the range of valid integers, so these two
- should have given results of 0.
-
- What about 0 as an input? Did you get the correct result?
-
- Can you determine whether a result of 0 is valid (0 input) or
- invalid (input of < -32 or > 31)?
-
- Later in this lesson you will write your own function to deal with
- this problem!
-
-
- 2. Using a Predefined Function.
-
- Pascal provides many functions and procedures which are already
- defined.
-
- Some advantages of using predefined subprograms:
-
- (1) The subprogram is already debugged.
-
- (2) The subprogram doesn't take up room in your program.
-
- (3) You can spend your time on more interesting programming, no
- need to "reinvent the wheel".
-
- To use a predefined function, you have to know:
-
- (1) The name of the function
-
- (2) What goes in (what values do you provide as input?)
-
- (3) What comes out (what result is associated with the function
- name?)
-
- The absolute value function, ABS, can be used in PROG12 to
- illustrate the use of a predefined function.
-
- What goes in What comes out
-
- 3 --------> [ABS] --------> 3
-
- -5 --------> [ABS] --------> 5
-
- The absolute value function provides a positive number of the same
- magnitude as the positive or negative number input to the function.
-
- ##### DO:
-
-
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 74
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Add the following statement after the ReadLn(No) statement in
- PROG12:
- WriteLn('Absolute value: ', ABS(No) );
-
- Run the program several times with both positive and negative
- numbers.
-
- NOTE: THERE ARE OFTEN SEVERAL WAYS TO DO THE SAME THING IN
- PROGRAMMING. YOU SHOULD BE LOOKING FOR WAYS TO DECIDE WHICH OF
- SEVERAL PROGRAMMING SOLUTIONS IS BETTER IN A GIVEN CASE.
-
- The next exercise demonstrates a way to use ABS in the error
- detection problem.
-
- ##### DO:
-
- Change the first line of the IF statement in the FUNCTION Cube to:
- IF ABS(Number) > 31
-
- Test the program with several values.
-
- How would you decide whether to use the function, ABS, in the main
- program or in the function, Cube?
-
- Is the action accomplished by ABS of interest to you in getting the
- cube of a number? If not, it should probably be pushed out of the
- main program and into the subprogram.
-
-
- 3. Writing Your Own Function.
-
- Now, it's your turn. Another approach to the error detection
- problem uses a second function, which you are about to write!
-
- Give the function the name: Has_Valid_Cube
-
- The function will have one input: Number of type Integer
-
- The type of the function will be: Boolean
-
- What the function does:
-
- If Number would produce a valid cube, the function, Has_Valid_Cube,
- will have the value, TRUE.
-
- If Number would produce an error, Has_Valid_Cube will have the
- value, FALSE.
-
- ##### DO:
-
- Write the function, Has_Valid_Cube. Place it before the main
- program. It can be either before or after the function, Cube.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 75
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Look at FUNCTION Cube if you need help with the form of the
- function declaration or the IF statement needed.
-
- ##### DO:
-
- In the main program, replace the WriteLn which references Cube with
- the following:
-
- IF Has_Valid_Cube (No)
- THEN
- WriteLn('The cube is: ', Cube(No) )
- ELSE
- BEGIN
- WriteLn('The cube of ',No,' is outside the integer range');
- WriteLn('in this version of Pascal.');
- END;
-
-
- Test the program with several positive and negative values and
- values which would cause erroneous cubes.
-
- (If you have trouble writing the function, PROG12A is available as
- a sample. Don't check PROG12A until you have given it a try on
- your own!)
-
- Are there any other improvements you want to make to PROG12?
-
- FUNCTION Cube still checks for invalid inputs. Is this still
- necessary?
-
- ##### DO:
-
- Change FUNCTION Cube so that it does no error checking, just
- calculates the cube of the number input.
-
- Test the program with several values including 0.
-
- Note that there is no longer any ambiguity when the result is a
- cube of 0.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 76
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 13. STRINGS
-
- OBJECTIVES - In this lesson, you will learn about:
-
- 1. Strings
- 2. String replacement statement
- 3. Predefined string function, LENGTH
-
-
- 1. Strings.
-
- You have already seen some Pascal strings in the WriteLn statements
- of earlier lessons.
-
-
- WriteLn('This is a string.');
-
- It is often convenient to store strings as variables or constants.
-
- A string constant may be defined in the CONST section:
-
- CONST String_1 = 'TURBO-LESSONS';
-
- String variables must be declared in the VAR section. The form of
- the declaration is:
-
- VAR First_Name : String[12];
-
- This sets up storage for a variable named First_Name which can
- store a string up to 12 characters long.
-
-
- 2. String Replacement Statement.
-
- The replacement statement for strings is:
-
- String_Name := (string expression);
-
- ##### DO:
-
- Examine PROG13. Notice the following:
-
- A string constant, S_Test is given the value 'Test String' in the
- CONST declaration section.
-
- Several string variables are defined in the VAR section.
-
- ##### DO:
-
- Run the program.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 77
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- What happens when an attempt is made to store too long a string in
- a string variable?
-
- ##### DO:
-
- Add the following to the program:
-
- S5 := S_Test;
- WriteLn(S5);
-
- Run the program.
-
- How many characters of S5 are printed?
-
- ##### DO:
-
- Modify WriteLn(S5)
- to: WriteLn('[', S5, ']');
-
- Run the program. How many characters of S5 are printed?
-
- DEBUGGING NOTE: When working with strings, you may find it helpful
- to print some kind of marker before and after a string to help
- "see" the occurrences of the character, blank.
-
- You have seen what happens when storing 'Test String' in too short
- a variable: S3 holds 'Tes', S8 holds 'Test Str'.
-
- What happens when a string is stored in a variable that is larger
- than needed? Are blanks added?
-
- ##### DO:
-
- Modify the WriteLn(S14) to bracket S14 (like you did above with S5)
- and run the program.
-
- How many characters of S14 were printed?
-
- Were extra blanks added? (More on this later.)
-
- ##### DO:
-
- Look at PROG13A. (Don't forget you can Zoom to full screen with F5
- to get a better look at the program in the Edit window.)
-
- WriteLn(S8[I]);
-
- Notice the use of the square brackets in the statement above. This
- is a way to refer to a specific character in a string.
-
- S8[2] means the 2nd character in the string, S8.
-
- NOTE: SQUARE BRACKETS ARE USED IN TWO DIFFERENT WAYS WITH STRINGS.
- WHEN DECLARING VARIABLES, THE BRACKETS ENCLOSE THE MAXIMUM LENGTH
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 78
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- OF THE STRING. IN PROCESSING STATEMENTS, THE NUMBER IN THE
- BRACKETS DESIGNATE A PARTICULAR CHARACTER IN A STRING.
-
- ##### DO:
-
- Run the program, using 2 as position number.
-
- Try 5 as an input. What character was stored as the 5th character
- of S8?
-
- The string stored in S8, 'TURBO', is 5 characters long but S8 is 8
- characters long.
-
- What characters, if any, are stored in S8[6], S8[7], and S8[8]?
-
- ##### DO:
-
- Run the program with input values of 6, 7, and 8.
-
- What characters were printed?
-
- ##### DO:
-
- Terminate the program by entering -1 as position number.
-
- ##### DO:
-
- Change the first statement as the first statement in the BEGIN END
- block from S8:= S_Test;
- to S8 := '12345678';
-
- Run the program again, using 6, 7, and 8 as input.
-
- What do you conclude about "unused" positions in a string?
-
- Before you are prompted to enter the "position number", S8 is
- printed.
-
- Do positions 6, 7, 8 of the string print? Why?
-
-
- 3. Predefined String Function, LENGTH.
-
- Because the length of a string is often needed in processing, the
- function, LENGTH, has been provided for that purpose.
-
- ##### DO:
-
- Add the following statement after the UNTIL statement:
- WriteLn('Length of string: ', LENGTH(S8) );
-
- Run the program.
-
- ##### DO:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 79
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Just before the END, insert:
-
- FOR I := 1 to LENGTH(S8) DO
- WriteLn('Position ', I:2, ': ',S8[I]);
-
- Run the program.
-
- ##### DO:
-
- Change the statement
-
- S8 := '12345678'
- to: S8 := 'OK';
-
- Run the program.
-
- Also try 'Wake Up' as a value for S8.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 80
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 14. INTRODUCTION TO PROCEDURES
-
- OBJECTIVES - In this lesson, you will learn about:
-
- 1. PROCEDURE declaration
- 2. Using a Procedure
- 3. Using Parameters
- 4. A counter with error checking
-
-
- 1. PROCEDURE Declaration.
-
- The PROCEDURE subprogram is similar to the FUNCTION subprogram
- introduced in an earlier lesson. The form of the declaration is:
-
- PROCEDURE Add(No_1, No_2 : Integer; VAR Sum : Integer);
-
- BEGIN
- Sum := No_1 + No_2;
- END;
-
- Add is the name of the Procedure.
-
- No_1, No_2, and Sum are integer variables called "parameters".
-
- VAR in front of Sum indicates that this parameter is a "two-way
- street". It can receive data from the calling program, and return
- data to the calling program. No_1 and No_2 can only receive data
- from the calling program.
-
- The BEGIN END block defines the processing performed by the
- procedure:
-
- Add the value in the memory location, No_1, to the value in memory
- location, No_2, and place the result in the memory location, Sum.
-
-
- 2. Using a Procedure.
-
- A reference to this procedure in the main program (or another
- procedure or function) would have the following form:
-
- Add(2, Count, Adjusted_Count);
-
- The procedure is "called" or utilized by simply using its name as a
- statement. (When you define a Procedure or Function, you are
- adding additional "statements" to your programming language.)
-
- Notice that there are three "parameters" here, and three in the
- Procedure declaration. The three here are associated with the ones
- in the declaration by position.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 81
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- The first parameter here, the integer, 2, provides input to the
- first parameter of the procedure, No_1.
-
- The second, the variable, Count, provides input to the second
- parameter of the procedure, No_2.
-
- The third, Adjusted_Count, provides input to the third parameter of
- the procedure, Sum.
-
- Since Sum is declared VAR, variable, it also provides output back
- to Adjusted_Count after the procedure does its calculation.
-
- In the Main Program In Procedure Add
-
- 2 ----------> No_1 { When Procedure }
- Count ----------> No_2 { is called }
- Adjusted_Count ----------> Sum { }
- --------------------------------------------------------------
- Adjusted_Count <---------- Sum { When Procedure ends }
-
- ##### DO:
-
- Inspect PROG14.
-
- ##### DO:
-
- Add the following to the watch window: Adjusted_Count, Count, Sum,
- No_1, No_2.
-
- ##### DO:
-
- Press F7 to begin tracing.
-
- Notice the Procedure variables, No_1, No_2, and Sum are "Unknown
- identifiers" at this point. Count and Adjusted_Count have
- "leftover" values--whatever happens to be in the memory assigned to
- these 2 variables.
-
- ##### DO:
-
- Trace the Write and ReadLn statements. Enter 4. Stop when the ADD
- statement is highlighted.
-
- Watch carefully what happens in the next step, when you trace the
- ADD statement. No_1, the first "parameter" in the Procedure ADD
- should receive the value 2, which is the first value listed in the
- ADD statement in the main program. No_2 should receive 4, the
- value of Count. Sum should receive the value of Adjusted_Count.
-
- ##### DO:
-
- Press F7 once to trace the ADD statement.
-
- ##### DO:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 82
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Trace the Sum:=No_1 + No_2 statement.
-
- Which variables changed?
-
- ##### DO:
-
- Trace the rest of the program. Watch the values of No_1, N0_2,
- and Sum when the program goes from the procedure ADD back to the
- main program.
-
- ##### DO:
-
- Add the following statement as the first statement in the main
- program:
-
- Adjusted_Count := 10;
-
- Run the program.
-
- Does it make any difference what value is stored in Adjusted_Count
- before Procedure Add is referenced? Look at the procedure - is
- Adjusted_Count used as an input for the calculation, or only as a
- result?
-
-
- 3. Using Parameters.
-
- You have already been using parameters, but in this section, you
- will see a little more of the power and flexibility of parameters.
-
- ##### DO:
-
- Change the Add statement in the main program to:
-
- Add(2, 2, Adjusted_Count);
-
- Run the program. Is the result as expected?
-
- ##### DO:
-
- Change the Add to:
- Add(Count, Count, Adjusted_Count);
-
- Run the program. Any surprizes?
-
- ##### DO:
-
- Change the Add to:
- Add(2, 3, 4);
-
- Run the program. What happened?
-
- The compiler refused to accept 4 as a variable identifier.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 83
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- The VAR preceding Sum in the procedure declaration puts a limit on
- the corresponding parameter in the calling program: it has to be a
- variable location to receive the value of Sum when the procedure
- finishes its calculation.
-
- The first two parameters in the procedure, No_1 and No_2, are used
- only for input to the procedure - they do not provide any output
- back to the corresponding parameters in the calling program.
-
- For this reason, the first two parameters in the calling program
- are less restricted. They can be constants, variables, or
- expressions, as long as they are of the same type, integer, as the
- corresponding parameters in the procedure.
-
- ##### DO:
-
- Change the Add statement:
-
- Add(4, 2 * Count + 5, Adjusted_Count);
-
- Run the program. Does the expression work o.k. as a parameter?
-
- ##### DO:
-
- Change the Add to:
- Add(Count, Count, Count);
-
- Also change the WriteLn to print the value of Count instead of
- Adjusted_Count.
-
- Run the program. Any problems?
-
-
- 4. A Counter with Error Checking.
-
- You could use the Procedure Add as a counter by using the following
- call:
-
- Add(Count, 1, Count);
-
- The procedure would add 1 to Count and put the result in Count.
-
- You could accomplish the same thing with the statement:
- Count := Count + 1;
-
- So why bother to use the procedure?
-
- What if Count reaches the upper limit of the integer range, 32767?
-
- In the main program you could expand the counting statement to:
-
-
- IF Count < 32767
- THEN
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 84
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Count := Count + 1
- ELSE
- WriteLn('Counter reached upper limit');
-
-
- PROGRAMMING NOTE: HOW DO YOU DECIDE WHAT GOES INTO THE MAIN
- PROGRAM AND WHAT TO PUT IN SUBPROGRAMS?
-
- FROM THE FOLLOWING DISCUSSION, TRY TO FORMULATE
- AT LEAST ONE RULE FOR MAKING THIS DECISION.
-
- This looks a little messy, maybe you should use a procedure to get
- this out of the main program.
-
- Not a bad reason, but there's a more important reason:
-
- Ask yourself, "How much of the processing in the IF statement above
- is of interest in the main program?"
-
- Probably only the fact that a count is being incremented. The
- error checking and how it is done is probably of little interest
- and just clutters up the main program.
-
- ##### DO:
-
- Write your own procedure to increment the counter, Count.
-
- Call the procedure, Increment.
-
- Check for the upper limit of the integer range. (The IF statement
- above would be one way.)
-
- Note that only one parameter is needed, preceded by VAR.
-
- ##### DO:
-
- In the main program, add the following to check out your procedure:
-
- FOR I := 1 to 10 DO
- BEGIN
- Increment(Count);
- WriteLn('Count = ', Count);
- END;
-
- Run the program using the following values as input for count:
-
- 0, 3, -34, 32760
-
- (PROG14A is provided in case you need help.)
-
- You could also write a procedure, Decrement, to decrease a counter.
- Note that the error checking would be checking the lower limit, -
- 32768, or perhaps 0 depending on how you intended to use the
- counter.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 85
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 15. INTERACTIVE SCREEN HANDLING
-
- OBJECTIVES - In this lesson, you will learn about:
-
- 1. Setting up a Data Entry Screen
- 2. Being nice to users - ClrScr
- 3. Getting around the screen - GotoXY
- 4. Screen messages and accepting user input
-
-
- 1. Setting up a Data Entry Screen.
-
- For most computer processing applications you will need to provide
- for entry of data. This is one of the points where your programs
- interact with the person using the program.
-
- How your programs are viewed by those using them will depend on how
- well you manage the user-computer interaction on the screen.
-
- In this lesson you will try some of the basic techniques of screen
- handling for data entry.
-
- NOTE: Some of the programs have the statement
-
- Repeat Until KeyPressed;
-
- as the last statement. This statement serves to keep the program
- from ending until you press a key to end the program. Anytime you
- see this statement at the end of a program, remember that you must
- press a key to end the program. The reason for adding this
- statement is to allow you to view the output screen after the
- program runs. Without it, the Edit screen covers up most of the
- output screen as soon as the program is done. In this lesson you
- need to see the whole output screen.
-
-
- ##### DO:
-
- Run PROG15.
-
- Take a look at the program to see how this screen was produced.
-
- ##### DO:
-
- Experiment with PROG15.
-
- Run the program after each of the following:
-
- (1) Add or delete spaces in the WriteLn statements to move the
- various items.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 86
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- (2) Line the prompts up on the left. You may want to keep the
- colons in a vertical column after you move the prompts.
-
-
- 2. Being Nice to Users - ClrScr.
-
- Pascal provides a predefined procedure to clear the screen.
-
- Screen interaction will go smoother if unnecessary items are
- removed when no longer needed.
-
- ##### DO:
-
- Add the following statement as the first statement in the main
- program:
-
- WriteLn('This is something leftover from previous processing');
-
- ##### DO:
-
- Run the program.
-
- How does the data entry screen look now?
-
- There are often things left on the screen that need to be cleared
- away for a better screen presentation.
-
- The procedure, ClrScr, will clear the screen.
-
- Where should you put ClrScr, in the main program, or in the
- procedure?
-
- Right! In the procedure, because clearing the screen is really
- just a part of printing the entry screen.
-
- ##### DO:
-
- At the beginning of the Procedure, Print_Entry_Screen, add:
- ClrScr;
-
-
- Run the program. Is the "leftover" message gone?
-
- NOTE: YOU SHOULD ALWAYS CLEAR THE SCREEN AS NEEDED. EARLIER
- VERSIONS OF TURBO CLEARED THE SCREEN AT THE BEGINNING OF THE
- PROGRAM, BUT THAT IS THE TYPE OF THING YOU SHOULD NOT DEPEND ON.
- WHAT IF THE NEXT VERSION CHANGES?
-
-
- 3. Getting Around the Screen - GotoXY.
-
- Cursor positioning is done with the predefined procedure, GotoXY.
-
- To find out how it works, try PROG15A.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 87
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- ##### DO:
-
- Examine PROG15A, then run it a few times using the following values
- for X and Y:
-
- X Y
- 1 20
- 40 1
- 70 23
-
- Does GotoXY work the way you expected?
-
- If that is the way you expected it to work, no problem.
-
- If you, like me, find that X and Y seem to be reversed, you can
- either learn to use GotoXY as is, or write a procedure to make it
- work the way you want it to!
-
- ##### DO:
-
- Load PROG15B.
-
- The following procedure has been added before the main BEGIN END
- block:
-
- PROCEDURE Locate(X, Y : Integer);
- BEGIN
- GotoXY(Y, X); { Note the reversed Y, X here }
- END;
-
- Also notice that the GotoXY(X, Y) statement in the main program has
- been changed to:
- Locate(X, Y);
-
- Run the program several times using the values:
-
- X Y
- 1 50
- 10 1
- 23 70
-
- My own choice is to use GotoXY as is, but if you work in both
- Pascal and Basic at the same time, you might want some procedure
- like Locate, to make the cursor positioning work the same in both.
-
- Now you know how to get around the screen. You're on your way
- toward friendly input screens!
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 88
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 16. REAL NUMBERS
-
- OBJECTIVES - In this lesson, you will learn about:
-
- 1. Range of Real Numbers
- 2. Input/Output of Real Numbers
- 3. Calculations with Real Numbers
- 4. Calculations with Integers and Real Numbers
-
-
- 1. Range of Real Numbers
-
- For business processing (dollar amounts), and scientific
- processing, integers alone are not adequate. Decimal numbers and
- sometimes numbers in scientific notation are needed.
-
- TURBO provides Real Numbers with 11 significant digits of precision
- in the range: 1E-38 to 1E+38. TURBO also has several other types
- of Real numbers with other ranges and precision.
-
- ##### DO:
-
- Set the lower window to Output and run PROG16.
-
- Enter 444.333222111 and examine the result presented in scientific
- notation.
-
- How many digits are retained before the E? (I counted 11,
- including digits on both sides of the decimal point.)
-
- This is the 11 significant digits of precision. Note that the last
- 1 you entered was dropped.
-
-
- 2. Input/Output of Real Numbers.
-
- You will need to know what to expect with various combinations of
- input and output of real numbers.
-
- ##### DO:
-
- Run PROG16.
-
- Enter 444.333222111 and study the various outputs.
-
- When the real number, A, is output without any formatting,
- scientific notation is used:
-
- 4.4433322211 are the significant digits.
- E+02 means multiply by 10 to the 2nd power to get the
- number.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 89
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- If you want the number presented in some other form, you can add
- the :w:d formatting to the name of the variable to print.
-
- :w Width of the print field
- :d Decimal positions to print
-
- WriteLn(A:10:2); This statement would print the number, A, in a
- print field 10 characters wide, with 2 decimal positions.
-
- Look at the outputs on the screen again. The formats used are
- printed at the left. Square brackets are printed to show the width
- of the field and where the number is printed (left or right-
- justified).
-
- :-w Width of print field, left-justify the number.
-
- Notice the use of the minus sign to force printing of the number at
- the left of the field.
-
- ##### DO:
-
- Trace the program.
-
- What happens to the total width of the print field when numbers are
- printed left-justified?
-
- ##### DO:
-
- Run PROG16 with 3.4567 as input.
-
- What happens when a print format is specified which will not hold
- all of the significant digits? Is the number rounded?
-
-
- 3. Calculations with Real Numbers.
-
- ##### DO:
-
- In PROG16 change the ReadLn(A) to ReadLn(B).
-
- Add after ReadLn(B):
-
- A := B + C;
-
- The result printed will be the sum of B, which you enter, and C
- which is a constant, -2.0.
-
- Run the program with 2.34 as input.
-
- Are the results as expected?
-
- In scientific calculations, very large, and very small numbers are
- sometimes needed. Can you enter these in a convenient form without
- a long string of zeros?
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 90
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- The radius of the earth is 6370000 meters.
- This is the same as 6.37 times 100000
- Which is the same as 6.37 times 10 to the 6th power
- Which may be entered as 6.37E6 or 6.37E06 or 6.37E+6 or
- 6.37E+06.
-
- ##### DO:
-
- Change ReadLn(B) back to ReadLn(A) and remove the next statement,
- A:=B+C;
-
- ##### DO:
-
- Run the program with 6.37E6 as input.
-
- Try the other 3 forms listed above.
-
- Are the results the same in all cases?
-
- ##### DO:
-
- Run the program with 6370000 as input.
-
- Does it make any difference whether the number is input in
- scientific notation or in the usual form?
-
- ##### DO:
-
- Run the program with 6.37E7 as input.
-
- What happens when the result is too large for the format? (Count
- the character positions used to print A:10:2. Include the decimal
- point as one position).
-
- ##### DO:
-
- Try 6.37E18 as input.
-
- For future reference, remember this possibility where the number is
- too large. Several numbers printed on the same line may not be in
- the right place if one number is too large and "pushes" the others
- farther right on the print line.
-
- ##### DO:
-
- Run the program with 6.37E-6 as input.
-
- Notice the unformatted output is correct, but the formatted output
- shows nothing but zeros.
-
- The result, A, is 0.00000637, with significant digits too far to
- the right to show up in the formatted output.
-
- ##### DO:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 91
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Change the WriteLn format, A:10:2 to A:10:6. (Notice there are two
- A:10:2's in the statement, the first is a screen message, the
- second is the actual format.)
-
- Run the program with the following values:
-
- 6.37E6, 6.37E-6, 6.37E-4
-
-
- 4. Calculations with Integers and Real Numbers.
-
- What happens when you mix Integers and Real numbers in
- calculations?
-
- ##### DO:
-
- Get a copy of the original PROG16 and make the following changes:
-
- Change ReadLn(A); to ReadLn(I);
-
- Change the prompt to Write('Enter an Integer: ');
-
- Add after the ReadLn statement:
-
- J := I + C;
-
- Run the program.
-
- What results did you get?
-
- The "Type mismatch" refers to J.
-
- Since the calculation involves both an integer, I, and a real
- number, C, the result cannot be stored in an integer variable. If
- the result had significant digits after the decimal, they would be
- lost when stored as an integer.
-
- ##### DO:
-
- Change the calculation to:
-
- A := I + C;
-
- Run the program.
-
- NOTE: ON YOUR OWN, YOU MAY WANT TO EXPERIMENT WITH THE WRITELN
- FORMATS. CHANGE THE FORMATS TO VARIOUS VALUES AND TRY THEM WITH A
- VARIETY OF INPUTS.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 92
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- A CHALLENGE
-
- One of the objectives stated at the beginning of these lessons was
- to "provide the basics you need to get started with Pascal and
- build your confidence and enthusiasm to continue learning on your
- own".
-
- I want to challenge you now to write some programs of your own.
- Right away! Don't wait--get started. If you make mistakes, Turbo
- Pascal has some great tools to help you correct them.
-
- You may be surprized how soon you will be writing very useful
- programs.
-
- Watch for the next TURBO-LESSONS. Be sure to let me know what
- lessons you would like: More Turbo Pascal, QuickBasic, C, word
- processors, spreadsheets, something else?
-
- Thanks for your support of TURBO-LESSONS and Shareware.
-