Está en la página 1de 4

A note on Problem Solving

Introduction
If a problem is stated in the form of an equation, the solution requires nothing more than
executing a set of well-known algorithms.
However, real problems are expressed in words rather than algebraic symbols. The
problem is how to get started on the path that leads from the conditions given in the
problem to the solution to the problem.
Understanding the problem would seem to be the best first step in the direction of finding
solution to the problem. Yet, this first step is more often overlooked.
Understand the problem first
There are two extremely important elements in understanding the problem. Here they are:
Be clear about what information you have to work with.
Be clear about what information you are trying to discover.
When the statement of a problem becomes more complex, it is worthwhile to make a list
of the information given in the problem description, so nothing is overlooked or
misunderstood.
Similarly, re-stating what solution to the problem requires can make the end point of the
problem clearer.
Example-1
Problem: If a man is 6' 1.5" tall, what is his height in centimeters?
Solution:
1. Information given to us: The man's height is a physical measure of 6 feet and 1.5
inches.
2. Objective: Find equivalent value of the given height in metric system, i.e., in
centimeters.
One can have serious trouble solving this problem if he or she does not know the
relationship between feet, inches and centimeters. We can ask someone and find that 1
inch = 2.54 centimeters. The point here is to ask and find any missing information.
The second step requires the knowledge of conversion between inch and centimeter.
Developing pseudocode or creating flow chart
This step will help organize the process before making the program that communicates to
the computer. Pseudocode is a natural language version of computer code. There is no
formal syntax and it cannot be understood by the computer or compiler. If one has a well-
written pseudocode or flowchart, it can easily be converted to a program using any
programming language.
Pseudocode solution
1. Begin
2. Get the portion of height in feet (use variable name: height_feet)
3. Get the portion of height in inches (use variable name: height_inch)
4. Set height_in_inch = height_feet * 12 + height_inch
5. Set height_in_cm = height_in_inch * 2.54
6. Display height_in_cm
7. End
Note the use of keywords such as GET or SET. A variable is a placeholder, any name
that holds a value. In programming languages, a variable name is composed of a set of
letters in alphabet, digits, and the underscore character, starting with a letter. So, valid
variable names to use might be: x, aaaa, total123, h_feet, h_inch, etc. SPACES are not
allowed in a variable name.
Example-2
Problem: How much does 50 gallons of water weigh?
Solution:
This problem would be trivial if the weight of one gallon of water were known; however
this is not commonly known. However, we can provide one more piece of information to
see if that helps. 3.785 liters in one gallon and one liter has a volume of 1000 cm
3
and
it weighs 1000 grams. 1000 grams = 1 kilogram = 2.2 pounds.
Information given to us:
1. 50 gallons of water to be weighed.
2. 3.785 liters = 1 gallon (given on milk container)
3. 1 liter has a volume of 1000 cm
3
.
4. 1 cm
3
of water has a mass of 1 gram.
5. 1000 grams equals 1 kilogram (kg).
6. At certain standard, 1kg of water = 2.2 pounds.
Objective: Find the weight of 50 gallons of water.
One can have serious trouble solving this problem if they do not know the relationship
between weight and volume.
Developing pseudocode or creating flow chart
Pseudocode solution
1. Begin
2. Set liter = 50 * 3.875
3. Set cm_cubed = liter * 1000
4. Set grams = cm_cubed *1
5. Set kilograms = grams / 1000
6. Set pounds = kilograms / 2.2.
7. Display pounds.
8. End
Here is how the numbers work, so we could check our calculations. I hope the math is not
overwhelming. Most of our calculations for the homework assignments will not be this
bad!
2. liter = 50 * 3.875 = 193.75
3. cm_cubed = 193.75 * 1000 = 193750
4. grams = 193750 * 1 = 193750
5. kilograms = 193750 / 1000 = 193.75
6. pounds = 193.75 * 2.2 = 426.25
By the way, is there a faster way to solve this problem? The answer is usually YES! Do
you have a better method of writing the pseudocode to do the same thing?
Another example:

Before you write a program, its a good idea to start with writing an algorithm for the
problem you are trying to solve. An algorithm is a step-by-step recipe for solving a
problem. It can be written in the forms of a flowchart and/or a pseudo-code. When
writing an algorithm, always select descriptive names for your variables, clearly state
every step, and validate your pseudo-code.

For example, lets write a pseudocode for a problem to calculate and display the price of
a book that is discounted at a specific rate.

a. Do you understand the problem? If not, read it again. If you still do not understand the
problem, there is no reason for continuing to the next step.

b. What are my input variables? bookPrice, discountRate, newBookPrice

c. Here is the pseudo-code:

1. start
2. prompt for the price of the book
3. get the bookPrice
4. prompt for the discount rate
5. get the discountRate
6. set newBookPrice = bookPrice bookprice * discountRate/100
7. display The price of the book after the discount = newBookPrice
8. end

Note how step 6 above specifies a formula to calculate the newBookPrice. A common
mistake is to have step 6 looking like the following where we are being ambigious.

6. calculate the newBookPrice

d. Validate your pseudo-code. Assume the price of the book is $100, and the discount rate
is 10%. The price of the book after the discount should be 100 (100 * 10 / 100), which
is $90. Does your pseudo-code also produce the value of $90? If yes, you have
successfully validated your pseudo-code.

Regardless of the language (PL/SQL, Java, C++, etc.) you are using to write your
programs, starting with an algorithm makes your programming job much easier. The idea
is to be able to write an algorithm in such a way that any programmer can use it to write
their program in the language of their choice.

También podría gustarte