Week2 2
Week2 2
Introduction to Computer
Programming
Violetta Cavalli-Sforza
Week 2, Lecture 2
Case Study 1
Converting Units of Measurements
• Problem Statement
You work in a store that sells
imported fabric, measured in square
meters; The store customer wants to
know the equivalent amount in
square yards. You need to write a
program that performs the
conversion.
Case Study 1
Converting Units of Measurements
• Analysis:
– Input:
• Fabric size in square meters (SqMeters).
– Output:
• Equivalent amount in square yards (SqYards).
– Relevant Formulas:
• The relationship between sq. meter & sq. yards
• (1 m2 = 1.196 y2)
Case Study 1
Converting Units of Measurements
• Design:
– Algorithm:
1. Read the fabric size in m2.
2. Convert size to yd2 using the formula
(1 m2 = 1.196 y2).
3. Display fabric size in yd2.
– Refinement of step2:
2.1 Fabric size in yd2 = 1.196 * fabric size in m2
Case Study 1
Converting Units of Measurements
• Implementation
– Program header
• Give a logical name for your program
– Program Name: ConvertSqMtoSqYd;
• The name of the source code file of the program can be
different from the logical name
– File name: CONVMYSQ.PAS
– TurboPascal limits file names to 8 characters
– Declaration section
• Memory cells used in your program
• Constant declaration: use constants for unchanged
values
• Variable declaration: use variables for values that
change
– Program body
• Data processing takes place here
Case Study 1
Converting Units of Measurements
• Implementation (Using Turbo Pascal)
program ConvertSqMtoSqYd;
{Convert square meters to square yards }
const MetersToYards = 1.96; {conversion
constant}
var SqMeters, {input - fabric size
in meters}
SqYards : real; {output - fabric size
in yards}
begin
{Read the fabric size in square meters.}
WriteLn ('Enter the fabric size in square meters >');
ReadLn(SqMeters);
{Convert the fabric size to square yards.}
SqYards := MetersToYards * SqMeters;
{Display the fabric size in square yards. }
WriteLn ('The fabric size in square yards is ', SqYards)
end.
Case Study 1
Converting Units of Measurements
– Output
• Write labels for Number, Square root, Square, and Cube
• Write values of Number, Square root, Square, and Cube
(This is one way of doing it…)
Case Study 2: Square & Cube
• Design (Algorithm)
– Declaration section
const
HEAD = ‘Number Sq.Root Square Cube’; { Heading }
var
Number, { input- number being processed }
Square, { output - square of the number }
Cube : integer; { output- cube of the number }
SquareRoot : real; {output- square root of the
number}
– Program body
Case Study 2: Square & Cube
• Implementation: First draft
– Program Body
• Enclosed between begin & end.
• Write algorithm as comments
begin {Program body}
{1. Initialize}
{2. Read Number }
{3. Calculations / Evaluate formulas}
{4. Output heading & values}
{5. Terminate}
end.
Case Study 2: Square & Cube
• Implementation: Second draft
Add Pascal statements in corresponding places
• Output:
• Existing data plus new data to file
Case Study 3: Processing Analysis
• Processing:
– Display existing data
• Decide: about how to display the data.
E.g. Table with headers:
Assessment My Score Max Score
HW#1 8 10
…. … ….
– Prompt for new data:
• Data: Name of assessment, My score, Max score
• Decide:
– prompt for data one by one or all 3 at a time?
– prompt for data for a single assessment or multiple assessments?
If multiple assessments, how do you indicate you are done?
– Store old and new data:
• Decide:
– Overwrite the old file?
– Make a new version of the file?
• Decide:
– Display all the data the program knows about before exiting?
Case Study 3:
Revised Problem Statement
• A program that will let me enter and store the score that I
receive for each assessment in the course, the
maximum score that I could have obtained in that
assessment, and the name of the assessment. When I
run the program, it will:
– Retrieve and display the scores it already knows in a table with
suitable headers
– Give me the option to enter data for one or more additional
assessments or to exit
– If I choose to enter data for an assessment it will prompt me for
all three values on one line
– After I enter the values it will again give me the option to enter
new data or to exit.
• When I exit the program
– all the data (old and new) will be stored to a new file so that it
can be retrieved next time the program is run.
– all the data it has for me will be shown.
Are you starting to think that you should use a spreadsheet instead?