0% found this document useful (0 votes)
16 views17 pages

Week2 2

Computer programming slides

Uploaded by

basharat.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

Week2 2

Computer programming slides

Uploaded by

basharat.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

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
– 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

• Testing (Output Window):


– A program run
Enter the fabric size in square meters > 2.00
The fabric size in square yards is 2.392000E+00
Case Study 2: Square & Cube
• Problem Statement
– Write a program that calculates the square root, square,
and cube of a number
• Is this statement clear & sufficient for analysis &
design steps?
– NO
Case Study 2: Square & Cube
• Modified Problem Statement
Write a program that gets an integer number
from the user, calculates the square root,
square and cube of the number and then
prints the number,the square root, the
square, and the cube. If the number is
negative, the square root should not be
evaluated. An error message should be
written instead. The output should be
labeled with suitable headings.
Case Study 2: Square & Cube
• Analysis
– Input
• Get a number from the user

– Processing & formulas


• Calculate the square root, the square, & the cube
– SquareRoot = sqrt(Number)
– Square = Number x Number.
– Cube = Number x Square.

– 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)

1.Pseudocode for initialization


Clear screen
Write program purpose
2. Pseudocode for Input
Read Number
3. Pseudocode for Processing
SquareRoot = Sqrt(Number)
Square = Number * Number
Cube = Square * Number
4. Pseudocode for Output
Write “Number”, “Sq. Root”,“Square”, “Cube”
Write Number, SquareRoot, Square, Cube
5. Pseudocode for termination
Write Goodbye message
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.
Ideas from Students
• A program for keeping track of my grades in a course
– Easy
• A program like those in graphic calculators that allows
entering the data and will automatically display
mean/average, the frequency, the median, the mode …
– Easy, we’ll do a simple version of it
• A special dictionary that will enable me to write
customized terms and the computer will automatically
recognize the word I meant and transform it by itself. Ex:
dvlp would stand for development. That would be very
time saving while writing reports.
– Not hard, but in this course we don’t learn everything we need
• A program for planning courses: what courses do I take
next and what courses do I have left.
– A planning and scheduling problem, can be arbitrarily hard
• A program for managing my time and schedule
– If you know an algorithm for doing that, let me know….
Case Study 3: Keeping Track of
Grades in a Course
• A program for keeping track of my grades
in a course
– Is this specified clearly enough?
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
and give me the option to enter the new data.
When I exit the program, all the data (old and
new) will be stored to a file so that it can be
retrieved next time the program is run.
Case Study 3:
Begin Analysis
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
and give me the option to enter the new data.
When I exit the program, all the data (old and new)
will be stored to a file so that it can be retrieved
next time the program is run.
Case Study 3: I/O Analysis
• Input:
• Existing data from file
• New data from keyboard

• 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?

You might also like