I’ve been stuck on a recursion assignment for hours now.
Basically its recursion practice and I completed everything except one thing:
Find the amount of numbers in range within an array.
I have to use recursion, and pointers of the array. So far I’ve done find the sum of array and find if the array HAS the numbers within range. No problem.
BUT, now I have to find how many occurences within the range it is. I can do it with a loop and using a "counter index" variable, but I must use recursion.
I can’t seem to get it to work.
Right now my attempt is this:
int numberOfValuesInRange(float min, float max, const float *pBegin, const float *pEnd){
if(pBegin >= pEnd) // base case
return 0;
if (min+max - *pBegin >= min+max)
return 0 + numberOfValuesInRange(min, max, pBegin, pEnd);
}
I get nothing.
And below is the other bits of code for sum and a bool if it has numbers in range.
Sum of array.
float sum(const float *pBegin, const float *pEnd){ //Adds all elements in array of float numbers and returns their sum
if (pBegin >= pEnd)
return 0;
return *pBegin + sum(pBegin+1, pEnd);
}
And check if array has values in range:
bool hasValueInRange(float min, float max, const float *pBegin, const float *pEnd){ //Checks if array elements are within min/max range
if(pEnd > pBegin && *pBegin <= max && *pBegin >= min){
cout << "Array DOES contain values in range " << endl;
return true;}
else{
cout << endl << "Array does NOT contain values in range " << endl;
return false;
}
}
Here is my main function (If it helps):
int main(){
const int size = 5;
float sampleArray[5] = {2, 4, 6, 0, 1};
sum(&sampleArray[0], &sampleArray[size])
hasValueInRange(0, 5, &sampleArray[0], &sampleArray[size]); //Checks for values between 0 and 5
int x = 0;
x = numberOfValuesInRange(2,5, &sampleArray[0], &sampleArray[size]); //Occurences within range 2 to 5
cout << x; // Should be 2
return 0;
Any help would be lovely, thanks!
I know its just one piece of the puzzle missing…
>Solution :
If the number is in range add one
if ((*pBegin) >= min && (*pBegin) <= max)
return 1 + numberOfValuesInRange(min, max, pBegin+1, pEnd);
Code:
#include <iostream>
using namespace std;
int numberOfValuesInRange(float min, float max, const float *pBegin, const float *pEnd){
if(pBegin >= pEnd) // base case
return 0;
//if (min+max - *pBegin >= min+max)
// return 0 + numberOfValuesInRange(min, max, pBegin, pEnd);
if ((*pBegin) >= min && (*pBegin) <= max)
return 1 + numberOfValuesInRange(min, max, pBegin+1, pEnd);
return numberOfValuesInRange(min, max, pBegin+1, pEnd);
}
float sum(const float *pBegin, const float *pEnd){ //Adds all elements in array of float numbers and returns their sum
if (pBegin >= pEnd)
return 0;
return *pBegin + sum(pBegin+1, pEnd);
}
bool hasValueInRange(float min, float max, const float *pBegin, const float *pEnd){ //Checks if array elements are within min/max range
if(pEnd > pBegin && *pBegin <= max && *pBegin >= min){
cout << "Array DOES contain values in range " << endl;
return true;}
else{
cout << endl << "Array does NOT contain values in range " << endl;
return false;
}
}
int main(){
const int size = 5;
float sampleArray[5] = {2, 4, 6, 0, 1};
cout<<sum(&sampleArray[0], &sampleArray[size])<<endl;
hasValueInRange(0, 5, &sampleArray[0], &sampleArray[size]); //Checks for values between 0 and 5
int x = 0;
x = numberOfValuesInRange(2,5, &sampleArray[0], &sampleArray[size]); //Occurences within range 2 to 5
cout <<x<<endl; // Should be 2
return 0;
}
Output :
$ g++ recurssion.cpp && ./a.out
13
Array DOES contain values in range
2