(a) Write a recursive function
int find(const int A[], int n, int x);
which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found.
(b) Write a recursive function
int rfind(const int A[], int n, int x);
which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found.
(c) Write a program search.cc which asks the user to enter the size of an array, the elements of an array, and an element to search for. For each set of input, the program should print the results of find and rfind. You should allocate the array dynamically. The program should be terminated when 0 is entered for the size of the array.
Solution:
#include <stdio.h>
#include <stdlib.h>
int currIndex = 0;
int find(const int A[], int n, int x){
if(n==currIndex){
return n;
}
if(A[currIndex] == x){
return currIndex;
}
currIndex++;
return find(A,n,x);
}
int rfind(const int A[], int n, int x){
if(-1==currIndex){
return n;
}
if(A[currIndex] == x){
return currIndex;
}
currIndex--;
return find(A,n,x);
}
int main(){
int x,n,i,*ptr;
printf("Enter the size of an array : ");
scanf("%d",&n);
if(n<=0){
printf("Size can't be negative or zero");
exit(0);
}
ptr = (int*) malloc(n*sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter all %d elements :\n",n);
for(i=0; i<n; i++){
printf("Enter %d element :",i+1);
scanf("%d",ptr +i);
}
printf("Enter an element to search : ");
scanf("%d",&x);
printf("\nfind() return value = %d \n",find(ptr,n,x));
currIndex = n-1;
printf("rfind() return value = %d \n ",rfind(ptr,n,x));
currIndex = 0;
free(ptr);
}
Output:

(a) Write a recursive function int find(const int A[], int n, int x); which returns the...
may i ask for help with this c++ problem?
this is the code i have for assignment 4 question 2:
#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
int main()
{
string inputStr;
stack <int> numberStack;
cout<<"Enter your expression::";
getline(cin,inputStr);
int len=inputStr.length();
stringstream inputStream(inputStr);
string word;
int val,num1,num2;
while (inputStream >> word)
{
//cout << word << endl;
if(word[0] != '+'&& word[0] != '-' && word[0] !=
'*')
{
val=stoi(word);
numberStack.push(val);
// cout<<"Val:"<<val<<endl;
}
else if(word[0]=='+')
{
num1=numberStack.top();
numberStack.pop();
num2=numberStack.top();
numberStack.pop();...
13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int x) that has as parameters an array numbers with n > 0 elements, and an integer x, and returns how many times I appears in the array. 14. Write a recursive function with the declaration: double dist(double* u, double *v, int n) that gets two double precision arrays with n > 1 elements and returns the value: Vu[0] - v[0])2 + (u[1] – v[1])2...
In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
Write a generic function int find_lower_bound(T seq[], int n, const T& value). The function returns the index of the largest element in the given sequence that is less than the given value. If multiple elements satisfy, return the one with smallest index. Return -1 if no such element exists. //main.cpp #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; #include "source.h" struct Point{ int x,y; bool operator<(const Point& p) { return (x<p.x || (x==p.x&&y<p.y)); } }; int...
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...
What is the run-time of this recursive code: int maximum(int array[], int index, int len); //define maximum function. int minimum(int array[], int index, int len);//define minimum function. int main() { //Main method int array[MAX_SIZE], N, max, min; //Defining all the variable. int i; printf("Enter size of the array: ");//Taking input from user as a array size scanf("%d", &N); printf("Enter %d elements in array: ", N);//Taking input from user for(i=0; i<N; i++) { scanf("%d", &array[i]);//storing all the element in array. }...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...