How to find number of times certain conditions happened in an array?

Discussion in 'C++' started by lightaiyee@gmail.com, Nov 26, 2007.

  1. Guest

    Dear Gurus,

    I would like to implement a function that computes the number of times
    a certain condition is met in a global array.

    For example, I have an global array of size 500.
    float array[500];

    I have a function that finds the maximum of an array of size 50.
    bool findMax50(float input[])

    Next, I want to implement a function that can do the following;

    Sum( findMax50(array), period)
    If period is 20, then this function will start at the last element of
    the global array and will return 1 if this element is a maximum among
    the 50 elements it covers. Then, it moves on to the (last -1)th
    element all the way to (last-20)th element. The Sum() function will
    sum up the number of times maximum happened.

    My greatest difficulty lies in how one can input a function as a
    parameter into another function.

    Can some Guru advise?
     
    , Nov 26, 2007
    #1
    1. Advertising

  2. Guest

    Re: How to find number of times certain conditions happened in anarray?

    On Nov 26, 6:18 am, wrote:
    > Dear Gurus,
    >
    > I would like to implement a function that computes the number of times
    > a certain condition is met in a global array.
    >
    > For example, I have an global array of size 500.
    > float array[500];
    >
    > I have a function that finds the maximum of an array of size 50.
    > bool findMax50(float input[])
    >
    > Next, I want to implement a function that can do the following;
    >
    > Sum( findMax50(array), period)
    > If period is 20, then this function will start at the last element of
    > the global array and will return 1 if this element is a maximum among
    > the 50 elements it covers. Then, it moves on to the (last -1)th
    > element all the way to (last-20)th element. The Sum() function will
    > sum up the number of times maximum happened.
    >
    > My greatest difficulty lies in how one can input a function as a
    > parameter into another function.
    >
    > Can some Guru advise?


    hi
     
    , Nov 26, 2007
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Dear Gurus,
    >
    > I would like to implement a function that computes the number of times
    > a certain condition is met in a global array.
    >
    > For example, I have an global array of size 500.
    > float array[500];
    >
    > I have a function that finds the maximum of an array of size 50.
    > bool findMax50(float input[])
    >
    > Next, I want to implement a function that can do the following;
    >
    > Sum( findMax50(array), period)
    > If period is 20, then this function will start at the last element of
    > the global array and will return 1 if this element is a maximum among
    > the 50 elements it covers. Then, it moves on to the (last -1)th
    > element all the way to (last-20)th element. The Sum() function will
    > sum up the number of times maximum happened.
    >
    > My greatest difficulty lies in how one can input a function as a
    > parameter into another function.
    >
    > Can some Guru advise?


    What you are trying to do is not clear.

    First off, you have findMax50 returning a bool, a true or false. That can't
    express the max of an array.

    Socond your Sum( findMax50(array), period), what is it supposed to return?

    What is it you want at the end? It sounds like you may want to return a
    std::vector from findMax50 which will have the largest 50 values from the
    array. but what is Sum supposed to do? Is it output only? Is it supposed
    to return a value?

    I'm fairly sure what you are trying to achieve is doable, but I can't get a
    grasp on what you are trying to accomplish.
     
    Jim Langston, Nov 26, 2007
    #3
  4. Daniel T. Guest

    wrote:

    > I would like to implement a function that computes the number of times
    > a certain condition is met in a global array.
    >
    > For example, I have an global array of size 500.
    > float array[500];
    >
    > I have a function that finds the maximum of an array of size 50.
    > bool findMax50(float input[])
    >
    > Next, I want to implement a function that can do the following;
    >
    > Sum( findMax50(array), period)
    > If period is 20, then this function will start at the last element of
    > the global array and will return 1 if this element is a maximum among
    > the 50 elements it covers. Then, it moves on to the (last -1)th
    > element all the way to (last-20)th element. The Sum() function will
    > sum up the number of times maximum happened.
    >
    > My greatest difficulty lies in how one can input a function as a
    > parameter into another function.
    >
    > Can some Guru advise?


    I would do it with a template:

    template < typename Fn >
    void Sum( Fn f, int period )
    {

    }

    You could create a function pointer variable, but the above is easier.
     
    Daniel T., Nov 26, 2007
    #4
  5. terminator Guest

    Re: How to find number of times certain conditions happened in anarray?

    On Nov 26, 1:18 pm, wrote:
    > Dear Gurus,
    >
    > I would like to implement a function that computes the number of times
    > a certain condition is met in a global array.
    >
    > For example, I have an global array of size 500.
    > float array[500];
    >
    > I have a function that finds the maximum of an array of size 50.
    > bool findMax50(float input[])
    >
    > Next, I want to implement a function that can do the following;
    >
    > Sum( findMax50(array), period)
    > If period is 20, then this function will start at the last element of
    > the global array and will return 1 if this element is a maximum among
    > the 50 elements it covers. Then, it moves on to the (last -1)th
    > element all the way to (last-20)th element. The Sum() function will
    > sum up the number of times maximum happened.
    >
    > My greatest difficulty lies in how one can input a function as a
    > parameter into another function.
    >

    though as usual I cant make heads or tials of a post the answer to
    your qustion:
    > Can some Guru advise?


    Is:

    You can use a function pointer:

    typedef ret_type (*funcptr)(param_types);
    ret_type foo(param_types);
    funcptr fptr=&foo;

    alternatively you can use functioniods:

    class my_functioniod_base{
    public:
    virtual ~my_functioniod_base()=0;
    ret_type operator()(param_types) const =0;
    };

    struct Tfoo:
    public my_functioniod_base
    {
    ret_type operator()(param_typs) const{/*define it here.*/};
    };

    Tfoo foo;

    my_functioniod_base & fref=foo;
    fref(params);//runs Tfoo::eek:perator().

    you can do a little template work too:

    template<typename pred>
    void run(pred p){
    p();
    };

    void f();

    run(&f);

    have a look at the standard <algorithm> and <functional> headers too;
    interesting tools you can find there.

    yours,
    FM.
     
    terminator, Nov 26, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PJ6
    Replies:
    8
    Views:
    2,076
  2. Simon
    Replies:
    1
    Views:
    288
    Yuriy Solodkyy
    Jun 11, 2007
  3. Simon
    Replies:
    1
    Views:
    119
    Yuriy Solodkyy
    Jun 11, 2007
  4. Robert
    Replies:
    0
    Views:
    114
    Robert
    Sep 11, 2003
  5. blnukem
    Replies:
    9
    Views:
    108
    Robin
    Apr 30, 2004
Loading...

Share This Page