answersLogoWhite

0


Best Answer

An array or vector of int or double or any other signed type can achieve this. If the array must alternate, then simply traverse the array alternating the signs as you go:

std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

bool sign=true; // true is positive.

for (int i=0; i<9; ++i)

{

if (sign) { // value must be positive

if (v[i]<0) v[i] *= -1; // if negative, make positive

} else { // value must be negative

if (0<v[i]) v[i] *= -1; // if positive, make negative

}

sign = !sign; // switch sign for next iteration

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write in c plus plus a series of n integers alternating plus and minus?
Write your answer...
Submit
Still have questions?
magnify glass
imp