Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

C: Find the longest series of even and positive numbers

  Asked By: Nemanja    Date: Mar 16    Category: C Programming    Views: 1743
  

Hi.

I need help with this program:

Example input: 2 4 6 -1 7 3 -2 1
Output: 3 3 (longest series of even is 3, longest series of positive is 3)

Here is the code:

#include <stdio.h>
int even(int x)
{
return x % 2 == 0;
}

int positive(int x)
{
return x>0;
}
int longest_series(int a[], int n, int (*f) (int)) {
int i;
/*Length of the current series is 0 or 1 depending on whether it is a array
empty or not*/
int cs = 0;
/* Longest series is current series */
int ls = cs;
for (i = 0; i < n; i++) {
if ((*f)(a[i]))
cs++;
else
cs = 0;
if (cs > ls)
ls = cs;
}
return ls;
}
int main() {
int a[] = {2,4,6,-1,7,3,-2,1},
n = sizeof(a)/sizeof(int);
printf("%d %d\n", longest_series(a, n, &even),
longest_series(a, n, &positive));
return 0;
}

My question is how to write this code if the prototype of function is:

void series(int *array, int n, int (*s)(int), int **beginning, int *length);

Thanks for replies.

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on C: Find the longest series of even and positive numbers Or get search suggestion and latest updates.




Tagged: