0% found this document useful (0 votes)
46 views

3 ProblemSolving PDF

This document discusses problem solving and programming fundamentals. It provides a case study on calculating the amount owed for cupcakes. It outlines the software development method (SDM) as: 1) specify requirements, 2) analyze the problem, 3) design an algorithm, 4) implement the algorithm, 5) test and verify, and 6) document. Each step is then explained in more detail using the cupcake problem as an example.

Uploaded by

Hamza Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

3 ProblemSolving PDF

This document discusses problem solving and programming fundamentals. It provides a case study on calculating the amount owed for cupcakes. It outlines the software development method (SDM) as: 1) specify requirements, 2) analyze the problem, 3) design an algorithm, 4) implement the algorithm, 5) test and verify, and 6) document. Each step is then explained in more detail using the cupcake problem as an example.

Uploaded by

Hamza Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAMMING

FUNDAMENTALS
WEEK # 3 PROBLEM SOLVING
CASE STUDY

1 Marriam Daud
Dept of Computer Science & IT
The University of Lahore
[email protected]
INTRODUCTION TO PROBLEM SOLVING
 Programming is a problem solving activity. When
you write a program, you are actually writing an
instruction for the computer to solve something
for you.

 Problem solving is the process of transforming the


description of a problem into a solution by using
our knowledge of the problem domain and by
relying on our ability to select and use appropriate
problem-solving strategies, techniques and tools.

2
CASE STUDY: YUMMY CUPCAKE

 Problem: You are required to calculate the amount to be


paid by a customer buying cupcakes.

3
SOFTWARE DEVELOPMENT METHOD (SDM)
 For programmer, we solve problems using Software
Development Method (SDM), which is as follows:
1. Specify the problem requirements.
2. Analyze the problem.
3. Design the algorithm to solve the problem.
4. Implement the algorithm.
5. Test and verify the completed program.
6. Documentation

4
1. REQUIREMENT SPECIFICATION
 Specifying the problem requirements requires you to state
the problem clearly and to gain the understanding of what
to be solved and what would be the solution.
 When specifying problem requirement, we ask ourselves
the following questions:
 What the problem is?
 What the solution should provide?
 What is needed to solve it?
 If there are constraints and special conditions?

5
YUMMY CUPCAKE

Problem: You are required to calculate the amount to be


paid by a customer buying cupcakes.
 What the problem is.

 What the solution should provide.

 What is needed to solve it.

 If there are constraints and special conditions.

6
2. PROBLEM ANALYSIS
 Analyzing the problem require us to identify the following:
 Input(s) to the problem, their form and
the input media to be used
 Output(s) expected from the problem,
their form and the output media to be
used
 Special constraints or conditions (if any)
 Any formulas or equations to be used

7
YUMMY CUPCAKE
 Input?
 Quantity of the cupcake purchased (integer)
 Price per cupcake (RM, float)

 Output?
 Total amount to be paid by the customer (float)

 Constraint/condition?
 Quantity purchased must be more than zero
 Price per cupcake must be more than zero (it is not free)
 We assume that the price given is the standard price to all
cupcakes

 Formula/equation?
 Amount to pay = quantity of cupcake x price per cupcake

8
3. DESIGNING ALGORITHM

 Designing algorithm to solve the problem requires


you to develop a list of steps, arranged in a
specific logical order which, when executed,
produces the solution for a problem.
 Using top-down design (also called divide and
conquer):
 You first list down the major tasks
 For each major task, you further divide it into
sub-tasks (refinement step)
 When you write algorithm, write it from the
computer’s point of view.
9
DESIGNING ALGORITHM CONT..
 An algorithm must satisfy these requirements:
 It may have an input(s)
 It must have an output(s)
 It should not be ambiguous (there should not be
different interpretations to it. Every step in algorithm
must be clear as what it is supposed to do)
 It must be general (it can be used for different inputs)
 It must be correct and it must solve the problem for
which it is designed
 It must execute and terminate in a finite amount of
time
 It must be efficient enough so that it can solve the
intended problem using the resource currently available
on the computer

10
YUMMY CUPCAKE
Major Task:
1. Read the quantity of cupcake purchased
2. Read the price per cupcake
3. Calculate total amount to pay
4. Display the total amount to pay

However, looking at the above algorithm, we can still further refine step 3,
by introducing the formula to calculate the amount to pay.

After Refinement:
1. Read the quantity of cupcake purchased
2. Read the price per cupcake
3. Total amount to pay = quantity of cupcake x price per cupcake
4. Display the total amount to pay

11
4. IMPLEMENTATION
 The
process of implementing an algorithm by writing a
computer program using a programming language (for
example, using C language)
 Theoutput of the program must be the solution of the
intended problem
 Theprogram must not do anything that it is not
supposed to do
(Think of those many viruses, buffer overflows, trojan horses,
etc. that we experience almost daily. All these result from
programs doing more than they were intended to do)

12
5. TESTING AND VERIFICATION
 Program testing
is the process of executing a
program to demonstrate its correctness
 Program verification is
the process of ensuring
that a program meets user-requirement
 After theprogram is compiled, we must execute
the program and test/verify it with different inputs
before the program can be released to the public
or other users (or to the instructor of this class)

13
6. DOCUMENTATION
 Writing description that explain what the program
does.
 Can be done in 2 ways:
 Writing comments between the line of codes
 Creating a separate text file to explain the program

 Important not only for other people to use or modify


your program, but also for you to understand your
own program after a long time (believe me, you will
forget the details of your own program after some
time ...)

14
DOCUMENTATION CONT…
 Documentation is so important because:
 You may return to this program in future to use the whole of
or a part of it again.
 Other programmer or end user will need some information
about your program for reference or maintenance
 You may someday have to modify the program, or may
discover some errors or weaknesses in your program
 Although documentation is listed as the last stage of
software development method, it is actually an
ongoing process which should be done from the very
beginning of the software development process.

15

You might also like