Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,915 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,182 people online right now. Registration is fast and FREE... Join Now!




help with find() in c++

 
Reply to this topicStart new topic

help with find() in c++

prads
11 Dec, 2007 - 01:31 AM
Post #1

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
Hello,
I want to check the values in an array and output its indices if found to be greater than some threshold.....here's what i tried to do......
CODE

#include<stdio.h>
using namespace std;
int main()
{
int i=5,index;
double store_xcorr_sum[10]={2.0,2.1,5.0,7.5,3.5};
for(i=0;i<4;i++)
{
index = find(abs(store_xcorr_sum[i]) > 3);
cout<<index;
}
getchar();
return 0;
}


but this is not running....... should i use something like this....
CODE
find(store_xcorr_sum[0],store_xcorr_sum[4], store_xcorr_sum[i]>3)        

can someone please help me out..
Thanks,
prads
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help With Find() In C++
11 Dec, 2007 - 01:54 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Are you set on the use of the find or find_if function from algorithm? If so, you should be including <algorithm>. And you should also be including <iostream>, otherwise the cout object isn't defined. This code should not have compiled as is.

If you just need to output all of the indices for the store_xcorr_sum[i] > 3, then you can just go element by element in a loop:
CODE
    for (int i=0; i<10; ++i) {
        if (store_xcorr_sum[i] > 3.0) {
            cout << i << " ";
        }
    }


If you really want to use one of the methods from <algorithm>, you'll need to use find_if method with a unary predicate...you can read a little more about that method here or here. You won't be able to use a relational operator in find as you can in MATLAB.

With a little more detail about the context, we may be able to offer better suggestions about what would best suit your purposes. Are you going to want to store these indices in an array, or is just having them element by element good enough?
User is offlineProfile CardPM
+Quote Post

prads
RE: Help With Find() In C++
11 Dec, 2007 - 02:07 AM
Post #3

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
hmm......i want it in an array........and here's what i thought i should do.......
CODE

j=0;
for(i=0;i<z;i++)
{
if(store_xcorr_sum[i]>153)
{
index[j]=i;
j++;
}
}


this should give me and array that has all the indices lined up.......
thanks,
prads
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help With Find() In C++
11 Dec, 2007 - 02:13 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
If I needed the indices stored in an array, that's pretty much what I'd do. Did you have a question about this, or are you just posting the final resolution?

In any event, that looks pretty clean, and I can't see any major problems that could come of it (assuming that you've declared arrays of sufficient size).
User is offlineProfile CardPM
+Quote Post

prads
RE: Help With Find() In C++
11 Dec, 2007 - 02:18 AM
Post #5

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
QUOTE(jjhaag @ 11 Dec, 2007 - 03:13 AM) *

If I needed the indices stored in an array, that's pretty much what I'd do. Did you have a question about this, or are you just posting the final resolution?

In any event, that looks pretty clean, and I can't see any major problems that could come of it (assuming that you've declared arrays of sufficient size).


nope its the final solution and i have no more doubts on that......however i have a few on some other functions like isempty and more......but will post it in a few minutes........
thanks,
prads
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:14PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month