01-Tcp2103-Problem Solving

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

INTRODUCTION TO PROGRAMMING

Chapter 1 :
Problem Solving
INTRODUCTION TO PROGRAMMING

Objectives

By the end of this lesson, student should be able to


1. List 5 steps in solving programming problem
2. Define the IPO based on the given problem
3. Define what is algorithm and how it is different
from program code
4. Develop algorithm based on the given problem
statement.
INTRODUCTION TO PROGRAMMING

Problem Solving Techniques

Problem

Define the problem


Understand & Decide what
analyze a problem information we have to
work with
What result should be

Solution Algorithm : step by step


procedure for solving a
problem.
INTRODUCTION TO PROGRAMMING

Problem example

Problem 1 : You and some friends want to have a


party on Saturday night, and you need to plan and
prepare for it.

Problem 2 : You feel thirsty and want to buy a drink


from the nearest vending machine.
INTRODUCTION TO PROGRAMMING

Solution Example : the algorithm

• Problem 1
1. Call guests and keep a count of the ones who can
come
2. Make a shopping list and buy food.
3. Clean apartment
4. Move furniture
5. Welcome guests
6. Have the party
7. Say goodbye to leaving guests.
8. Clean up apartment and move furniture back.
INTRODUCTION TO PROGRAMMING

Solution Example : the algorithm

• Problem 2
1.0 Find nearest vending machine
2.0 Insert coins
• If coins inserted >= price
• Choose beverage
• Go to 3.0
• Else
• Go to 2.0
3.0 Have change
4.0 Give beverage
INTRODUCTION TO PROGRAMMING

Problem Solving – Step by step

Define the problem

Formulate mathematical
model

Develop an algorithm

Write the code for the


problem

Test your program


INTRODUCTION TO PROGRAMMING

Define the problem


INTRODUCTION TO PROGRAMMING

1. Define the problem

• Input
• What kind of input is there?

• Process
• What tasks do the objects perform on this data?

• Output
• What should my output look like?
INTRODUCTION TO PROGRAMMING

1. Define the problem

• Problem example

1. Computes the sum of 2 integer numbers

2. Compute and print the average of a set of data


values.

3. Computes the area of a circle


INTRODUCTION TO PROGRAMMING

1. Define the problem (IPO)

1. Computes the sum of 2 integer numbers

Objects : number1, number2 & Sum


INTRODUCTION TO PROGRAMMING

1. Define the problem (IPO)

2. Compute and print the average of a set of data


values.

Objects : data, sum, count & average


INTRODUCTION TO PROGRAMMING

1. Define the problem (IPO)

3. Computes the area of a circle

Objects : radius, area & circle


INTRODUCTION TO PROGRAMMING

2. Formulate a mathematical model

• Most technical problems can be modeled


mathematically
• An existing model must be chosen or a new one
developed if no standard mathematical model
exists.
INTRODUCTION TO PROGRAMMING

2. Formulate a mathematical model

1. Computes the sum of 2 integer numbers


Sum = number1 + number2

2. Compute and print the average of a set of data values.


Average = (data1 + data2 + data3) / 3

3. Computes the area of a circle


Area = radius x radius x 
INTRODUCTION TO PROGRAMMING

3. Develop an Algorithm

• Describes how a problem is solved in terms of the


actions to be executed
• Specifies the order in which the actions should be
executed
• Helps programmer plan a program before writing
it in a programming language.
• A program must be systematically and properly
design before coding begins.
• The design process results in the construction of an
algorithmn.
INTRODUCTION TO PROGRAMMING

3. Develop an algorithm

• Definition :
• A set of detailed, unambiguous and
ordered instructions develop to describe
the processes necessary to produce the
desired output from a given input.
• Is written in simple English and is not a
formal document
INTRODUCTION TO PROGRAMMING

3. Develop an algorithm

1. Computes the sum of 2 integer numbers

1. Read in two integer numbers


2. Compute the sum using the following formula:
sum = number1 + number2
3. Display the sum
INTRODUCTION TO PROGRAMMING

3. Develop an algorithm

2. Compute and print the average of a set of data


values.

1. Read in the numbers


3. As long as the data values exist, add the next data
value to the sum and add 1 to the count.
4. To compute the average, divide the sum by the
count.
5. Display the average.
INTRODUCTION TO PROGRAMMING

3. Develop an algorithm

3. Computes the area of a circle

1. Read in the radius


2. Compute the area using the following formula :
area = radius x radius x 
3. Display the area
INTRODUCTION TO PROGRAMMING

4. Write the code for the problem

• algorithm cannot be read by the computer


• It first must be written in a programming language
• A compiler will then convert your program code
into machine code which the computer can
understand
INTRODUCTION TO PROGRAMMING

5. Test your program

• If the program compiles correctly, use a simple


set of test values (values that would allow the
result to be computed by hand or calculator) to
verify that the result is what you expected.
• If the results seem valid, test the program with
a variety of "real" data sets.
INTRODUCTION TO PROGRAMMING

exercise

In a group of 2, define the problem and write an algorithm in order to


solve the following problem.
1. Go back to your home town by bus.
2. Prepare 8 pieces of sandwich.
3. Evaluate the number entered by user whether it is larger than 100 or
not. If yes display message “The number is over the limit”.
4. Buy a book at a bookstore.
5. Calculate sum of three numbers and display the result.
INTRODUCTION TO PROGRAMMING

Review Questions

• What are the five steps in solving problem? State


your answer based on programming context.
• What is the meaning of algorithm?
• Is algorithm the same as a program code?
• What is the different between programming code
and machine code?
INTRODUCTION TO PROGRAMMING

Exercise

1. Formulate a mathematical model, and develop an


algorithm for each of the following problems.
a. Find the average of a set of ten numbers.
b. Convert a set of Fahrenheit temperatures to Celsius
temperatures and print a table of the temperatures.
c. Given the values of the base and height of a triangle,
calculate the area of the triangle. Repeat this calculation
for 10 pairs of values.
INTRODUCTION TO PROGRAMMING

Guide

To convert Fahrenheit temperatures into


Celsius:
• Begin by subtracting 32 from the Fahrenheit
number.
• Divide the answer by 9.
• Then multiply that answer by 5.

TC = (TF – 32) x 5/9


INTRODUCTION TO PROGRAMMING

Guide

To convert Celsius temperatures into


Fahrenheit:
• Begin by multiplying the Celsius temperature by
9.
• Divide the answer by 5.
• Now add 32.

TF = TC x 9/5 + 32
INTRODUCTION TO PROGRAMMING

The End

You might also like