Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Even/Odd sum with forward recursion

By: beco on Mar 24th, 2011  |  syntax: C  |  size: 0.61 KB  |  views: 166  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. void checksum(int m[], int pos, int *seven, int *sodd)
2. {
3.         if(pos>=MAX)
4.         {
5.                 printf("sodd: %d, seven: %d.\n", *sodd, *seven);
6.                 return;
7.         }
8.         if (pos==0)
9.         {
10.                 *seven = m[pos];
11.                 checksum(m, pos+1, seven, sodd);
12.                 return;
13.         }
14.         if (pos==1)
15.         {
16.                 *sodd = m[pos];
17.                 checksum(m, pos+1, seven, sodd);
18.                 return;
19.         }
20.         if (!(pos%2))
21.         {
22.                 *seven = *seven + m[pos];
23.                 checksum(m, pos+1, seven, sodd);
24.
25.         }
26.         else
27.         {
28.                 *sodd= *sodd + m[pos];
29.                 checksum(m, pos+1, seven, sodd);
30.         }
31.         return;
32. }
33.
34. int main(void)
35. {
36.     int mat[MAX]={0,1,2,3,4,5,6,7,8,9};
37.         int seven, sodd;
38.
39.     checksum(mat, 0, &seven, &sodd);
40. }
clone this paste RAW Paste Data
Top