I need help trying to find the min and max values in an array recursively in c++. the functions were given and cannot be changed.
I tried it out for both but for some reason nothing happens and the code does not enter the loop and I want to know what I am doing wrong. Here is my main and the min and max functions.
int main()
{
int array[] = { 46, 22, 7, 58, 91, 55, 31, 84, 12, 78 };
if (findMax(array, 10) == 91)
{
cout << "findMax is correct!" << endl;
}
if (findMin(array, 10) == 7)
{
cout << "findMin is correct!" << endl;
}
int findMax(int array[], int size)
{
int i = (size - 1);
int max = 0;
if (array[0] < array[i]) {
max = array[i];
findMax(array, size - 1);
}
return max;
return 0;
}
int findMin(int array[], int size)
{
int i = 0;
int j = size - 1;
if (i == j)
{
return array[i];
i++;
}
int temp = findMin(array, size);
if (array[i] < temp)
{
return array[i];
}
else
{
return temp;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…