0% found this document useful (0 votes)
34 views40 pages

L1 - Problem Solving Methodology

This document outlines the syllabus for the course 18EC360 - Programming for Problem Solving. The syllabus covers key topics like problem solving methodology, basics of programming, modular programming and functions, array and string handling, memory management and pointers, derived data types, and file handling. It also lists experiments involving fundamental programming constructs, decision making and looping statements, arrays, strings, recursive and non-recursive functions, dynamic memory management, and solving numerical problems. The document provides an overview of the problem solving process and methodology for engineering problems which involves clearly stating the problem, describing inputs/outputs, working examples by hand, developing algorithms and programs, and testing solutions.

Uploaded by

S Ponmalar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views40 pages

L1 - Problem Solving Methodology

This document outlines the syllabus for the course 18EC360 - Programming for Problem Solving. The syllabus covers key topics like problem solving methodology, basics of programming, modular programming and functions, array and string handling, memory management and pointers, derived data types, and file handling. It also lists experiments involving fundamental programming constructs, decision making and looping statements, arrays, strings, recursive and non-recursive functions, dynamic memory management, and solving numerical problems. The document provides an overview of the problem solving process and methodology for engineering problems which involves clearly stating the problem, describing inputs/outputs, working examples by hand, developing algorithms and programs, and testing solutions.

Uploaded by

S Ponmalar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

18EC360 – PROGRAMMING FOR PROBLEM SOVING

PRESENTED BY

Dr. S.PONMALAR
Assistant Professor
Department of ECE
Thiagarajar College of Engineering
Madurai

1
18EC360 - SYLLABUS
• Problem Solving Methodology: problem specification and
analysis, algorithm design, flowchart, programs, program
testing and verification
• Basics of Programming: data types and its representation,
variables, keywords, Operators, operator precedence, types of
expression, Control Structures: Selection structure, looping
Structure
• Modular Programming and Functions: Function
declaration, function definition, function call-call by value -
call by reference, storage classes, variable scope, use of
stacks in function call, Recursive functions, library functions

2
SYLLABUS
• Array and string handling algorithms: 1-D, 2-D array
declaration, initialization, using arrays as function arguments,
strings sorting: selection sort, bubble sort, searching: linear
and binary search, text processing: key word search, text
editing
• Memory management & Pointers: use of malloc-realloc-
free- heaps in memory management, Pointers and memory
addressing, Arrays and pointer arithmetic, Pointers and
Functions, Pointers to pointers, pointer and string arrays, Void
and function pointers
• Derived data types: structures- Arrays of Structures –
Passing Structures to Functions – Structure Pointers –
Structures within Structures, union, bitfield, enumeration,
typedef File Handling: read, write and update text files – file
structure for database management
3
SYLLABUS

List of Experiments:
1. Programs to explore
fundamental programming constructs
– Find the size of the processor, range of all primary data types.
– Use of different types of operators and expressions.
2. Programs using decision making, case control and looping
statements.
– Print twin prime numbers in a given range
– Finding greatest common divisor using Euclid’s method
– Trigonometric series generation
3. Programs using 1-D and 2-D arrays
– Bubble and insertion sort algorithms
– Matrix multiplication
4
SYLLABUS
4. Programs using strings
1. Linear pattern search
2. Text editing
5. Programs using recursive and non-recursive functions
3. Binary search
4. Finding nth Fibonacci number
6. Programs for dynamic memory management and pointer
arithmetic
7. Programs to create database files using file structures
8. Solving numerical methods/engineering problems (sample)

1. Finding roots of a linear equation using bisection method


2. Numerical integration by trapezoidal method
3. Linear convolution
4. Bitwise operations to set specific bit fields 5
Introduction to Problem Solving
• Engineers must analyze and solve a wide range of technical
problems.
– Some will be reasonably simple single-solution problems.
– Others will be open-ended and will likely require a team of
engineers from several disciplines.
– Some problems may have no clear solution.

• Problem Solving Involves:


– Experience
– Knowledge
– Process
– Art

6
PROBLEM SOLVING
Types of problems:
• computational, problems involving some kind of mathematical
processing;

• logical, problems involving relational or logical processing

• repetitive, problems involving repeating a set of mathematical


and/or logical instructions.

7
PROBLEM SOLVING (contn.)
• Algorithmic Solution
– Solutions that can be attained by solving a series of steps
• heuristic solutions
– Solutions that cannot be reached through a direct set of steps

8
The Problem-Solving aspect:
Requirements for solving problems by computer
• Algorithm
• Data Structure
• Programming Language

Problem

Algorithm & Programming Language with data structure

input “computer” output

9
Steps in Problem Solving

1. Identify the problem.


2. Understand the problem.
3. Identify alternative ways to solve the problem.
4. Select the best way to solve the problem from the list of
alternative solutions.
– identify and evaluate the pros and cons of each possible solution
before selecting the best one.
– In order to do this, you need to select criteria for the evaluation.
5. List instructions that enable you to solve the problem using the
selected solution. (Algorithm)
6. Evaluate the solution.
– Check to see if the results are correct.

10
Steps in problem solving (Example)
Solving a problem of what to do in the 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.
3. Identify alternatives.
− Watch television.
− Invite friends over.
− Play video games.
− Go to the movies.
− Play cricket.
− Go to the amusement park.
− Go to a friend’s party.
The list is complete only when you can think of no more alternatives.

11
Steps in problem solving (Example)
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? If nobody is having fun, then the planner needs to
review the steps to have a fun evening to see whether anything can be
changed, if not then the process must start again.

12
An Engineering Problem-Solving Methodology

The process or methodology for engineering problem has the


following steps:
1. State the problem clearly.
2. Describe the input and output information.
3. Work the problem by hand (or with a calculator) for a
simple set of data.
4. Develop a solution and convert it to a computer program.
5. Test the solution with a variety of data.

13
An Engineering Problem-Solving Methodology - Example
1. PROBLEM STATEMENT
• The first step is to state the problem clearly to avoid any misunderstandings.
• For this example, the problem statement is the following:
– Compute the straight-line distance between two points in a plane.

2. INPUT/OUTPUT DESCRIPTION
• describe the information (inputs) that is given to solve the problem and
identify the values to be computed (output).
• a diagram showing the input and output is useful. I/O diagram for this
problem is

14
An Engineering Problem-Solving Methodology - Example
3. HAND EXAMPLE
• The third step is to work the problem by hand or with a calculator,
using a simple set of data.
• The solution by hand for this specific example is as follows:
– Let the points have the following coordinates:

15
An Engineering Problem-Solving Methodology - Example
4. Algorithm Development
• develop an algorithm, or a step-by-step outline, of the
problem solution.
The steps required to compute and print the distance between
two points:
Algorithm
1. Give specific values for point1 (x1, y1) and point2
(x2, y2)
2. Compute side1=x2-x1 and side2=y2-y1
3. Compute the distance between the two points, which
is equal to the length of the hypotenuse of the
triangle.
distance  side12  side2 2
4. Print the distance between the two points.
16
An Engineering Problem-Solving Methodology - Example
(cont.)
FLOWCHART

17
Flowchart to compute distance between two points
START

Input x1,y1,x2,y2

Dist - sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))

Print dist

stop

18
An Engineering Problem-Solving Methodology - Example (cont.)

5. PROGRAM DEVELOPMENT
• The algorithm is then converted to C commands so that
we can use the computer to perform the computations.

19
Program:
/* This program computes the distance between two points. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare and initialize variables. */
int x1=1, y1=5, x2=4, y2=7, side_1, side_2;
float dist;
/* Compute sides of a right triangle. */
side_1 = x2 - x1;
side_2 = y2 - y1;
dist = sqrt(side_1*side_1 + side_2*side_2);
/* Print distance. */
printf("The distance between the two points is %f \n", dist);
return 0;
}
20
An Engineering Problem-Solving Methodology - Example (cont.)

6. TESTING
• The final step in problem-solving process is testing the
solution.
• First test the solution with the data from the hand
• When the C statements in this solution are executed, the
computer displays the following
– output:
The distance between the two points is 3.61
• This output matches the value that we calculated by hand.
• If the C solution did not match the hand-calculated solution,
then we should review both solutions to find the error.
• Once the error is corrected, test it with different set of data.

21
Simple examples to develop solutions

22
Compute the area of a triangle

1. State problem
x=3 cm
2. I/O
y=4 cm
3. Hand example
4. Develop solution
x area
and Coding y
5. Testing
area = ½ * 3 *4 = 6 cm2

1. Get values of x and y


2. Compute area = ½*x*y
3. Print area

23 23
Given two complex numbers z1=a1+b1*i
and z2=a2+b2*i, find z3= z1+z2

a1
1. State problem b1 a3
a2 b3
2. I/O b2

3. Hand example
4. Develop solution 1. Get a1 b1 a2 b2
2. Compute a3 = a1 + a2
and Coding
3. Compute b3 = b1 + b2
5. Testing 4. Print z3 = a3 + b3 * i

24 24
Given the number of seconds, find number
of hours, minutes and seconds

