1 / 35
文档名称:

数据结构与算法分析.docx

格式:docx   大小:61KB   页数:35
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

数据结构与算法分析.docx

上传人:zhuwo11 2022/3/27 文件大小:61 KB

下载得到文件列表

数据结构与算法分析.docx

相关文档

文档介绍

文档介绍:Chapter10:
ADTImplementations:Recursion,AlgorithmAnalysis,andStandardAlgorithms
ProgrammingProblems1.
/*Driverprogramoublex,intn)
{
if(n==0)
return1;
elseif(n<0)
returnpower(x,n+1)/x;
else
returnpower(x,n-1)*x;
}.
7-9.
/*
Driverprogramtotesttherecursivearrayreversal,arraysum,andarraylocationfunctionsofExercise27-29.
Input:Integerexponentsandrealbases
Output:Eachrealtothatintegerpower
*/
#include<iostream>
usingnamespacestd;
typedefintElementType;
constintCAPACITY=100;
typedefElementTypeArrayType[CAPACITY];
voidreverseArray(ArrayTypearr,intfirst,intlast);
/*
Recursivelyreverseanarray--.
Precondition:Arrayarrhaselementsinpositionsfirstthroughlast.
Postcondition:Elementsinpositionsfirstthroughlastofarrarereversed.
*/
ElementTypesumArray(ArrayTypearr,intn);
/*
Recursivelysumtheelementsofanarray--.
Precondition:Arrayarrhasnelements.
Postcondition:Sumoftheelementsisreturned
*/
intlocation(ArrayTypearr,intfirst,intlast,ElementTypeitem);
/*
Recursivelysearchtheelementsofanarray--.
Precondition:Arrayarrhaselementsinpositionsfirstthroughlast.
Postcondition:Locationofiteminarrisreturned;-1ifitemisn'tfound.
*/
voiddisplay(ArrayTypearr,intn);
/*
Displaytheelementsofanarray.
Precondition:Arrayarrhasnelements.
Postcondition:Elementsofarrhavebeenoutputtocout.
*/
intmain()
{
ArrayTypex;
cout<<"Enteratmost"<<CAPACITY<<"integers(-1tostop):\n";intitem,count;
for(count=0;count<CAPACITY;count++)
{
cin>>item;
if(item<0)break;
x[count]=item;
}
cout<<"Originalarray:";
display(x,count);
reverseArray(x,0,count-1);
cout<<"Reversedarray:";
display(x,count);
cout<<"\nSumofarrayelements="<<sumArray(x,count)<<endl;
ElementTypetoFind;
for(;;)
{
cout<<"Enteranitemtosearchfor(-1tostop):";
cin>>toFind;
if(toFind<0)break;
cout<<"Locationofitem="<<location(x,0,count-1,toFind)<<endl<<"where-1denotesitemnotefound)\n";
}
}
//--DefinitionofreverseArray()voidre