Deitel C++ How To Program 9th Edition Chapter 7 Exercise 7.32

17 Jul 2024 - Syed Muhammad Shahrukh Hussain

Write a program which finds minimum value within an array using recursive function.

Terminal

Minimum:8

Solution

#include <iostream>
#include <array>

using namespace std;

int recursiveMinimum(array<int, 10> &arr, int start, int end);

int main() {
  array < int, 10 > arr =  { 26, 14, 15, 23, 32, 10, 13, 11, 10, 8 };
  cout << "Minimum:" <<recursiveMinimum(arr, 0, 9) << endl;
  return 0;
}

int recursiveMinimum(array<int, 10>  &arr, int start, int end) {
  if (start < end) {
    int next =  recursiveMinimum(arr, start + 1, end);
    if (arr[start] <= next)
      return arr[start];
    else
      return next;
  }
  // reach the end of array now return the value of last element.
  // this value will be than equal to next and compared with previous functional call.
  return arr[start];
}

Sources