473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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 '07 #1
4 1647
On Nov 26, 6:18 am, lightai...@gmail.com 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 '07 #2
<li********@gmail.comwrote in message
news:cb**********************************@s8g2000p rg.googlegroups.com...
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.
Nov 26 '07 #3
li********@gmail.com 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.
Nov 26 '07 #4
On Nov 26, 1:18 pm, lightai...@gmail.com 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::operator().

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 <algorithmand <functionalheaders too;
interesting tools you can find there.

yours,
FM.
Nov 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bite My Bubbles | last post by:
How can i count the number of times a string appears within another string. Thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!
3
by: Lizard | last post by:
OK, total newbie here, so this may be a mind-numbingly dumb question AND I may be phrasing it badly. I have an xsl:template which looks like this: <xsl:template match="LoanRecord"> <hr>...
50
by: sabarish | last post by:
Hi to all. find out the biggest among two numbers without using any conditional statements and any relational operators.
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
5
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
6
by: rcamarda | last post by:
Hello, I need to find students that have 4 consecutive absences. When a student is absent 4 times in a row, they can be dropped from the class. My class attendance file contains each attendance...
22
by: MLH | last post by:
If 3 things can be in one of 2 states, the number of possible combinations is equal to 2^3. But if I have 3 things, 2 of which can be in 2 states and the other in 3 states, what's the simplest...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.