You are on page 1of 54

Problem Solving and

Programming Concepts
Problem Solving in Everyday Life
 People make decisions every day to solve problems that affect their lives.

 The problems may be as unimportant as what to watch on television or as


important as choosing a new profession.

 If a bad decision is made, time and resources are wasted.

 So it’s important that people know how to make decisions well.

 There are six steps that ensures the best decision.


Problem Solving Steps

i. Identify the problem.


ii. Understand the problem.
iii. Identify alternative ways to solve the problem.
iv. Select the best way to solve the problem from the list of alternative solutions.
v. List instructions that enable you to solve the problem using the selected
solution.
vi. Evaluate the solution.
Identify the Problem

 The first step toward solving a problem is to identify the problem.


 If you don’t know what the problem is, you cannot solve it.
Understand the problem
 You must understand what is involved in the problem before you can continue
toward the solution.
 This includes understanding the knowledge base of the person or machine for
whom you are solving the problem.
 If you are setting up a solution for a person, then you must know what that
person knows.
 A different set of instructions might have to be used depending on this
knowledge base.
Understand the problem
 For example, you would use a more detailed set of instructions to tell someone
how to find a restaurant in your city if he has a limited knowledge of the city than
if he knows it well.
 Knowing the knowledge base is very important since you cannot use any
instructions outside this base.
 You also must know your own knowledge base. You cannot solve a problem if
you do not know the subject.
 For example, to solve a problem involving calculus, you must know calculus; to
solve a problem involving accounting, you must know accounting.
Identify alternative ways to solve the
problem.

 Make a list of all possible alternate solutions to the problem.


 Alternate solution must be acceptable ones.
 You could go from Jalandhar to Jammu by way of New Delhi, but this would
probably not be an acceptable solution to your travel needs.
Select the best way to solve the problem from
the list of alternative solutions

 In this step, you need to identify and evaluate the pros and cons of each possible
solution before selecting the best one.
List instructions that enable you to solve the
problem using the selected
solution.

 No instruction can be used unless the individual or the machine can understand it.

 This can be very limiting, especially when working with computers.


Evaluate the solution

 To evaluate or test a solution means to check its result to see if it is correct, and
to see if it satisfies the needs of the person(s).
 For Example, When a person needs a piece of furniture to sleep on, buying her a
cot may be a correct solution, but it may not be very satisfactory.
 If the result is either incorrect or unsatisfactory, then the problem solver must
review the list of instructions to see that they are correct or start the process all
over again.
If any of these six steps are not
completed well,
the results may be less than desired.
Is there Anybody who do not Suffer from
any Problem in Daily Life???????
NO………….
 People solve problems daily at home, or work, or wherever they go. Problems
at home include such things as what to cook for dinner, which movie to see
this evening, which car to buy, or how to sell the house.
 At work, the problems might involve dealing with fellow employees, work
policies, management, or customers. The better the decisions an employee
can make, the more valuable that person will be to the company.
Example: Take the problem of what to do this evening.

1. Identify the problem. How do the individuals wish to spend the evening?
2. Understand the problem. With this simple problem, also, the knowledge base
of the participants must be considered. The only solutions that should be
selected are ones that everyone involved would know how to do. You probably
would not select as a possible solution playing a game of chess if the
participants did not know how to play.
Example: Take the problem of what to do this evening.
3. Identify alternatives.
a. Watch television.
b. Invite friends over.
c. Play video games.
d. Go to the movies.
e. Play miniature golf.
f. Go to the amusement park.
g. Go to a friend’s party.
The list is complete only when you can think of no more alternatives.
Example: Take the problem of what to do this evening.

4. Select the best way to solve the problem:


Weed out alternatives that are not acceptable, such as those that cost too much
money or do not interest one of the individuals involved.
Specify the pros and cons of each remaining alternative.
Weigh the pros and cons to make the final decision. This solution will be the best
alternative if all the other steps were completed well.
5. Prepare a list of steps (instructions) that will result in a fun evening.
6. Evaluate the solution. Are we having fun yet? If nobody is having fun, then the
planner needs to review the steps to have a fun evening
Types of Problems
 Problems do not always have straightforward solutions.
 Some problems, such as balancing a checkbook or baking a cake, can be solved
with a series of actions.
 These solutions are called algorithmic solutions.
 Once the alternatives have been eliminated, for example, and once one has
chosen the best among several methods of balancing the checkbook, the solution
can be reached by completing the actions in steps. These steps are called the
algorithm.
Types of Problems
 The solutions of other problems, such as how to buy the best stock or whether to
expand the company, are not so straightforward.
 These solutions require reasoning built on knowledge and experience, and a
process of trial and error.
 Solutions that cannot be reached through a direct set of steps are called heuristic
