Uploaded by samson william

I NEED ANSWERS

advertisement
Introduction To Problem Solving
Tutorial 1
Problem solving in this context simply means finding a solution for a task or set of tasks which need to be
done. It does not suggest that anything is wrong. A problem can be as simple as wanting to add a set of
numbers, to something complicated like calculating salaries for 5000 workers in a company with different
work schedules.
Before you start coding a program on a computer, you need to know how to solve the problem. To do
this, you have to (1) analyze the problem; (2) arrive at a suitable solution using an algorithm; and (3) test
the algorithm to ensure that there are no LOGICAL ERRORS.
NOTE: A program with logical errors will work perfectly BUT produce incorrect results. Look at the
following example: (4 + 3) x 2 will give the correct result of 14. If you forget to add the parentheses ()
4+3x2, your result will be 10. So the program worked, and it did what it was supposed to do. But the
instructions were incorrect and so the output is also incorrect. You must keep this in mind always.
Analyzing the problem
To analyze the problem, we will use an IPO Chart. This allows us to break the problem down into three
components – Input, Output and Processing.
So, you first start by carefully reading the problem and identifying what the program is required to output
(display), then identifying what goes into the program to produce the output. These are listed in the table
as seen below.
Input {2}
Processing
Num1
Num2
Num3
The program here simply calculates the total of three numbers.
Output {1}
Total
The next step is to decide how to move from input to the output. So we can now work on the processing.
The processing steps start with receiving the input, followed by the calculation(s) and finally the display
of results. Look at the table below.
Input
Num1
Num2
Num3
Processing {3}
1 Get three numbers (Num1, Num2, Num3)
2 Add the three numbers (Num1+Num2+Num3)
And store the result in Total.
3 Display Total.
Output
Total
VARIABLE – A storage place in memory whose content / value can change.
Page
To proceed, there are some key words which you need to get accustomed to:
1
The next stage is to simulate a program using PSEUDOCODE. Pseudocode looks like program code but IT
IS NOT PROGRAM CODE. This will be introduced in the next lesson.
CONSTANT – A data item with a name and value which remains the same throughout the execution of a
program.
SEQUENCE- each program / solution must have sequence. That means a set of steps in a precise order.
Note that every step must be explicitly stated, since the computer cannot assume anything, and will do
only what it is instructed to do. Here is an example: To make a telephone call you must follow this order














Pick up the handset
Listen for dial tone
Press 2
Press 4
Press 6
Press 4
Press 3
Press 8
Press 3
Press 1
Press 5
Press 9
Listen for answer at other end of line
Speak to the person
For a human, the list appears shorter because we make several assumptions. For the computer, you have
to clearly state each instruction without any ambiguity; i.e. exact and single instructions.
What you need to know about variables








A variable (and a constant) must have a unique name
Names must be meaningful – they should identify the data they represent. E.g. Salary for moneys
paid to a worker; Price – money value of an item.
Must begin with a letter but can contain both letters and digits
Must be a single word / term
Where more than one word is involved, they must be joined to make one
Long words must be abbreviated (e.g. First Operating cost can become OpCost1)
When words are joined, each word should begin with a capital letter (e.g. CashFlowAmt)
Only special character that can be used is the underscore (_)
EXERCISE TO BE COMPLETED
Here is a number of small problems for you to analyze using IPO Charts. Type the problem statement
before drawing the IPO chart.
YOU ARE EXPECTED TO WORK ALONE ON THESE PROBLEMS
Page
1. Create a program which will accept the ages of four boys and calculate their total age and average
age. The program must display both the total age and the average age.
2
EACH STUDENT’S VARIABLE NAMES MUST BE DIFFERENT FROM THAT OF THE OTHER STUDENTS
Page
3
2. Create a program which will accept the length and width of a rectangle and display the area.
3. Create a program which will accept the distance travelled by a car and the time it took to reach
its destination and calculate the (average) speed at which it travelled. {Note that speed = distance
over time}.
4. Create a program which will accept the amount of money paid on a product as well as the price.
It will then calculate the balance owed to the store. It will then display the amount paid, the
original price and the balance.
5. Create a program which will accept the length, width and depth of a rectangular pool and display
the volume of water it can hold.
6. Create a program which will accept the number of hours worked by an employee as well as the
rate at which he is to be paid. It will then calculate the wages due. The program will then display
the hours worked, the wages rate and the wages owed to the employee.
7. Create a program which will accept the number of days worked and calculate the number of
weeks. The program must then show the number of days and the number of weeks calculated.
Download