Jeremy Beare
Jeremy Beare

Reputation: 487

C++ : For Loop and Vectors

I have been working on Codility on the task MaxCounters. And I have got stuck. No matter what I try, the following code won't loop

// you can also use includes, for example:
// #include <algorithm>

vector<int> solution(int N, vector<int> &A) 
{
    vector<int> counter (N,0);

    for (int i = 0;i < A.size();i++)
    {
        int value = A.at(i);

        if (value = A.size() + 1)
        {

        }
        else
            counter[value - 1] += 1;
    }

    return (counter);
}

I have used this website to find a solution to my problem but to no avail. I keep getting this error message.

In file included from user.cpp:20: func.cpp: In function 'std::vector > solution(int, std::vector >&)': func.cpp:7: warning: comparison between signed and unsigned integer expressions func.cpp:11: warning: suggest parentheses around assignment used as truth value

Any ideas why it just keeps throwing this error?

EDIT: After looking from the other tests, it is clear that I would get this error no matter what exercise I did. So as a result, I conclude I haven't understood looping vectors. What is the correct way to loop through a vector?

Upvotes: 0

Views: 571

Answers (3)

DavidO
DavidO

Reputation: 13942

You are getting two different warnings. The first one relates to this problem:

if( value = A.size() + 1)

...which is just a typo, and should be written as:

if( value == A.size() + 1 )

Once you've resolved that you can move on to the next warning:

An int is signed. A.size() returns an unsigned value. You could change for( int i = 0; i < A.size(); ++i) to something like for( size_t i = 0.....

Since all of the ints you are storing are probably positive, use an appropriate container type (unsigned, for example). Here's how it would look:

vector<unsigned> solution(int N, vector<unsigned> &A) 
{
    vector<unsigned> counter (N,0);

    for (size_t i = 0;i < A.size();i++)
    {
        unsigned value = A.at(i);

        if ( value == A.size() + 1 )
        {

        }
        else
            counter[value - 1] += 1;
    }

    return (counter);
}

The reason that comparing or assigning an unsigned to a signed is worthy of generating a warning is because the ranges of signed versus unsigned containers are different.

Upvotes: 0

Mantosh Kumar
Mantosh Kumar

Reputation: 5731

You program should be updated in the following way

  1. This should avoid the comparing the comparison between signed and unsigned integer.

    for (int i = 0;i < A.size();i++)

TO

   for (vector<int>::size_type i = 0;i < A.size();i++)
  1. As your loop is taking care of size(), you should use [] operator instead of at as internally it do have check for out-of-bound access. This can be avoided.

    int value = A.at(i);

TO

 int value = A[i];
  1. This is actually a main problem and in your code as you have used = instead of ==.

    if (value = A.size() + 1)

TO

   if (value == A.size() + 1)

Upvotes: 0

bcr
bcr

Reputation: 1328

As the compiler is suggesting, this:

if (value = A.size() + 1)

is assigning value the value of A.size() + 1, not evaluating their equality

if (value == A.size() + 1)

Might be what you want instead.

Additionally, size() returns an unsigned int, not an int, thus the compiler complaints with regards to that.

Upvotes: 3

Related Questions