int sign(int v)
{
return (v == 0) ? 0 : ((v > 0) ? 1 : -1);
}
int check(int v[], int n) // return 1 for true, 0 for false
{
int sign = 1;
int count = 1;
int sc = 0;
for (int i = 0; i < n; i++)
{
if (sign(v[i]) != sign)
return 0;
sc++;
if (sc == count)
{
sc = 0;
if (sign == 1)
sign = -1;
else
{
sign = 1;
count++;
}
}
}
return 1;
}
int main()
{
// ... put here your code for filling v
cout << (check(v, n)) ? "DA!" : "NU!" << endl;
}
Also, you should declare v as int v[n], and fill it from v[0] to v[n-1].
0
solved C++ algorithm exercise [closed]