solutions.
Types of Problems
 The problem solver can use the six steps for both algorithmic and heuristic
solutions.
 However, in step 6, evaluating the solution, the correctness and appropriateness
of heuristic solutions are far less certain.
 It’s easy to tell if your completed checkbook balance is correct and satisfactory,
but it’s hard to tell if you have bought the best stock.
 With heuristic solutions, the problem solver will often need to follow the six
steps more than once.
Problem Solving With Computers

 Program: Set of instructions that’s is used to perform some specific task.

 Solution: instructions that must be followed to produce the best results.

 Results: means the outcome or the completed computer-assisted answer.

 Program: means the set of instructions that make up the solution after they have

been coded into a particular computer language.


Problem Solving With Computers

 Computers are built to deal with algorithmic solutions, which are often difficult
or very time consuming for humans.
 People are better than computers at developing heuristic solutions.
 Example, Solving a Calculus Problem or alphabetizing 10,000 names are very
easy task for computer.
 But the problem of how to throw a ball or how to speak English is not. The only
difficulties lies in programming.
Problem Solving With Computers

 The field of computers that deals with heuristic types of problems is called
artificial intelligence.
 Artificial intelligence enables a computer to do things like the human can do.
 As a result, the computer’s problem-solving abilities are similar to those of a
human being.
Constants and Variables
 The computer uses constants and variables to solve problems.
 A constant is a value never changes during the processing of all the instructions
in a solution.
 Constants can be any type of data—numeric, alphabetical, or special symbols.
 During the execution of the program, this constant is given a value and then is
referred to by its name.
 For example, the value of PI does not change, it would be a constant and defined
within the program.
Constants and Variables
 The only way to change the value of the constant is to change the program.
 Many name conventions says that constants can be given names containing all
capital letters in order to easily distinguish them from variables.
 The value of a variable may change during processing.
 It is also called as Identifiers.
 Variables can be of any data type, just as constants can.
 A programmer must give a name to each variable value used in a solution.
Naming Conventions for Constants and Variables

There are many reasons to follow a naming convention:


It allows several programmers to work on the same project without the problem
of conflicting variable and module names.
It allows programs to be easily read because there is only one consistent name for
a variable.
Naming conventions allow the code to be easily maintained. Programmers spend
most of their time on software maintenance, not development.
The software should perform more efficiently by using consistent naming of
modules and variables.
Rules for Naming and Using Variables

 It is important to understand the difference between the name of a variable and


the value of a variable.
 The name is the label the computer uses to find the correct memory location; the
value is the contents of the location.
Rules for Naming and Using Variables

1) Name a variable according to what it represents, that is, Hours for hours
worked, PayRate for rate of pay, and so on.
2) Do not use spaces in a variable name.
3) Start a variable name with a letter.
4) Do not use a dash (or any other symbol that is used as a mathematical operator)
in a variable name.
5) Be consistent when using upper- and lower-case characters. In some languages
HOURS is a different variable name than Hours.
Data Types and Their Data Sets
How the Computer Stores Data?
 The computer stores data internally in memory locations.
 These data are found by the variable names used by a program.
 Each memory location can hold one and only one value at a time.
 When a user enters a new value into the variable location, the previous value is
destroyed.
 These memory locations are temporary.
 When a program completes its instructions, and/or when the computer is
turned off, the values stored in the internal memory are destroyed.
How the Computer Stores Data?

• When data, information, or programs have to be kept for future, they are stored
externally on an external storage medium such as a hard disk drive in storage
areas called files.
• There are basically two types of files: program files and data files.
• Program files contain the instructions to tell the computer what to do.
• Data files contain the data required to execute the program files.
Functions
• Functions are small sets of instructions that perform specific tasks and return
values.
• They are usually built into a computer language.
• Functions are repeatedly used in the problem solving process in order to
shorten the problem-solving time and improve the readability of the solution.
• Each language has a set of functions within it.
• Most languages allow programmers to write their own functions.
Functions
• A function is a block of code that has a name and it has a property that it is reusable.
• The form of a function is the name of the function followed by an open parenthesis,
followed by the data needed to perform the function and concluded by a closed
parenthesis:
Function_Name(data)
• The value of the result of the function is returned in the name of the function.
• Functions use data. The data is listed as part of the function and are called parameters.
• Sqrt(N) function will calculate the square root of N.
• Max(N1, N2, N3), will find which of three numbers is the largest.
Functions
• These functions have been divided into classes.
• Mathematical functions (square root, absolute value, or a random
number).
• String functions(copy part of the string into another variable, find the
length or the number of characters in the string).
• Conversion functions(convert a string value to a numeric value).
• Statistical functions(maximum values, minimum values).
• Utility functions(date and time functions).
Operators
• The computer has to be told how to process data.
• Operators are the data connectors within expressions and equations.
• They tell the computer how to process the data.
• They also tell the computer what type of processing (mathematical, logical, or
whatever) needs to be done.
• Mathematical, relational, and logical operators.
• The operand and the resultant are two concepts related to the operator.
• Operands can be constants or variables.
Operators
• Mathematical operators include addition, subtraction, multiplication,
division, integer division, modulo division, powers, and functions.
• Relational operators include the following: equal to, less than, greater than,
less than or equal to, greater than or equal to, and not equal to. A programmer
uses relational operators to program decisions. The resultant of a relational
operation is logical data type True or False.

