Uploaded by Abdullah Rizwan

1- Introduction to Problem Solving

advertisement
INTRODUCTION
Programming Fundamentals (CS118) Fall 2018
Abeeda Akram
What is a computer?
[Norton] A Computer is an electronic device that processes data, converting it into
information that is useful to people.
[Wikipedia] A Computer is a programmable device, usually electronic in nature, that can
store, retrieve and process data.
Limitations of Computer
• The computer can do nothing without being told what to do.
• A computer is not intelligent.
• It cannot analyze a problem and come up with a solution.
• A human must
1. Analyze the problem
2.
3.
Develop the instructions for solving the problem (the program),
Then ask the computer to carry out these instructions
3
Advantage of using a computer
• Once a solution is written for the computer, it can repeat the solution
• very quickly
• consistently,
• again and again, for different situations and data.
4
What is Computer Science?
Computer science is the study of
1.
Problems
2.
Problem-solving
3.
The solutions that come out of the problem-solving process.
Given a problem, a computer scientist’s goal is to develop an algorithm for
solving a problem..
• An algorithm is a precise sequence of instructions for solving a problem.
5
Steps of Problem Solving
1. Understand the Problem
2. Formulate a Model
3. Develop an Algorithm
4. Write the Program
5. Test the Program
6. Evaluate the Solution
Problem: Draw the Hut
Algorithm-Solution
1. Draw a square frame
2. Draw a triangular roof
3. Draw a door
Making a peanut butter and jam sandwich
Solution !!!
• Collect your ingredients.
• Spread peanut butter evenly onto one slice of bread using a knife.
• Spread jelly or jam evenly onto the other slice of bread.
• Press the two slices of bread together.
• Cut the sandwich.
• Enjoy your easy and yummy looking sandwich!
Programming Language
• A programming language is an artificial language designed to express
computations that can be performed by a computer.
Programming
languages can
Create programs
that control the
behavior of a
machine
Express
algorithms
precisely
Can be used as a
mode of human
communication
9
10
Swap two Numbers
With a third location.
30
50
Without using a third location.
30
50
Pseudocode
• Calculate Area of rectangle?
• How do you write pseudocode?
Breadth
Length
Area
Step 1: Start
Step 2: Read length, breadth
Step 3: Compute the product of length and breadth
Step 4: Set the result to area
Step 5: Display area
Step 6: Stop
Circumference of Rectangle ?
What to add in last Pseudocode to calculate circumference?
PSEUDO CODE
Step 1: Start
Step 2: Read length, breadth
Step 3: Compute the product of length and breadth
Step 4: Set the result to area
Step 5: Compute the addition of length and breadth
Step 6: Set the result to temp
Step 7: Compute the product of 2 and temp
Step 8: Set the result to perimeter
Step 9: Display area, perimeter
Step 10: Stop
Temperature Conversion
• Fahrenheit (F) to Celsius (C)
C = (F - 32) * 5/9
• Celsius to Fahrenheit(F)
F = (C * 9/5) + 32
• Kelvin (K) to Fahrenheit (F)
F = (K - 273.15) * 9/5 + 32
• Kelvin (K) to Celsius (C)
C= K - 273.15
16
PSEUDO CODE
Step 1: Start
Step 2: Read temperature in Fahrenheit (F)
Step 3: Subtract 32 and
Step 4: Set the temperature
Step 5: Divide 5 by 9 and set value
Step 6: Compute the product of temperature and value
Step 7: Set the result to product
Step 8: Display result
Step 9: Stop
GUESS A NUMBER
Suppose your friend guesses a number between 1 and 16. You have to
find out the number. The only answer your friend will provide you is
whether your answer is less than, greater than or equal to the number
he has guessed. If you are lucky you will guess the number in a
minimum of 1 try….but what if you are not.
What is the maximum number of attempts that you need to guess the
number correctly.
• Think about this:
• What will be your answer if you do exhaustive search??
• Can you cut down the search???
The Fox, the Goose, and the Beans
A farmer with a fox, a goose, and a sack of beans needs to cross a river. The farmer
has a rowboat, but there is room for only the farmer and one of his three items.
The fox cannot be left alone with the goose, or the fox will eat the goose.
Likewise, the goose cannot be left alone with the sack of beans.
How does the farmer get everything across the river?
19
Download