-3

Basically, I have an array given of "x" numbers and I have to output the amount of how many times the sign changed in the numbers of the array.

For example array is: 2 -4 5 6 7 -2 5 -7

The output should be 5. Why? Because the sign changes first time at -4, second time at 5, third time at -2, fourth time at 5 and last time at -7. Total 5 times.

So, I have this so far but that doesn't work perfectly:

#include <iostream>
using namespace std;

int main()
{
int a[50],n,cs=0,ha=0;



cin >> n;
for (int i=0;i<n;i++)
{
    cin >> a[i];
}
for (int j=1;j<=n;j++)
{
    if(a[j]<0 && a[j-1]>0)
    cs++;
    else if(a[j]>0 && a[j-1]<0)
    cs++;
}
cout << cs << endl;
return 0;
}

Please help!

user2041143
  • 151
  • 1
  • 1
  • 5
  • "Please help"? What's not working? What line is giving a problem? what's the problem with your expected output? whathaveyoutried.com – djechlin Feb 04 '13 at 22:02
  • Accessing `a[n]` is undefined behaviour. – chris Feb 04 '13 at 22:03
  • My problem is that my code is not working correctly, it's displaying an output incorrect to the one expected. – user2041143 Feb 04 '13 at 22:04
  • You have to take a value of `0` into account. You should, for example, consider it to be positive. Then, change `>0` to `>=0` to handle this. – leemes Feb 04 '13 at 22:07

4 Answers4

3

Your problem is that you're running into uninitialized memory, which is causing undefined behaviour. You initialize a[0] through a[n-1] in your input loop and then read from a[0] (with j=1 and a[j-1]) to a[n] (j=n and a[j]) in your calculation loop.

Simply change it to j < n.

chris
  • 60,560
  • 13
  • 143
  • 205
2

If STL is an option for you, you can use std::adjacent_find. This is how you would use it in a complete program:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> v { 1 , 3, -5, 8, -9, -10, 4 };

    auto signSwitch = [] (int x, int y) { return (abs(x) == x) ^ (abs(y) == y); };

    int cnt = 0;
    auto i = adjacent_find(begin(v), end(v), signSwitch);
    while (i != end(v))
    {
        cnt++;
        i = adjacent_find(i + 1, end(v), signSwitch);
    }

    cout << cnt;
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • I wouldn't recommend taking the product. Consider two large integers; they will overflow. I'd use something like `(x >= 0) != (y >= 0)`. – leemes Feb 04 '13 at 22:09
  • Interesting application of `adjacent_find`. – chris Feb 04 '13 at 22:10
  • @leemes: true. That's just the quickest thing that came up to my mind. I never remember if `sgn()` is standard. – Andy Prowl Feb 04 '13 at 22:10
  • @AndyProwl [It doesn't seem like there is one.](http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c) – leemes Feb 04 '13 at 22:11
1

Your second loop should terminate at j < n.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1

On your second for loop you should not have j go to <=. it should just be

 for (int j=1;j<n;j++)
aprohl5
  • 101
  • 1
  • 1
  • 8