Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Example: Grocery Store

In a grocery store a fast calculation is needed to count the number and type of coins that make up the change remaining from a dollar, so it is a great help to have this logic programmed into the cash register. The following program accepts from the keyboard the price of the purchase (for the sake of simplicity, this is assumed to be less than one dollar) and produces as output the number of quarters, dimes, and nickels remaining from a dollar bill. The program is an application of the integer operators DIV and MOD.

{ ------------------------------ figure 2-3 ------------------------------ }
{ Grocery Store }
PROGRAM Grocery(INPUT,OUTPUT);
VAR
 Change, TotalPrice,
 Dollars, Quarters, Dimes, Nickels, Cents:INTEGER;
BEGIN
 WRITE('Enter the total-price in cents: ');
 READLN(TotalPrice);
 Change:= 100 - TotalPrice;
 { Quarters }
  Quarters:= Change DIV 25;
  Change:= Change MOD 25;
 { Dimes }
  Dimes:= Change DIV 10;
  Change:= Change MOD 10;
 { Nickels }
  Nickels:= Change DIV 5;
  Change:= Change MOD 5;
 { Cents }
  Cents:= Change;
 WRITELN('The change is:');
 WRITELN(Quarters,' Quarters');
 WRITELN(Dimes,' Dimes');
 WRITELN(Nickels, ' Nickels');
 WRITELN(Cents, ' Cents')
END.

A sample run of the program gives the following:

Enter the total-price in cents: 22 ----> Type "22" and press Enter
The change is:
3 Quarters
0 Dimes
0 Nickels
3 Cents

Drill 2-1

Modify the last program to accept any amount of money as total-price (including fractions of a dollar) and any amount of cash as amount-paid. The program should read the amount-paid and the total-price, and display the change in bills of different denominations, quarters, dimes, nickels, and cents.

Turbo Pascal Additional Functions

Turbo Pascal has a considerable number of additional arithmetic functions. Of these functions, you will especially need two of them:

FRAC(n) returns the fractional portion of the real number “n”
INT(n) returns the integer portion of the real number “n”

For example:

    WRITELN(FRAC(8.22):2:2);      produces 0.22
    WRITELN(INT(8.22)2:2);  produces 8.00

Both functions return real numbers.

You can make use of these functions in Drill 2-1.

Another couple of functions are used to generate random numbers:

RANDOM(n) returns a random integer between 0 and the integer “n” (the zero is included).
RANDOM returns a real random number between 0 and 1 (the zero is included).

Try these two statements:

    WRITELN(RANDOM:2:2);
    WRITELN(RANDOM(n));

where “n” is an integer variable readout from the keyboard.

Use the two statements in a program and look at the results for several runs. They should be different in each run.

Drill 2-2

Write the Pascal expressions for the following:

1.  The quadratic equation: Ax2 + Bx + C
2.  The determinant: B2 - 4AC
3.  The square root of the determinant
4.  The absolute value of the determinant
Then, write a program to produce the roots of the equation according to the input values of A, B, and C. Use test values for A, B, and C that give real roots. Typical values are:
A=1, B=2, C=1, give the solution: X1= X2= -1.00
A=1, B=4, C=2, give the solution: X1= -0.59, X2= -3.41

2-4 The Character Type: Char

The CHAR type is used to store a single character in Pascal. You can declare a variable of the type CHAR as in the following example:

    VAR
      SingleLetter: CHAR;

In the main body of the program (between BEGIN and END.) you may assign a single character to the variable “SingleLetter” like this:

    SingleLetter:= 'A';

As is clear from this example, a constant literal of the type CHAR must be exactly one character, included in single quotes:

    'A' '3' '*' '$' ' '

In order to represent a single quotation (or apostrophe) as a character constant, use two single quotes like this:

    WRITELN ("Hi There")

You can use the output statements WRITELN or WRITE to display a character constant or a character variable:

    WRITELN('A');
    WRITELN(SingleLetter);

The character set is internally represented by a one-byte integer code. The universally used code for small computers is the ASCII code (American Standard Code for Information Interchange). The ASCII code includes 256 characters from 0 to 255 (see Appendix A). The first half of the ASCII code (from 0 to 127) is standard on all personal computers. It includes the following characters:

  The uppercase letters (A-Z): ASCII 65 to 90
  The lowercase letters (a-z): ASCII 97 to 122
  The digits (0-9): ASCII 48 to 57

The code also contains punctuation characters and control characters.

The second half of the ASCII code is not standard and is implemented differently on different machines.

The relative sequence of a character in the ASCII set is called the ordinal number.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.