[ create a new paste ] login | about

Link: http://codepad.org/DVx0sgg1    [ raw code | output | fork ]

C++, pasted on May 3:
#include <iostream>
using namespace std;

// calc(): returns sum(a[0], a[2], a[4], ...) - sum(a[1], a[3], a[5], ...)
int calc(int *a, int size)
{
    int sum_even_pos = 0;
    int sum_odd_pos = 0;
    for (int i = 0; i < size; i++) 
    {
        sum_even_pos += a[i];
        if (++i < size) sum_odd_pos += a[i];
    }
    return sum_even_pos - sum_odd_pos;
}

int main( )
 {
 int a[6]={1,2,3,5,6,2};
 int size = 6;   
 cout<< calc(a, size)<<endl;
 
 return 0;
}


Output:
1
1


Create a new paste based on this one


Comments: