[英]recursive C++ function that takes as input an array of integers n then prints the maximum and the minimum value in the array without using any loops
Hello I have a homework question I am stuck in...any hint or tips would be appreciated. 您好我有一个家庭作业问题,我无法解决。任何提示或技巧将不胜感激。 the question is:
问题是:
Write a short recursive C++ function that takes as input an array of integers of size n then prints the maximum and the minimum value in the array without using any loops. 编写一个简短的递归C ++函数,该函数将大小为n的整数数组作为输入,然后在不使用任何循环的情况下打印该数组中的最大值和最小值。
I have the following so far: 到目前为止,我有以下内容:
/* Precondition: the function is given the array, first Index and last index, takes two int variable maxim, minim set to 0
also takes a bool isFirst. Not sure if I really need the bool isFirst to display the min and max values from the
function it self.
Postcondition: the function displays the maximum and the minimum in the array.
*/
void findMaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim, bool isFirst)
{
if (firstIndex == lastIndex) // Base case one element in the array
{
maxim = A[firstIndex];
minim = A[firstIndex];
return;
}
else
{
int myMin = 0;
int myMax = 0;
findMaxMin(A, firstIndex, (lastIndex-1), myMin, myMax, false);
if(A[lastIndex] < myMin)
myMin = A[lastIndex];
if (A[lastIndex] > myMax)
myMax = A[lastIndex];
maxim = myMax;
minim = myMin;
cout<<"mymin now is: "<< myMin<<endl;
cout<< "mymax now is: "<<myMax<<endl;
//if (isFirst)
// cout<< " my min is" << minim << " my max is"<< maxim<< endl;
}
}
however this is not working as it should be.. not sure where the problem is. 但是,这不能正常工作..不确定问题出在哪里。
the divide and conquer method attempt 1: this works only problem is that it prints the minimum and maximum on every recursive call it makes but the code seems to be correct. 尝试使用分而治之方法1:唯一有效的问题是,它在每次进行的递归调用上都会打印最小值和最大值,但是代码似乎是正确的。 Solution to the cout problem I found was to print from the main but the question specifies that the print be made from inside the function;
我发现的cout问题的解决方案是从主体打印,但问题是指定从函数内部进行打印; but could not find a way to print from the function only the last comparison;
但是找不到从函数中仅打印最后一次比较的方法; it always prints on every recursive call it makes
它总是在它进行的每个递归调用上打印
void MaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim)
{
if(firstIndex == lastIndex) // base case one element
{
maxim = A[firstIndex];
minim = A[firstIndex];
return;
}
else if(firstIndex == lastIndex -1) // base case two element
{
if(A[firstIndex] < A[lastIndex])
{
maxim = A[lastIndex];
minim = A[firstIndex];
}
else
{
maxim = A[firstIndex];
minim = A[lastIndex];
}
}
else // there are more than two elements in the array
{
int mid = (firstIndex+lastIndex)/2;
MaxMin(A,firstIndex,mid,maxim,minim);
int maxim1 =0;
int minim1 =0;
MaxMin(A,mid+1,lastIndex, maxim1, minim1);
if(maxim < maxim1)
{
maxim = maxim1;
}
if(minim > minim1)
{
minim = minim1;
}
}
//this is printed on every recursive call. //这在每个递归调用上都打印。
cout<<"my minimum now is "<<minim<<endl;
cout<<" my maximum now is "<<maxim<<endl;
}
The problem is in that function declaration int &maxim
is before int &minim
and in the recursive call you put minimum value first. 问题在于函数声明
int &maxim
在int &minim
之前int &minim
并且在递归调用中将最小值放在第一位。 So you need to call: 因此,您需要致电:
findMaxMin(A, firstIndex, (lastIndex-1), myMax, myMin, false);
findMaxMin(A,firstIndex,(lastIndex-1),myMax,myMin,false);
The way you structured your code also firstIndex
can only be 0. So you can remove this parameter and replace with 0. Also isFirst
is not needed. 您的代码的结构方式
firstIndex
也只能为0。因此,您可以删除此参数并替换为0。而且isFirst
不需要。 You can simplify further if you use directly minim
, maxim
instead of myMin
, myMax
. 如果直接使用
minim
, maxim
代替myMin
, myMax
,则可以进一步简化。
As a hint and not a definete solution. 作为提示,而不是确定的解决方案。 Any loop of the form
形式的任何循环
for(int loopIndex, loopContinueCondition(loopIndex), loopIncrement(loopIndex))
can be converted to a recursion of the form 可以转换为以下形式的递归
ResultType my_for(int loopIndex, ResultType partialResult, ...) {
... //normal loop body
newPartialResult = ...;
if (loopContinueCondition(loopIndex)) {
loopIncrement(loopIndex);
return my_for(loopIndex, newPartialResult, ...);
} else
return newPartialResult;
}
where ...
represents the additional data normally available in the loop body. 其中
...
表示循环主体中通常可用的其他数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.