Google Answers Logo
View Question
 
Q: computers pascal programming ( Answered,   0 Comments )
Question  
Subject: computers pascal programming
Category: Computers > Programming
Asked by: vergita-ga
List Price: $50.00
Posted: 10 Dec 2002 06:39 PST
Expires: 09 Jan 2003 06:39 PST
Question ID: 122361
Pascal program

Design and implement a simple program to calculate the travel cost
claimed on a single travel expense form. The program will perform the
following main functions – display welcome screen, enter details
relating to the claimant and vehicle, calculate travel cost, display a
report and display an exit screen.

Display welcome screen.

The program will start by displaying a user-friendly welcome screen
stating the programs purpose.

Enter details relating to claimant and vehicle.

Following the welcome screen, the program will display a screen in
order to prompt for and input the details below:

·	Name of claimant
·	Address of claimant 
·	Registration number of vehicle for which mileage is claimed 
·	Engine capacity of vehicle for which mileage is claimed 
·	Number of journeys claimed 

Calculate travel cost.

For each journey, the program will display a screen in order to prompt
for and input the mileage. The miles travelled will be added to a
running total. If the engine capacity of the vehicle is less than or
equal to 2.0 litres, the travel expense is calculated at 10pence per
mile. If the engine capacity is greater than 2.0 litres, the travel
expense is calculated at 15 pence per mile.

Display report 

After calculating the travel cost, the program will display a screen
in order to show information relating to a travel expense claim form.
The report will start with a heading. Following the heading, the
report will show the following:

·	Name and address of person making the claim 
·	Vehicle registration number and engine capacity 
·	Travel cost 

Display exit screen.

The program will end by displaying a user friendly exit screen.
Answer  
Subject: Re: computers pascal programming
Answered By: leapinglizard-ga on 10 Dec 2002 10:55 PST
 
I used the Clrscr procedure from the crt package to clear the screen
for each page of text, having gathered from your description that this
is the desired effect. I compiled and tested this program using the
Free Pascal Compiler. Code follows.


program claim;
uses crt;
var
    line, name, address, registration: string;
    capacity: real;
    i, journeys, miles, part, cost, pounds, pence: integer;