• Logical operators are used to connect relational expressions (decision-


making expressions) and to perform operations on logical data.
Hierarchy of Operations
Expressions and Equations
 Expressions and equations make up part of the instructions in the solution to a
computer problem.
 An expression processes data, the operands, through the use of operators.
 An equation stores the result of an expression in a memory location in the
computer through the ( = ) sign.
Area=Length *Width
 Expressions use no equal sign (except as a relational operator).
 The resultant of an expression is not stored and, therefore, is unavailable for use
at another time.
Expressions and Equations
 Equations are often called assignment statements because the variable on the left
hand side of the equal sign ( = ) is assigned the value of the expression on the
right-hand side.
 The equal sign does not mean equals; instead, it means replaced by or is
assigned the value of.
 There is only one variable on the left-hand side of the equal sign. On the right
hand side of the equal sign is an expression.
Expressions and Equations
Communicating with the Computer
 Since the computer does not speak English as you do, you have to learn its
system of communication or language.
 Its system of communication requires instructions governed by specific rules.
 If the instructions you write are incorrect, the computer will give an error
message, the wrong answer, or no answer.
 If the instructions are not properly sequenced properly, the result will be wrong.
 Syntax – Rules for writing instructions.
 Error is bug.
 Debugging: A bug must be found and corrected.
Organizing the Solution
 Certain organizational tools will help you learn to solve problems on the
computer.
 The various tools are :
 Problem analysis chart: which shows a beginning analysis of the problem.
 structure chart or interactivity chart : which shows the overall layout or
structure of the solution.
 IPO chart: which shows the input, the processing, and the output.
 Algorithms: which show the sequence of instructions comprising the solution
 Flowcharts: which are graphic representations of the algorithms.
 Pseudocode: which represents a language like solution
Analyzing the Problem
 The programmer first has to understand and analyze the requirements of the
problem.
 A good way to analyze a problem is to separate it into four parts.
Calculate the gross pay of an employee given the hours worked and the
rate of pay. The gross pay is calculated by multiplying the hours worked
by the rate of pay.

The problem analysis chart is an aid to clear thinking because it helps the problem solver
to identify the essential data and information in a problem and to disregard the
nonessentials.
Developing the Interactivity Chart

 The next step in organizing your solution is to divide the processing


into subtasks called modules.
 Each module should contain the tasks to accomplish one function
such as entering data, printing results, or calculating results.
 There will be one module that controls the flow to most of the other
modules. This will be called the Control or Main module.
Developing the Interactivity Chart

 Each module should contain no more than 20 instructions.


 This breakdown enables you to view a complex problem in
simpler parts and to program smaller, simpler parts of a
program rather than one large, complex program.
 This chart is called the interactivity chart or structure chart.
Developing the Interactivity Chart
Developing the Interactivity Chart
Programming Solving
Techniques

Procedural Object-Oriented
Top-Down Approach
Within each module the execution is procedural.
• The top-down method simply means that as you divide the problem into
subtasks.
• When using the top-down method, the module that encompasses the complete
solution becomes what is called the Control module because it controls the
processing of all the data.
• Dividing a solution into modules is not an easy job. When you approach a
solution to a problem, you need to think about the basic things you want to
accomplish, during the processing, in general terms.
• Some of these might be input of data, initialization of data, types of processing
such as calculations or putting data in order, and completing the solution.
Developing the IPO Chart
• The IPO (input-processing-output) chart extends and organizes the information in
the problem analysis chart.
• It shows in more detail what data items are input, what processing takes place on
that data, and what information will be the end result, the output.
• The output is the first to be completed in an IPO chart. The next is the input,
and the last is the processing.
• An analogy to completing an IPO chart would be planning a trip. The first
thing you need to know is the destination—San Francisco, Paris, New York.
This information is the output. Then you can plan what to pack, your means
of transportation, and all the other essentials for starting out—the input.
• Finally, you plan the route—the processing. How can you know what to pack
or what route to follow if you don’t know where you’re going?
Writing the Algorithms
After using the structure chart and the IPO chart, the next step in organizing a
solution is for the programmer to develop sets of instructions for the computer,
called algorithms.

You might also like