0

I am a beginner of C++. I learnt Python before. When using C++ for programming, my mind always stick to the skills of Python. Here is my question.

int main()
{
   int count ;
   int n1, n2, k, z;
   array<int,8> queen = {1, 7, 4, 6, 4, 5, 0, 4};
   auto array_length = end(queen) - begin(queen);
   count = 0;
   n1 = array_length - 1 ;
   n2 = 1 ;
   while (n1 > 0)
   {
       for (k=0; k < (n1+1) ; k=k+1)
       {
           z = abs(queen[k+n2] - queen[k]);
           if ( z == n2 )
           {
               count += 1 ;
           }
           if ( z == 0 )
               count += 1 ;
           if (( n1 - 1 ) == k)
           {
               n2 += 1 ;
               n1 -= 1 ;

           }
       }
   }
   cout << count << endl;
}

As you see, I have some trouble in line 8 (n1 = array_length - 1 ;). There is a warning telling me that

Implicit conversion loses integer precision: 'long' to 'int'

What's wrong with this? I do appreciate that if you correct my c++ code.

1
  • Suppose you have a container that can hold 1 quart of liquid, and there's some water in it. You pour the contents into a container that can hold 1 cup. What happens? Commented Jan 20, 2018 at 13:25

1 Answer 1

2
auto array_length = end(queen) - begin(queen);

change it to

int array_length = end(queen) - begin(queen);

auto is picking 'long' automatically. int and long has different sizes. int is 32 bits long is 64 bits

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.