simpleAlternatedSum X50867


Statement
 

pdf   zip   main.cc

html

Implement a RECURSIVE function that, given a positive natural n, returns the simple altenated sum 1−2+3−4+…+(−1)n+1· n. This is the header:

// Pre: n >= 1
// Post: Returns 1-2+3-4+...+(-1)^(n+1)*n
int simpleAlternatedSum(int n);

Observation You only need to submit the required procedure; your main program will be ignored.

Observation

Note that we are requesting a RECURSIVE solution.

Sample session
simpleAlternatedSum(1) = 1
simpleAlternatedSum(5) = 3
simpleAlternatedSum(10) = -5
simpleAlternatedSum(533) = 267
Information
Author
PRO1
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
C++
User solutions
C++