begin
    Clrscr;
    writeln('**********************************');
    writeln('*  Welcome to the claim program! *');
    writeln('*  Let''s calculate your travel   *');
    writeln('*  cost, shall we?               *');
    writeln('**********************************');
    write('press enter to continue...');
    readln(line);

    Clrscr;
    write('Enter name of claimant: ');
    readln(name);
    write('Enter address of claimant in a single line: ');
    readln(address);
    write('Enter registration number of vehicle: ');
    readln(registration);
    write('Enter vehicle''s engine capacity in litres: ');
    readln(capacity);
    write('Enter number of journeys claimed: ');
    readln(journeys);

    miles := 0;
    for i := 1 to journeys do
    begin
        Clrscr;
        write('Enter mileage of journey #', i, ': ');
        readln(part);
        miles := miles + part;
    end;
    if capacity <= 2.0 then
        cost := 10 * miles
    else
        cost := 15 * miles;
    pounds := cost div 100;
    pence := cost mod 100;

    Clrscr;
    writeln('***************************');
    writeln('* Details of travel claim *');
    writeln('***************************');
    writeln('Name:                 ', name);
    writeln('Address:              ', address);
    writeln('Vehicle registration: ', registration);
    writeln('Engine capacity:      ', capacity:2:1);
    writeln('Travel cost:          ', pounds, ' pound(s), ', pence, '
pence');
    write('press enter to continue...');
    readln(line);

    Clrscr;
    writeln('****************************************');
    writeln('*  Thanks for using the claim program. *');
    writeln('*  Goodbye!                            *');
    writeln('****************************************');
end.


If you have trouble compiling the program or if you'd like to have it
modified, please give me a chance to meet your needs before you assign
a rating.

Cheers,

leapinglizard

Request for Answer Clarification by vergita-ga on 10 Dec 2002 13:34 PST
i would like to know the meanings of the contants and variables used
in this pascal program and also their functions in the program.

and also i would like to know what is what is 'div' and 'mod' that is
in the lower part of the pascal program, and what is 'i' and explain
the function of it in the pascal program.

Clarification of Answer by leapinglizard-ga on 10 Dec 2002 14:21 PST
Here are descriptions of each variable.

line
    a string variable used to store the user's input
    after each 'press enter to continue...'; we never
    actually use the value of this variable

name
    a string variable used to store the user's name

address
    a string variable used to store the user's address

registration
    a string variable used to store the vehicle's
    registration number

capacity
    a real variable used to store the vehicle's engine
    capacity in litres; in the travel-claim report, we
    display it to one decimal place of precision

i
    an integer variable used to iterate over the number
    of journeys undertaken by the user

journeys
    an integer variable used to store the number of
    journeys undertaken by the user

miles
    an integer variable used to store the total number
    of miles traveled by the user

part
    an integer variable used to store the number of
    miles traveled in each journey; as the user enters
    each part, we add it to the total

cost
    an integer variable used to store the total cost of
    the user's journey in pence

pounds
    an integer variable used to store the number of
    pounds in the total cost

pence
    an integer variable used to store the number of
    pence, after deduction of pounds, in the cost

The variable i is used in the line "i := 1 to journeys do", which is
the head of a loop. This line says that the integer i should take on
all values between 1 and the value of the variable journeys,
inclusively. For example, if the value of journeys is 3, then the
variable i will take on each of the values 1, 2, and 3 as the body of
the loop, which is enclosed between "begin" and "end;", is executed.

The "div" operator returns the quotient resulting from an integer
division. For example, "7 div 3" yields 2, since 2*3=6 is the greatest
integer less than or equal to 7. The "mod" operator returns the
remainder of an integer division. For example, "7 mod 3" yields 1,
since 7-6=1. In the above program, we use div and mod to calculate the
number of pounds and pence in the total cost of a travel claim. If,
for example, the total is 105 pence, we have 105 div 100 = 1 and 100
mod 100 = 5, so we can tell the user that it amounts to 1 pound and 5
pence.

Cheers,

leapinglizard

Request for Answer Clarification by vergita-ga on 12 Dec 2002 05:52 PST
COULD YOU POSSIBLY PROVIDE ME WITH TH 'DATA TABLE'FOR THIS PROGRAM AND
THE 'PSEUDO CODES'OF THE PROGRAM BEFORE IT WAS PUTTEN INTO PASCAL
FORM.

BASICALLY SHOW THE WAY YOU MADE THE PROGRAM AND HOW YOU DEVELOPED IT.

Clarification of Answer by leapinglizard-ga on 12 Dec 2002 07:51 PST
I'm not sure what you mean by "data table"--I earned a degree in
computer science without ever hearing of such a thing--but I can tell
you in plain English how I chose my variables.

I knew that in order to implement the "press enter to continue"
feature, I would need a string into which I read the user's input, so
I declared the variable called line.

The problem statement asks us to read and display the user's name,
address, and registration number, so I declared string variables for
each of these. (It's not clear what form a registration number will
take, but since we're not required to perform any numerical
manipulation, a string will always work.)

The statement tells us that engine capacity is measured in litres, and
since there are engines that displace 1.6 and 2.3 and 4.2 litres, for
example, I knew that capacity would have to be a real variable.

The number of journeys is clearly a discrete quantity, and I decided
that the number of miles in a journey would be rounded off. Thus, I
use integer variables to store both of these. I foresaw the need to
iterate over the number of journeys, and declared the integer variable
i for this purpose. The variable called part is used to store the
user's input each time he is asked to enter the number of miles
traveled on a given journey.

As for the integer variables cost, pound, and pence, I use the first
of these to store the total cost of travel in pence, while the last
two serve to store the number of pounds in the total and the number of
remaining pence, respectively. In my previous clarification, I explain
how to derive these figures. The variables pound and pence are only
necessary because I decided to display the travel cost with the
following instruction.

    writeln('Travel cost:          ', pounds, ' pound(s), ',
             pence, 'pence');

If you don't like this style of display, feel free to replace it with
the following.
 
   writeln('Travel cost:          ', (cost/100.0):4:2);

If you do make the replacement, it wouldn't hurt to remove the
variables pound and pence, as well as the lines containing the div and
mod instructions.

That covers all the variables.

I don't know what style of pseudocode you've been taught, and you must
realize that there are no universal conventions. Nonetheless, I'm
confident you'll understand my semi-structured description of the
program. If the following resembles Pascal, it's because most
pseudocode is inspired by the Algol family of programming languages,
of which Pascal is one.

PROGRAM claim
line, name, address, registration: STRING
capacity: REAL
i, journeys, miles, part, cost, pounds, pence: INTEGER
    display welcome message
    name <- claimant's name
    address <- claimant's address
    registration <- vehicle registration number
    capacity <- vehicle's engine capacity
    journeys <- number of journeys undertaken
    miles <- 0
    for i <- 1 to journeys
        part <- number of miles in journey #i
        miles <- miles + part
    if capacity <= 2.0 then
        cost <- 10 * miles
    else
        cost <- 15 * miles
    display details of travel claim
    display parting message

You shouldn't find it difficult to adapt this to your notation of
choice.

leapinglizard
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy