Thread: C: Find longest series of even and positive numbers

  1. #1
    Registered User
    Join Date
    Feb 2015

    C: Find longest series of even and positive numbers


    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 = 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 **begining, int *lenght);
    Thanks for replies.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  2. how to find the longest meaning substring?
    By shaoshao in forum C Programming
    Replies: 7
    Last Post: 06-03-2011, 12:15 PM
  3. How to find the longest word in text file?
    By alionas in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 01:05 PM
  4. Changing from positive to negative numbers
    By vopo in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 02:21 PM
  5. find the totals of positive and negative integers
    By noaksey in forum C Programming
    Replies: 5
    Last Post: 05-11-2004, 01:59 PM