problem solving techniques3
TRANSCRIPT
-
8/13/2019 Problem Solving Techniques3
1/12
1 ProblemSolvingTechniques
PROBLEM SOLVING TECHNIQUESIMPORT NT QUESTIONS SOLVED
Index:
OnemarkquestionsPage1to2
TwomarkquestionsPage3to7
FivemarkquestionsPage8to16
QuestionscarryingTWOmarkseach
1. Whatistopdownanalysis?Mar06Ans:
Givenproblemislogicallysplitintonumbersubproblemswhichareinturnsplitintosmallerproblemssuchthat
eachsmallestproblemcanbeeasilysolved.
2. Mentionanyoneadvantageoftopdownapproach.July06Ans:
(Writeanyoneofthefollowing)
a. Itleadstoeasyunderstandabilityoftheproblem.b. Helpsinreuseofthecodetoperformaparticulartaskinanyotherprogram.c. Itavoidsduplicationincoding.d. Tasksperformedbyanypartoftheprogramcanbeeasilyidentified.e. Stepwiserefinementoftheproblemreducesthecomplexity.
3. Definethetermmodularity.Mar07,Jul09Ans:
Itisatechniquewhereagivenproblemisdividedintoanumberofselfcontainedindependentprogram
segments.Eachsegmentiscalledamoduleanditcanbecalledinanotherprogramoranothermodule.
4. Whatisthemaindisadvantageoflinearsearchmethod?Mar07,Mar10Ans:
Itislengthyasitinvolvesmorenumberofcomparisons.Alsorepeatedexistenceofanelementinthearray
cannotbedetected.
5. Whatisstructuredprogramming?Jul07Ans:Structuredprogrammingdealswithlogicandcode.Itinvolvestopdownanalysisapproachtoproducecode
fromasmallsetoflogicalconstructslikei.Sequenceii.Iterationiii.Selectionandiv.Modularity.
6. DefineSorting.Mar08Ans:
Itistheprocessofarrangingtheelementsofanarrayeitherinascendingorindescendingorder.
7. WhatisStepwiserefinement?Jul08Ans:
TheprocessofTesting,Debugging,addingthecode,deletingthecodeormodifyingthecodeateverystepofthe
programasperthetopdownanalysischartisknownasStepwiserefinement.
-
8/13/2019 Problem Solving Techniques3
2/12
2 ProblemSolvingTechniques
8. Writeanyoneadvantageoflinearsearch.Jul08Ans:
Usingthistechniqueevenanunsortedarraycanbesearched.
9.What
is
the
other
name
of
Bubble
Sort.
Mar
09.
Ans:
ExchangeSort
10.Whatissearching?Jul09Ans:
TheprocessoffindingthepresenceandthelocationofanelementinanarrayisknownasSearching.
11.Whatisamodule?Mar10Ans:
Itisalogicallyseparatedsmallestpartofthegivenproblemwhichiscapableofperformingasingletask.
12.Whatisworstcaseinsorting?Ans:
Asortingtechniqueinvolvesnumberofexchangesofelementsbetweendifferentlocationsofthearray.A
situationwhereinamaximumnumberofexchangesareinvolvedissaidtobetheworstcaseinsorting.
QuestionscarryingTWOmarkseach
13.Whatisbinarysearch?Whenisitapplicable?Mar06Ans:
ThissearchingtechniqueissuperiortoLinearsearchbutcanbeusedonlywhenthearrayisreadily
sorted.Inthistechnique,thegivenarraytobesearchedissplitintotwoequalpartsandthesearchiscontinued
inthatpartofthearraytowhichthesearchelementbelongs.Theaboveprocessisrepeatedtilltheelementis
foundortillthearrayiscompletelysearched.
Itisapplicableonlywhenanarrayisalreadysorted.
14.Explaintheconceptofstructuredprogramming.Mar06Ans:
Structuredprogrammingdealswithlogicandcode.Itinvolvestopdownanalysisapproachtoproducecodefrom
asmallsetoflogicalconstructslikei.Sequenceii.Iterationiii.Selectionandiv.Modularity.
15.Whatdoyoumeanbysearching?Mentionanyonetype.Jul06Ans:
TheprocessoffindingthepresenceandthelocationofanelementinanarrayisknownasSearching.
LinearSearchandBinarySearch(anyonetobementioned)
16.Giveanytwoadvantagesofstructuredprogramming.Jul06Ans:
(TherearemanyadvantagesofStructuredprogramming,mentionanytwoofthefollowing)
1. Iteration:Asetofinstructionscanbeperformedrepeatedlyuntilaparticularconditionissatisfied.Thisreducesthecode.
2. Modularity:Identificationofthetaskiseasier.3. Modularity:Testinganddebuggingatthemoduleleveliseasier.4.
Modularity:
A
module
can
be
reused
at
different
part
of
the
program
or
into
any
other
program
also.
-
8/13/2019 Problem Solving Techniques3
3/12
3
5. S17.Justi
Ans:
It
is
neasie
Diffe
1.2. S3. I
18.WhaRefe
19.WhaAns:
Itis
t
techi
4.5. S6. I
20.ConsAns:
21.BriefProblemSol
election:Di
ytheneed
ecessary
to
randfaster
rentsorting
ubbleSort
electionSor
sertionSor
istopdow
:Question
issorting?
heprocess
niquesare(
ubbleSort
electionSor
sertionSor
tructatop
lyexplain
t
INPUT
radius(r)
vingTechni
ferentseto
orsortinga
sort
an
arraatanyinsta
techiniques
t
t
ndesign?
Mentionan
farranging
Mentionan
t
t
owndesign
eanalysis
o
A
ues
instruction
ndmention
as
it
becot.
are(Mentio
ar07
yonemeth
theelement
one)
tofindthe
finsertion
s
Areaof
PRO
ea=3.142*r
scanbeper
anyoneso
es
easier
to
nanyone)
d.Jul07
sof
an
arra
areaofcircl
ortmethod.
acircle
ESS
*r
formedbas
tingtechni
search
an
e
either
in
as
e.Jul07
Mar08
Radius
donwheth
ue.Mar0
lement
and
cendingor
i
OUTPUT
eraconditio
hence
acces
descendin
Area
nissatisfie
sibility
of
th
order.Diffe
ornot.
edata
is
rentsorting
-
8/13/2019 Problem Solving Techniques3
4/12
4 ProblemSolvingTechniques
Ans:
22.Mentionthevariousprogrammingconstructsofstructuredprogramming.Mar08,Mar10Ans:
i.Sequenceii.Iterationiii.Selectionandiv.Modularity.
23.Whatisprogrammaintenance?Explainbriefly.Jul08Ans:
Inthisprocess,apartorcompleteofthecodeofanymodulemaybechangedtoperformanaddedtaskorjustto
overcomethebugs.i.e.,modificationofcode
1. Basedonthepresent/futurenewrequirementsoftheuseror2. Basedontheerrorsfacedinthepreviousexecutionsor3. Tocopeupwiththecompetitioninthemarket.Inthisprocess,apartorcompleteofthecodeofanymodulemaybechangedtoperformanaddedtaskorjustto
overcomethebugs.
24.Writeanytwopropertiesoftopdownanalysis.Mar09.Ans:
(Thereare5importantcharacteristics.MentionanyTWO)1. CodeReusability:Thealreadyavailableasamodulecanbereusedindifferentpartofthesameprogramorin
anyotherprogram.
2. Understandability:Modularityrepresentclearlythesequenceofexecutionoftheprogramandhencereducesthe
errors
and
complexity.
3. ProgramMaintenance:Inthisprocess,apartorcompleteofthecodeofanymodulemaybechangedtoperformanaddedtaskorjusttoovercomethebugs.
4. EliminationofduplicationinCoding:Amodulecanbecalledanywhereintheprogramandasmanytimesrequiredwithrewritingthecode.Thiseliminatesduplication.
5. ClearIdentificationoftasks:Thenameofanymoduleclearlyexplainsthetaskperformedbythemodule.
25.Writeanalgorithmforlinearsearch.Mar09.
-
8/13/2019 Problem Solving Techniques3
5/12
5
Ans:
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
26.MenAns:
1.2.3. F
27.ConsAns:
28.Desi
Princial(P)
ProblemSol
1: Loc=1
2:ForI=0to
3:
If
(ele=A[I4: LOC
5: Got
[
6: If(LOC>=
7: Print
8: Else
9: Print
[
10:Exit
iondiffere
hile
oWhile
or,etc.,
tructatop
nan
algorit
INPUT
Ra(R
vingTechni
n1do
]
Then
=I
oStep6
[EndIf]
Endofforlo
0)then
ele,Found
ele,Notfo
EndIf]
titerative
owndesig
hmto
find
e)
ues
op]
inlocation,
nd
rogrammin
modelcha
inimumva
SI
ime(T)
SI=
LOC
constructs
ttofindth
luein
an
ar
PLEINTER
PROCESS
P*T*R)/10
.
simpleint
ayof
nvalu
EST
0
Princi
rest.
es.
pal
OUTPUT
ate ime SI
-
8/13/2019 Problem Solving Techniques3
6/12
6 ProblemSolvingTechniques
Ans:
SMALL=A[0]
POS=0
FOR
I
=
1
TO
N
1
DO
IF(A[I]
-
8/13/2019 Problem Solving Techniques3
7/12
7 ProblemSolvingTechniques
ClearIdentificationoftasks:Eachmodulecreatedshouldperformasingletaskandthenameofthemoduleshouldconveythepurposeofthemodule.
Program
Maintenance:
Solution
should
be
organized
in
such
a
way
that
adding
new
functionalities
should
not
leadanylargechangestotheoriginalsolution.
Eliminatesduplicationincoding:Aseverytaskiscodedasamodule,whichisanindependentprogramitcanberepeatedanynumberoftimesintheprogram.Thiseliminatestheneedtorepeatthesamecodeinanumberof
placesintheprogram.
Codereusability:Modulewhichhavebeencodedandworkproperlyinoneprogramcanusedinotherprograms.31.Writeanalgorithmforinsertionsort.
Ans:
32.Supposethefollowingnumbersarestoredinanarray,applybubblesort.23,3,4,36,31Ans:
\\\\\\\continuedinthenextpage...
A 23 3 4 36 31
0 1 2 3 4
-
8/13/2019 Problem Solving Techniques3
8/12
8 ProblemSolvingTechniques
33.ExplainthestepsinvolvedinBinarySearchmethodwithanexample.Ans:
Thisisthesimplesttechniqueofsearchinganelementinanarraythoughitismoretimeconsuming.
Inthistechnique,theelementinsearchiscomparedwitheachelementofthearrayoneatatimestartingfrom
thelowestpositionofthearraytillthesearchissuccessfulortillthewholearrayissearched.
Example:
Refer
qn.number
43.
34.WritethestepsinvolvedinperformingBinarySearchforthefollowing.32,48,56,79,82,99
Ans:
Letthearraybe
N=6
Searchelementbe,ele=79
Step1:
A 32 48 56 79 82 99
0 1 2 3 4 5
-
8/13/2019 Problem Solving Techniques3
9/12
9 ProblemSolvingTechniques
LOW=0
HIGH=N1=61=5
MID=(LOW+HIGH)/2=(0+5)/2=2
A[MID]=56
Step2:CompareA[MID]withele.56isnotequalto79
Step3:Checkwhetherthesearchelementisgreaterthanmidorlesser.79isgreaterthan56.
Step4:Change,LOW=MID+1=2+1=3
Step5:MID=(LOW+HIGH)/2=(3+5)/2=4
A[MID]=82
Step6:Checkwhetherthesearchelementisgreaterthanmidorlesser.79issmallerthan82.
Step4:Change,HIGH=MID 1=4 1=3
Step7:MID=(LOW+HIGH)/2=(3+3)/2=3
A[MID]=79
Step8:
Check
whether
the
search
element
is
greater
than
mid
or
lesser.
They
are
same.
Hence
Search
is
completed.
Result:Thesearchelementele=79isfoundthearrayAatlocation,LOC=3
35.Sortthefollowingelementsusingselectionsort.Ans:
Considerthearray:
Pass1:
Steps:
i. 5and4arecomparedand4issmall.ii. 4iscomparedwith14and4isonlysmalliii. 4iscomparedwith10and4isfoundsmalliv. 4iscomparedwith1and1isfoundsmall
Therefore1isinterchangedwith5andtheresultingarrayis
Theabovestepsarerepeatedinthecomingpassesandtheresultantarrayinthepassesareasbelow.
Pass2:
Pass3:
A 5 4 14 10 1
0 1 2 3 4
A 1 4 14 10 5
0 1 2 3 4
A 1 4 14 10 5
0 1 2 3 4
A 1 4 5 10 14
0 1 2 3 4
-
8/13/2019 Problem Solving Techniques3
10/12
10 ProblemSolvingTechniques
Pass4:
Andhencethearrayissortedintheincreasingorder.
36.Whatisstructuredprogramming? Listadvantagesofit.Ans:
Refertoquestions5and16
37.WriteanalgorithmtosearchanelementinanarrayusingBinarysearch.Ans:
38.Explainthebinarysearchmethodwithasuitableexample.Ans:
Binarysearchalgorithmisusedtosearchforanelementinasortedlist.Thevalueoftheelementinthe
middleofthelistiscomparedwiththevalueoftheelementtobesearchedfor.Inthemiddleelementislarger,
thedesiredelementhastobeinthefirstpartofthelist,ifitispresent.Ifthemiddleelementissmaller,the
desiredelementhastobesecondhalfofthelist.Hencethesearchiscontinuedonlythedesiredpartofthearray.
Ineverysuchcomparison,thelengthofthearraytobesearchedbecomeshalf.
Example:
A 1 4 5 10 14
0
1
2
3
4
A
1
4
14
15
25
-
8/13/2019 Problem Solving Techniques3
11/12
11 ProblemSolvingTechniques
Letussearchofelement15inthearray
Mid=(low+high)/2=(0+4)/2=2
Therefore
mid
element
is
A[mid]=
A[2]=14
Now,themidelementofthearrayis14.Andthesearchelement15isgreaterhenceitmustlieinthesecondhalf
ofthearray.Hencethearraytobesearchedisonly
Mid=(low+high)/2=(3+4)/2=3
ThereforemidelementisA[mid]=A[3]=15
Now,themidelementofthearrayis15.Andthesearchelement15issameasthemidelement.
Result:The
search
element
15
is
found
at
A[3].
39.Writeanalgorithmtofindthemaximumelementinanarray.Ans:
40.Tracebinarysearchalgorithmtofindthelocationoftheelement22inthefollowinglist.22,33,49,57,75.Ans:
Letussearchofelement22inthearray
Mid=(LOW+HIGH)/2
=(0+4)/2=2
ThereforemidelementisA[MID]=A[2]=49
Now,themidelementofthearrayis14.Andthesearchelement22issmallerhenceitmustlieintheFIRSThalf
ofthearray.Hencethearraytobesearchedisonly
Mid=(LOW+HIGH)/2= (0+1)/2=0
ThereforemidelementisA[MID]=A[0]=22
Now,themidelementofthearrayis22.Andthesearchelement22issameasthemidelement.
Result:Thesearchelement22isfoundatA[0].
0
Low
1 2
Mid
3 4
High
15 25
3
Low
4
High
A 22 33 49 57 75
0
Low1
2
Mid3
4
High
A 22 33
0
LOW
1
HIGH
-
8/13/2019 Problem Solving Techniques3
12/12
12 ProblemSolvingTechniques
41.Writeanalgorithmforselectionsorttechnique.Ans:
42.Applylinearsearchmethodtofindthevalue43inanarraycontainingthefollowingvalues.38,40,31,28,43,45,60.
Ans:
Letussearchofelement43inthearray
Letthesearchelementbeele=43
Step1:CompareelewithA[0],theyarenotsame.
Step2:CompareelewithA[1],theyarenotsame.
Step3:CompareelewithA[2],theyarenotsame.
Step4:CompareelewithA[3],theyarenotsame.
Step5:CompareelewithA[4],theyaresame.
Stopthe
search
and
declare
the
result
as,
the
search
element
is
found
at
A[4].
Youcanalsodownloadthesoftcopyofthismaterialbyusingtheurlgivenbelow.
www.npscience.com
or
forum.npscience.com
by
NawabPasha
A 38 40 31 28 43 45 60
0 1 2 3 4 5 6