a function that computes the alternating sum of all elements in an array

Pages: 12
umm, i'm pretty sure this works. I did it for 10 random numbers between 0 and 19. Modify it if you want to adjust these parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*

Calculates an alternating array.

*/

#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

int main(){

	int atr[10], i, s=0;

	for(i=0;i<10;i++){ 	atr[i]=rand() % 20;	
				cout << atr[i] << ' ';}

	cout << '\n';

	for(i=0;i<10;i++){ s += pow(-1,i)*atr[i];	}

	cout << s << '\n';

return 0;

}


As an output:

3 6 17 15 13 15 6 12 9 1 
-1
Topic archived. No new replies allowed.
Pages: 12