1 Write a recursive int function that returns the position o

1. Write a recursive int function that returns the position of a given value in an array. If the value is not in the array, return -1. Call this function from main and output the result in main. Test it with a value that is in the array and another value that is not in the array. That is, calling this function twice. If the value is the first element of the array, return 1 instead of 0.

2. Input a large integer value (long int) and output it in the following format using recursion: e.g. input 12345678 Output 2,345,678

PROGRAM IN C++

Thanks

Solution

#include <iostream>
using namespace std;
int recursiveSearch(int numbers[],int len,int search);
char[] formatnumber(long int n,char digits[])
int main() {
int numbers[] = {1,2,3,4,5};
cout << recursiveSearch(numbers,5,1)<<endl;
cout << recursiveSearch(numbers,5,6)<<endl;
}

int recursiveSearch(int numbers[], int len, int search)
{
int location;
if (numbers[len] == search)
{
return len+1;
}
else if (len == -1)
{
return -1;
}
else
{
return (location = recursiveSearch(numbers, len - 1, search));
}
}

/*
sample output
1
-1
*/

1. Write a recursive int function that returns the position of a given value in an array. If the value is not in the array, return -1. Call this function from m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site