Following situation: I have an array with some constant values, which represent ranges.
A range is always between two values in the array, e.g.: 10 – 20 = range1
20-30 = range2 and so on…
const int arr[] = {10, 20, 30, 40, 50, 60};
With a Search function, I search for the number(val) between those ranges in arr[] and return the range index where val was found.
For example: if val = 15 → return value would be 1
if val = 33 → return value would be 3
int Search(const int arr[], int n, int val)
{
int i = 0;
while (i < n) {
if (val > arr[i])
++i;
else
return i;
}
return -1;
}
OK, this works out so far…
Now following problem:
I have some parameters let’s call them x, y, z which are simple integers and they depend on the value of val.
The parameter values for x, y, z I know already before compilation, of course they are different for every range.
How can I now set x, y and z using the range index?
How can I make an array for example with the constant parameter values for x, y, z and set them depending on the returned range index? Or should it be a struct?
How would that look like…?
Thx
You could hold the parameters for each range in a
struct:And keep all these structs in a
std::vector:Adding the data would be done like this:
So finally you can access the parameters for range
nasparams[n-1].