Você está na página 1de 8

TECHCRASHCOURSE

(http://www.techcrashcourse.com/)
(//www.blogger.com/rearrange?blogID=8051473532172930360&widgetType=HTML&widgetId=HTML1&action=editWidget&sectionId=crosscol)

CProgramtoReverseanArrayUsingRecursion
Writeaprograminctoreversebyswappingelementsandrecursion.

ToReverseanarraywehavetoreversethesequenceofarrayelements.Thefirstelementof
arrayshouldbecomethelastelementandlastelementwillbecomethefirstelement.
ForExample:
IfInputarrayis:1234567
Reversedarrayshouldbe:7654321
ToreverseanarrayoflengthNusingrecursion,wehavetoswaptheleftmost(array[0])and
rightmost(array[N1])elementofarrayandthenrecursivelyreversetheinnersubarrayfrom
index1toN2.Keeponrepeatingthisunlesssizeofsubarrayisgreaterthanone.
Algorithmtoreverseanarrayusingrecursion
LetinputArrayisanarrayoflengthNandleftIndexandrightIndexaretwointeger
variablestostoreindexreferences
Wecanuserecursiontoreverseanarraybycreatingasmallersubproblemfrom
originalproblem.
Toreverseanarray,wewillfirstswapfirstelement(inputArray[0])andlast
element(inputArray[N1])ofarrayandthenrecursivelyreversesubarrayfromindex
1toN2.
Letreverseisafunctionwithtakesarray,leftIndexandrightIndexasinput.Itfirst
swapinputArray[leftIndex]andinputArray[rightIndex]andthenrecursivelycallitself
onsubarrayfromindexleftIndex+1torightIndex1.
voidreverse(int*array,intleftIndex,intrightIndex)
reverse(inputArray,i,j)=swap(inputArray[i],inputArray[j])andreverse(inputArray,
i+1,j1).
RecursionwillbreakwhenleftIndexbecomes>=rightIndex.

Cprogramtoreverseandarrayusingrecursion
Belowprogramusestwouserdefinedfunctions'swap'and'reverse'.Functionswap(int*array,
intleftIndex,intrightIndex)swapstheelementsofarrayatindexleftIndexandrightIndex
whereasfunctionreverse(int*array,intleftIndex,intrightIndex)isarecursivefunctionthat
reversethesubarrayofarrayfromindexleftIndextorightIndex.
Reversefunctioninternallycallsswapfunctiontoswapleftmostandrightmostelementof
subarrayandthenallitselftoreversesubarrayexcludingleftmostandrightmostelement.
RecursionwillterminatewhenleftIndexbecomesgreaterthanorequaltorightIndex,orin
otherwordswhensizeofthesubarraybecomeslessthanorequalto1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

/*
*CProgramtoreverseanarrayusingrecursion
*/
#include<stdio.h>
#include<conio.h>

voidswap(int*array,intleftIndex,intrightIndex);
voidreverse(int*array,intleftIndex,intrightIndex);

intmain(){
intinputArray[500],elementCount,counter;

printf("Enternumberofelementsinarray:");
scanf("%d",&elementCount);
printf("Enter%dnumbers\n",elementCount);

for(counter=0;counter<elementCount;counter++){
scanf("%d",&inputArray[counter]);
}

reverse(inputArray,0,elementCount1);

/*PrintReversedarray*/

24

printf("ReversedArray\n");

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

printf("ReversedArray\n");
for(counter=0;counter<elementCount;counter++){
printf("%d",inputArray[counter]);
}

getch();
return0;
}
/*
*Functiontoswaptwonumbersinarray
*/
voidswap(int*array,intleftIndex,intrightIndex){
inttemp;
temp=array[leftIndex];
array[leftIndex]=array[rightIndex];
array[rightIndex]=temp;
}

/*
*FunctiontoreverseanArrayusingrecursion
*/
voidreverse(int*array,intleftIndex,intrightIndex){
if(NULL==array){
printf("InvalidInput");
return;
}
/*
*Swapleftandrightelementsandrecursivelycallreverse
*functiononsubArray[leftIndex+1,rightIndex1]
*/
if(leftIndex<rightIndex){
swap(array,leftIndex,rightIndex);
reverse(array,leftIndex+1,rightIndex1);
}
}

ProgramOutput
Enternumberofelementsinarray:6
Enter6numbers
123456
ReversedArray
654321

RelatedTopics
Cprogramtoreverseastringusingrecursion(http://www.techcrashcourse.com/2015/03/c
programtoreversestringusingrecursion.html)
Cprogramtoreverseanumberusingrecursion
(http://www.techcrashcourse.com/2015/03/cprogramtoreversenumberusing
recursion.html)
Cprogramforpalindromecheckusingrecursion
(http://www.techcrashcourse.com/2015/03/cprogramtocheckpalindromeusing
recursion.html)
Cprogramtofindsumofdigitsofanumberusingrecursion
(http://www.techcrashcourse.com/2015/03/cprogramtofindsumofdigitsofnumber.html)
Cprogramtoreverseanarray(http://www.techcrashcourse.com/2015/03/cprogramto
reverseanarray.html)
CProgramtofindmaximumelementsinanarray
(http://www.techcrashcourse.com/2015/03/cprogramtofindmaximumelementarray.html)
Cprogramtofindsumofarrayelementsusingrecursion
(http://www.techcrashcourse.com/2015/03/cprogramsumofarrayusingrecursion.html)
Cprogramtoinsertanelementinanarray(http://www.techcrashcourse.com/2015/03/c
programtoinsertelementinanarray.html)
ListofallCPrograms(http://www.techcrashcourse.com/2014/10/cprogramexamples.html)

Previous
(http://www.techcrashcourse.com/2015/03/c
programtoreversenumberusing
recursion.html)

Next
(http://www.techcrashcourse.com/2015/11/c
programtofindareaandcircumferenceof
circle.html)

PostedbyTechCrashCourse(https://www.blogger.com/profile/17353939485345171416)
(https://www.blogger.com/emailpost.g?blogID=8051473532172930360&postID=360778781195956357)

NewerPost(http://www.techcrashcourse.com/2015/03/cprogramfibonacciseriesusing

NewerPost(http://www.techcrashcourse.com/2015/03/cprogramfibonacciseriesusing
recursion.html) OlderPost(http://www.techcrashcourse.com/2015/03/cprogramtodeleteduplicate
elementsfromsortedarray.html) Home(http://www.techcrashcourse.com/)
CTUTORIAL
CTutorial
(http://www.techcrashcourse.com/2015/05/c
programming
languagetutorial.html)
CHistory
(http://www.techcrashcourse.com/2015/05/c
programming
languagehistory.html)
CFirstProgram
(http://www.techcrashcourse.com/2015/05/c
programmingfirst
programhello
world.html)
Cprintfandscanf
(http://www.techcrashcourse.com/2015/05/c
programmingprintf
scanffunction.html)
CDataTypes
(http://www.techcrashcourse.com/2015/05/c
programming
languagedata
types.html)
CStorageClass
(http://www.techcrashcourse.com/2015/06/c
programmingstorage
classes.html)
COperators
(http://www.techcrashcourse.com/2015/05/c
programming
language
operators.html)
CBitwiseOperator
(http://www.techcrashcourse.com/2015/05/c
programmingbitwise
operators.html)
CTypeCasting
(http://www.techcrashcourse.com/2015/06/c
programmingdata
typecasting.html)
CIfStatement
(http://www.techcrashcourse.com/2015/05/c
programmingif
statement.html)
CSwitchStatement
(http://www.techcrashcourse.com/2015/05/c
programmingswitch
statement.html)
CIfElseStatement
(http://www.techcrashcourse.com/2015/05/c
programmingifelse
statement.html)
CforLoop
(http://www.techcrashcourse.com/2015/05/c
programmingfor
loop.html)
CwhileLoop
(http://www.techcrashcourse.com/2015/05/c
programmingwhile
loop.html)
CdowhileLoop
(http://www.techcrashcourse.com/2015/05/c
programmingdowhile
loop.html)
CbreakStatement
(http://www.techcrashcourse.com/2015/05/c
programmingbreak
statement.html)
CFunction

Declaration
(http://www.techcrashcourse.com/2015/05/c
programmingfunction
declaration.html)
CFunctionCalling
(http://www.techcrashcourse.com/2015/05/c
programmingfunction
calling.html)
CVariablesScope
(http://www.techcrashcourse.com/2015/05/c
programming
variablesscope
rules.html)
CArrays
(http://www.techcrashcourse.com/2015/05/c
programming
arrays.html)
CMultiDimensional
Arrays
(http://www.techcrashcourse.com/2015/05/c
programmingmulti
dimensional
arrays.html)
CStrings
(http://www.techcrashcourse.com/2015/05/c
programming
strings.html)
CStructures
(http://www.techcrashcourse.com/2015/06/c
programming
structure.html)
CPointers
(http://www.techcrashcourse.com/2015/08/c
programming
pointers.html)
CPointerArithmetic
(http://www.techcrashcourse.com/2015/08/c
programmingpointer
arithmetic.html)
CPassingPointers
(http://www.techcrashcourse.com/2015/08/c
programmingpassing
pointerfunction.html)
CPointertoaPointer
(http://www.techcrashcourse.com/2015/08/c
programmingpointer
topointer.html)
CArrayofPointers
(http://www.techcrashcourse.com/2015/08/c
programmingarrayof
pointers.html)
CPointersto
Structure
(http://www.techcrashcourse.com/2015/06/c
programmingpointer
tostructure.html)
CNestingof
Structures
(http://www.techcrashcourse.com/2015/06/c
programmingnesting
ofstructures.html)
CUnion
(http://www.techcrashcourse.com/2015/06/c
programming
union.html)
CTypedef
(http://www.techcrashcourse.com/2015/06/c
programmingtypedef
keyword.html)
CFileHandling
(http://www.techcrashcourse.com/2015/06/c
programmingfile
handlinginput

output.html)
CPreprocessor
Directives
(http://www.techcrashcourse.com/2015/06/c
programming
preprocessor
directives.html)
CGRAPHICS
CGraphicsTutorial
(http://www.techcrashcourse.com/2015/08/c
graphicsprogramming
tutorial.html)
DrawaCircle
(http://www.techcrashcourse.com/2015/08/c
programdrawcircle
graphics.html)
DrawBarGraph
(http://www.techcrashcourse.com/2015/08/c
programdrawbar
graphusing
graphics.html)
Draw3DBarGraph
(http://www.techcrashcourse.com/2015/08/c
programdraw3dbar
graphusing
graphics.html)
DrawSineWave
(http://www.techcrashcourse.com/2015/08/c
programdrawsine
graphwave
graphics.html)
DrawPieChart
(http://www.techcrashcourse.com/2015/08/c
programdrawpie
chartusing
graphics.html)
MakeaDigitalClock
(http://www.techcrashcourse.com/2015/08/c
programmakedigital
clockusing
graphics.html)
BouncingBall
Animation
(http://www.techcrashcourse.com/2015/08/c
programbouncingball
animation
graphics.html)
MovingCarAnimation
(http://www.techcrashcourse.com/2015/08/c
graphicsprogram
movingcar
animation.html)
DesignPattern
ObserverPattern
(http://www.techcrashcourse.com/2015/10/observer
designpatternin
java.html)
StatePattern
(http://www.techcrashcourse.com/2015/10/state
designpatternin
java.html)
StrategyPattern
(http://www.techcrashcourse.com/2015/10/strategy
designpatternin
java.html)
FilterPattern
(http://www.techcrashcourse.com/2015/10/filter
designpatternin
java.html)
CompositePattern
(http://www.techcrashcourse.com/2015/10/composite
designpatternin

java.html)
DecoratorPattern
(http://www.techcrashcourse.com/2015/10/decorator
designpatternin
java.html)
MementoPattern
(http://www.techcrashcourse.com/2015/10/memento
designpatternin
java.html)
FacadePattern
(http://www.techcrashcourse.com/2015/10/facade
designpatternin
java.html)
BridgePattern
(http://www.techcrashcourse.com/2015/10/bridge
designpattern.html)
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML7&action=editWidget&sectionId=sidebar
left1)
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML8&action=editWidget&sectionId=sidebar
left1)
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML3&action=editWidget&sectionId=sidebar
right1)
Cprogramtocheckstringispalindrome
(http://www.techcrashcourse.com/2014/10/c
programfindpalindromestring.html)
Cprogramforfibonacciseriesusingrecursion
(http://www.techcrashcourse.com/2015/03/c
programfibonacciseriesusingrecursion.html)
Cprogramtocheckyearisleapyearornot
(http://www.techcrashcourse.com/2014/10/c
programcheckleapyear.html)
Cprogramtogeneraterandomnumbers
(http://www.techcrashcourse.com/2015/08/c
programgeneraterandomnumbersrand.html)
Cprogramtocalculatepowerofanumber
(http://www.techcrashcourse.com/2014/10/c
programcalculatepowerofnumber.html)
Cprogramtodeleteanelementfromarray
(http://www.techcrashcourse.com/2015/03/c
programtodeleteelementfromanarray.html)
Cprogrammingdatatypes
(http://www.techcrashcourse.com/2015/05/c
programminglanguagedatatypes.html)
Cprogrammingstructure
(http://www.techcrashcourse.com/2015/06/c
programmingstructure.html)
Cprogrammingswitchstatement
(http://www.techcrashcourse.com/2015/05/c
programmingswitchstatement.html)
FunctiondefinitioninC
(http://www.techcrashcourse.com/2015/05/c
programmingfunctiondefinition.html)
CProgrammingHeaderFiles
(http://www.techcrashcourse.com/2015/06/c
programmingheaderfiles.html)
CGraphics
(http://www.techcrashcourse.com/2015/08/c
graphicsprogrammingtutorial.html)
Cprogramtofindhcfandlcmoftwonumbers
(http://www.techcrashcourse.com/2014/10/c
programfindhcfandlcm.html)
Cprogrammingswitchstatement
(http://www.techcrashcourse.com/2015/05/c
programmingswitchstatement.html)
Cprogramtosortcharactersofastring
(http://www.techcrashcourse.com/2014/11/c
programsortcharactersofstring.html)

CProgrammingExamples
(http://www.techcrashcourse.com/2014/10/c
programexamples.html)
Cprogramtomakecalculatorusingswitch
(http://www.techcrashcourse.com/2015/08/c
programmakesimplecalculatorswitchcase.html)
Cprogramtoconvertdecimaltobinary
(http://www.techcrashcourse.com/2015/08/c
programtoconvertdecimalnumberbinary.html)
Cprogrammingmultidimensionalarrays
(http://www.techcrashcourse.com/2015/05/c
programmingmultidimensionalarrays.html)
Cprogrammingloopsandjumps
(http://www.techcrashcourse.com/2015/05/c
programmingloops.html)Cprogrammingtutorialsfor
beginners
(http://www.techcrashcourse.com/2015/05/c
programminglanguagetutorial.html)
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML5&action=editWidget&sectionId=sidebar
right1)

PopularPosts
CProgramtoPrintTriangleandPyramid
patternsofStar(*)CharacterUsingLoop
(http://www.techcrashcourse.com/2015/08/c
programprinttrianglepyramidpattern
star.html)
Writeaprograminctoprintpyramidand
trianglepatternsusingloopThisProgramfirst
takesthenumbersofrowsinpatternand
then...
CProgramtoConvertDecimalNumbersto
BinaryNumbers
(http://www.techcrashcourse.com/2015/08/c
programtoconvertdecimalnumber
binary.html)
Writeaprograminctoconvertadecimal
number(base10)tobinarynumber(base2)
Writeaprograminctoconvertabinary
number(base2)...
CProgramtoPrintFibonacciSeriesusing
Recursion
(http://www.techcrashcourse.com/2015/03/c
programfibonacciseriesusingrecursion.html)
Writeaprograminctoprintfibonacciseries
usingrecursionFibonacciseriesarethe
numbersinthefollowingintegersequence0,...
CProgramtoPrintAllPrimeNumbersbetween
1toN
(http://www.techcrashcourse.com/2015/11/c
programtoprintallprimenumbersbetween
1toN.html)
WriteaCprogramtoprintallprimenumbers
between1toNusingforloop.WapinCtoprint
primenumbersbetween1to100.Required
K...
CGraphicsProgrammingTutorial
(http://www.techcrashcourse.com/2015/08/c
graphicsprogrammingtutorial.html)
ThisCGraphicstutorialsisforthosewhowant
tolearnfundamentalsofGraphics
programming,withoutanypriorknowledgeof
graphics.T...
CProgramtoDrawaCircleUsingCGraphics
(http://www.techcrashcourse.com/2015/08/c
programdrawcirclegraphics.html)
WriteaprograminCtodrawacircleonscreen
usinggraphics.hheaderfileInthisprogram,
wewilldrawacircleonscreenhavingce...
CProgramforMovingCarAnimationUsingC
Graphics
(http://www.techcrashcourse.com/2015/08/c

graphicsprogrammovingcaranimation.html)
WriteaprograminCformovingcaranimation
usinggraphics.hheaderfileInthisprogram,
wewillfirstdrawacarandcolorit.Ine...
CProgramtoPrintOddNumbersBetween1to
100usingForandWhileLoop
(http://www.techcrashcourse.com/2015/11/c
programtoprintalloddnumbersbetween1
to100usingloop.html)
WriteaCprogramtoprintoddnumbers
between1to100usingforloop.WriteaC
programtoprintalloddnumbersbetween1to
Nusingwh...
CProgramtoPrintEvenNumbersBetween1
to100usingForandWhileLoop
(http://www.techcrashcourse.com/2015/11/c
programtoprintallevennumbersbetween1
to100usingloop.html)
WriteaCprogramtoprintevennumbers
between1to100usingforloop.WriteaC
programtoprintallevennumbersbetween1
toNusing...
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=PopularPosts&widgetId=PopularPosts1&action=editWidget&sectionId=sidebar
right1)
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML2&action=editWidget&sectionId=sidebar
right1)

Home(http://www.techcrashcourse.com)|Contactus(http://www.techcrashcourse.com/p/contact.html)|About(http://www.techcrashcourse.com/p/about.html)
(//www.blogger.com/rearrange?blogID=8051473532172930360&widgetType=HTML&widgetId=HTML4&action=editWidget&sectionId=footer1)
(//www.blogger.com/rearrange?blogID=8051473532172930360&widgetType=HTML&widgetId=HTML9&action=editWidget&sectionId=footer1)
(//www.blogger.com/rearrange?
(//www.blogger.com/rearrange?
blogID=8051473532172930360&widgetType=HTML&widgetId=HTML10&action=editWidget&sectionId=footer blogID=8051473532172930360&widgetType=HTML&widgetId=HTM
21)
22)
Copyrightbytechcrashcourse.com|Allrightsreserved|.PoweredbyBlogger(https://www.blogger.com).
(//www.blogger.com/rearrange?blogID=8051473532172930360&widgetType=Attribution&widgetId=Attribution1&action=editWidget&sectionId=footer3)

Você também pode gostar