Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)

Even/Odd sum with forward recursion

By: beco on Mar 24th, 2011  |  syntax: C  |  size: 0.61 KB  |  hits: 159  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
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. }