read next post
The OP's post in a more readable format
Hi All,
Im a self learning c++ programmer with a few months of experience. My knowledge is okay at this language i am just struggling with my project that i am trying to do.
In my project, I want to be able to search a float and it will return an integer and itself
I have tried to use a 2D Array but i cannot seem to find away to search inside without it being too long with switch statements for example. My Array holds a number of 201 elements from 0-200.
I dont know if this is a good way to go forward with a 2D Array, please let me know otherwise
If anyone has any advice please help me out with resources and knowledge.
Thanks
What do you mean by “search a float”? Are you trying to find an exact march, or the closest one, or one that is less than but as close as possible or what?
Also, you don’t need a 2D array if indeed the integers you store are just in order and would be equal to the index in every case...
Also do you know about “for” loops? This does not seem like a difficult problem, perhaps if you show how this search is to be used it would give some clue as to why you are making it seem hard.
a7
Why do you need the integers in the array? As far as I can see, they're just the index of the entry in the array so you can just have an array of floats and search through it with a for loop.
Be careful with checking the floats - don't test for equality, check that the number you're seeking is close enough to the one you provided.
It also looks at a glance like the floats are in order and increase linearly. If this is the spcase, all you need is a formula, no searching or arrays.
a7
Hi,
Sorry i am not clear in my post.
The floats in the array are a frequency and the integer beside is its equivalent mmH2O Level. I want to be able to input a frequency and it will return its level.
For example
if i input "44.676" I would like the program to return "0" as it is its equivalent level, and so on with the other 200.
OK so that's not quite the case.
But these floats are very well behaved and a simple mathematical formula would more than adequately describe them, probably no more than a quadratic equation close enough.
a7
That is also quite a big array. Over 1600 bytes.
You can do a "fuzzy" search like this example on a single dimension array where the index is the value to be returned (untested) :
const float tolerance = 0.01 ;
float search = 42.6275 ;
for ( int i = 0 ; i < 201 ; i ++ ) {
if ( ( array[ i ] >= search - tolerance ) && ( array[ i ] <= search + tolerance ) ) {
// match found at current array index i
break ;
}
}
alto777:
But these floats are very well behaved and a simple mathematical formula would more than adequately describe them, probably no more than a quadratic equation close enough.
Excel's solver can give you an equation for it.
jacklythgoee:
The floats in the array are a frequency and the integer beside is its equivalent mmH2O Level. I want to be able to input a frequency and it will return its level.
The integers still look awfully like indices.
Either way, the values don't belong in RAM.
Twelve minutes and an online curve fitting tool yields
y = −0.162378 x^2 - 21.6586 x + 1292.48
which is very close, imagine what someone who cared might come up with in an hour.
a7
alto777:
Twelve minutes and an online curve fitting tool yieldsy = −0.162378 x^2 - 21.6586 x + 1292.48
which is very close, imagine what someone who cared might come up with in an hour.
a7
Thank you, but all of this stuff is new to me as i said i had only been coding for a few months. This is too much for me to handle. Basically calling myself stupid.
The floats in the array are a frequency and the integer beside is its equivalent mmH2O Level. I want to be able to input a frequency and it will return its level.
Instead of frequency, how about using time ... then everything can be referenced using integers.
It really does seem like some formula (alto777) could give you whats needed ... what's the application?
Example:
seconds = 1 / Hz
µs = 1,000,000/Hz
so for 40Hz, store 25000 µs
for 40.1587Hz, store 24,901 µs
The datasheet for your pressure sensor should give the information you need (formulas, etc.)
Hi,
My project is to input a a frequency and then return its equivalent value.
I don't have a clue where to start on this project and i am looking for some help and guidance.
Anyone that can produce something will be appreciated just know that you dont have to all i want is guidance:)
mmH2O Level | Frequency |
---|---|
0 | 44.676 |
1 | 44.64978 |
2 | 44.62352 |
3 | 44.59721 |
4 | 44.57086 |
5 | 44.54445 |
6 | 44.518 |
7 | 44.49151 |
8 | 44.46497 |
9 | 44.43839 |
10 | 44.41176 |
11 | 44.38509 |
12 | 44.35838 |
13 | 44.33162 |
14 | 44.30482 |
15 | 44.27799 |
16 | 44.25111 |
17 | 44.22419 |
18 | 44.19723 |
19 | 44.17023 |
20 | 44.1432 |
These are the list of values that i need. However itis much bigger. It goes from 0-200.
You should mention what Arduino board that you are using.
I put those values into a spreadsheet, graph them and got a trend line. The relationship looks linear and the equation is:
f(X) = -0.0266650X + 44.6776982
Floating point math is not real accurate on 8 bit Arduinos. You will need to see if it is accurate enough?
You could use a table but 200 floats is 800 bytes, a big chunk of memory on some Arduinos, but as accurate as you want it within the limitations of the board that you have.
Duplicate topics merged
Why did you start a second one ?
@groundFungus yes, no. You'll see now in the merge that it is in fact the inverse of the function you supply, and the longer list of pairs reveals a non-linear but pretty close to quadratic relation.
a7
alto777, I was not privy to the prior posts.
jacklythgoee, Thank you so much for allowing me to waste my time trying to help you.
@groundFungus yeah, I figured. Kinda wish now I hadn’t been privy to any of this, but I did find a few online curve fitting websites after seeing that the Excel solver isn’t part of my Excel from never mind his long ago… so not a total waste of time.
a7