Needing help with arrays with recursive functions

Recommended Posts

Hi guys, I used to have a background in the basics of C++, I could handle most standard if/else, couts, basic data types, functions etc. Played around a lot. Never messed with pointers or arrays or really too in depth with recursive areas.

I'm currently attempting to make a program that will accept a base number, run it through a function and return the number of times it passed through the function before the function ends.

The basics of what I'm attempting to accomplish is this. Take the base number, check it against an array. Based on which position in the array it is, I want the function to change its modifier. Then ++ the counter, and repeat. There will be two user inputs, one for the base number, and one for desired endpoint in the array. Once the desired endpoint in the array is finished, I want the function to cease and print the result of the function counter, as well as the endpoint of the base value.

The array will consist of a series of numbers in a random(but preset and already data entered by me), ascending order.

What I'm having issues with is figuring out how to make a function do something different based on which part of the array it's at. So say I want element 0 to refer to a behavior in the function of BaseValue*5, checks that show base value is now >= element 1 BaseValue*4.9, checks that show base value is now >= element 2 BaseValue*4.8 etc.

So the function will take the user input BaseValue, run it through the function. Function will first check the BaseValue against the array to determine what element it is >=. If it's >= element 2, I want it to multiply the BaseValue by 4.8 in this example. Then store that in an integer. Increase the function count by 1, then check to see if the new stored int is >= the 2nd user input for endpoint. If not, restart function.

It's been a long time since I programmed, and I'm more than happy to help explain anything that might need explaining or provide a more clear example of what I'm looking for assistance with.

So far I've defined my array base values, and set up the gathering of user inputs. Just trying to figure out some pseudo code for the function before I dive in and give myself 1000 errors.

Share on other sites

I don't exactly understand what you're trying to compute, but it sounds like you don't actually need recursion for that. Just an iterative loop (ie a 'for' or 'while' loop) seems to be sufficient from what I understand. However, maybe I am missing some crucial step that makes recursion useful. If you just want recursion as exercise, that's a good reason too.

Anyway, to know which part of the array you are, you can pass an index, or a pointer into the function. The former is probably easier for you.

Something along the lines of

double values[VALUES_COUNT] = { ... };

int f(int index) {
int count = 0;
...
if (index >= 0 && index < VALUES_COUNT) {
// Use values[index]
// count++;
..... // ???
..... // Profit!
}
return count;
}

Function is still missing a lot of functionality, but it's a start

Create an account

Register a new account

• Forum Statistics

• Total Topics
628676
• Total Posts
2984172

• 13
• 12
• 9
• 10
• 9