Search: | |||||

| ||||

Home » Forum » C Programming | RSS Feeds |

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.

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.

Related Topics:

- programming to display series of 24,99,224,399....upto 10 numbers
- sum of series of natural numbers
- sort numbers even and odd
- how to sum the even numbers only
- find the number of working days (monday to firday) between two give
- Converting a "Stringed" Number to a Number
- Converting a "Stringed" Number to a Number
- Ratio of positive and negative text in learning dataset for text classification?
- Compute the sum of both positive and negative elements of an array
- Alphanumeric Series auto filling
- How is inserting fibonacci series in singly linked list done?
- display series
- Chart Series
- fill series and select a range
- Naming series in multiple Graphs
- Display multiple series of data
- Check odd or even
- how to check for odd or even?
- using find find all
- Write a shell program to find the sum of the series sum=1 + 3 + 5 + 7 + …+ n
- Code for Write a shell program to find the sum of the series sum=1 + 5 + 9 + ...+ n in Unix
- program to display on screen two numbers from keyboard
- Whether the number is the power of two or not.
- prime number
- counting the frequency of randomly generated real number