answersLogoWhite

0

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

10y ago

What else can I help you with?