example 6_1.cpp

来自「data+structures+using+c的源码」· C++ 代码 · 共 35 行

CPP
35
字号
//Largest Element in an Array

#include <iostream>

using namespace std;

int largest(const int list[], int lowerIndex, int upperIndex);

int main()
{
	int intArray[10] = {23, 43, 35, 38, 67, 12, 76, 10, 34, 8};

	cout<<"The largest element in intArray: "
	    <<largest(intArray,0,9);
	cout<<endl;

	return 0;
}

int largest(const int list[], int lowerIndex, int upperIndex)
{
   int max;

   if(lowerIndex == upperIndex)   //the size of the sublist is 1
   		return list[lowerIndex];
   else
   {
		max = largest(list, lowerIndex + 1, upperIndex); 
		if(list[lowerIndex] >= max)
			return list[lowerIndex];
		else
			return max;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?