H
1. State problem total_sec
M
S
2. I/O
3. Hand example 3675 seconds can be written as
1 hour 1 min 15 sec
4. Develop solution 1. Get total_sec
2. H = total_sec / 3600 (integer division)
and Coding
3. M = (total_sec – (H*3600)) / 60
5. Testing M = (total_sec mod 3600) / 60
4. S = total_sec – (H*3600) – (M*60)
5. Print H hour, M min, S sec

25 25
Example: Average speed
Problem:
Suppose a car goes from city A to city B with speed of 40 mph and immediately comes
back with the speed of 60 mph.
• What is the average speed?
• Generalize this solution and outline step by step algorithm to find average speed
when the speed from A to B is X and the speed from B to A is Y?
Ans:
Speed = distance/time, distance = speed *time
City A to B : distance d= 40*t1
City B to A: distance d = 60 *t2
Since distance are same, 40*t1 = 60* t2
t1=60*t2/40 = 3/2*t2
Average speed =( 40*t1 + 60 *t2 ) / (t1 + t2 )
(40*(3/2)*t2 + 60*t2)/(t2+(3/2)*t2 = 120*t2/(5/2)*t2 = 120*2/5 = 48 MPH

average speed of an object is the total distance traveled by the object divided by the elapsed time to cover that
distance.
26 26
Ten heuristics for problem solving
How to Solve it: Modern Heuristics by Michalewicz and Fogel. Springer 2004.

1. Don’t rush to give an answer, think about it


2. Concentrate on the essentials and don’t worry about
the noise (tiny details)
3. Sometimes finding a solution can be really easy
(common sense), don’t make it harder on yourself
4. Beware of obvious solutions. They might be wrong
5. Don’t be misled by previous experience

27
Ten heuristics for problem solving
How to Solve it: Modern Heuristics by Michalewicz and Fogel. Springer 2004.

6. Start solving. Don’t say “I don’t know how”


– Most people don’t plan to fail, they just fail to plan!
7. Don’t limit yourself to the search space that is
defined by the problem. Expand your horizon
8. Constraints can be helpful to focus on the problem at
the hand
9. Don’t be satisfied with finding a solution, look for
better ones
10. Be patient. Be persistent!

28
1. Phases of Problem Solving

1. Problem definition phase


2. Getting started on a problem
3. The use of specific examples
4. Similarities among problems
5. Working backwards from the solution
6. General problem solving strategies

29
2. Top – Down Design

2.1 Breaking Problem into Sub problems


2.2 Choice of a suitable Data Structure
2.3 Construction of Loops
2.4 Establishing initial conditions for loops
2.5 Finding the iterative construct
2.6 Termination of loops

30
3.Implementation of Algorithms

– Use of procedures/functions to emphasize modularity


– Choice of variable names
– Documentation of programs
– Debugging programs
– Program testing

31
4. PROGRAM VERIFICATION

 Program verification

- Application of mathematical proof techniques

- Check the results obtained by the execution of a program


with arbitrary inputs according to the output specifications

32
5.THE EFFICIENCY OF ALGORITHMS

Efficiency of algorithm mainly depends upon


 Design
 Implementation
 Analysis of algorithms

Suggestions to design efficient algorithms


 Redundant computations
 Inefficiency due to late termination
 Early detection of desired output conditions
 Trading storage for efficiency gains

33
TUTORIAL

PROBLEM SOLVING METHODOLOGY

34
Dimensions of a rectangle ranch
A farmer has a rectangular ranch with a perimeter of P=110 meters and an
area of A=200 square meters.
• What are the
dimensions of
his ranch?
• What are the dimensions for any P and A?

35
Make a triangle with maximum area

• Suppose you have a stick with the length of L meters. You want to divide it into
three pieces with the length of x, y, and z meters, and make a triangle
• Find x, y, z such that
the area of that triangle
is maximum.

z
x

y
36
Climbing a wooden post

• A snail is climbing a wooden post that is H=10m high.


• During the day, it climbs U=5 m up.
• During the night, it falls asleep and slides down D=4m
• How many days will it take the snail to climb the top of
the post?
• Given that H > U > D. Can you generalize your
solution for any H, U, and D?

37
Minimum number of coins
• Suppose you want to give x= Rs. 57 to a person, what is the minimum number
of coins

• You have many 10, 5, 2,1 rupee coins

38 38
Example: Sum of numbers

Given n (for example n=1000), compute


• sum = 1+2+3+…+n

• sum_odd =1+3+5+7+…+(2n+1)

• ln2=1- 1/2 + 1/3 - 1/4 +…  1/n

39 39
Reference:
R. G. Dromey, "How to Solve It By Computer",
PearsonEducation, 1982

40

You might also like