Write a algorithm to determine an array ascendant using recursion
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
bool is_array_ascendent(int arr[], int len, int idx)
{
if(len == 1){
return true;
}
if(idx == len -2){
return arr[idx] < arr[idx + 1];
}
if(arr[idx] > arr[idx + 1]){
return false;
}
return is_array_ascendent(arr, len, idx + 1);
}
int main(int argc, char* argv[])
{
srand((unsigned)time(NULL));
int arr[10];
for(int i = 0; i < 10; i++){
arr[i] = 5 * i;
cout << arr[i] << " ";
}
cout << endl << "The array is ascendent ? " << is_array_ascendent(arr, 10, 0) << endl;
for(int i = 0; i < 10; i++){
arr[i] = rand() % 100;
cout << arr[i] << " ";
}
cout << endl << "The array is ascendent ? " << is_array_ascendent(arr, 10, 0) << endl;
return 0;
}
Output
0 5 10 15 20 25 30 35 40 45
The array is ascendent ? 1
37 71 82 74 65 99 5 87 70 43
The array is ascendent ? 0