-5
#include <bits/stdc++.h>
using namespace std;

string bin(int n){
    string x=""; 
    while(n!=0)
    {
        int z=n%2; 
        x+=to_string(z); 
        n%=2;
    }  
    return x;
}

int main(){
    int t; 
    cin>>t; 
    while(t--)
    {
        int n; 
        cin>>n; 
        int a[n]; 
        for(int i=0;i<n;i++)
        {    
            cin>>a[i]; 
            string x=bin(a[i]); 
            int u=x.size();
            int cnt=0;        
            for(int g=0;g<u;g++)
            {
                if(x[g]=='1') 
                    ++cnt; 
            }
            cout<<cnt<<' ';
       }
        cout<<'\n';
   }
}

This code is given several test cases and each test case will have an array of n integers, for each element in the array I should count the number of ones in the binary representation of it. I wrote a function that expects an integer and returns a string containing the binary representation of it. But I wonder why my code does not end, and not allowing me to receive other numbers in array.

For instance, there's one test case and and only array of 2 integers if I inputted 1 and wait for ever to enter the second number, what's happening?

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Intelli
  • 3
  • 1
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h –  Sep 09 '19 at 20:10
  • Does anybody know why there's such a huge influx of questions on this site that use #include ? Do I have confirmation bias, or is it really a recent thing? – JohnFilleau Sep 09 '19 at 20:10
  • I hate to say it but there are a lot of bad programming techniques in your code. Some of them are even illegal in standard C++. I suggest getting a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and see how they write code in there. – NathanOliver Sep 09 '19 at 20:10
  • @John almost any **tutorial** online will have it, as well as any competitive programming challenges. – SergeyA Sep 09 '19 at 20:11
  • 2
    @John `` is a widely used shortcut popular in the programming challenge world as it saves typing. It's also the start of the school year and some bad books use it so you see an uptick in it's use in questions. – NathanOliver Sep 09 '19 at 20:12
  • what is the input? (if you dont know because the tests are confidential, then write your own test) – 463035818_is_not_an_ai Sep 09 '19 at 20:14
  • 2
    I believe you meant `n/=2;` instead of `n %= 2;`, if `n` at some point is odd, the while loop inside bin function will run forever. – Mohamed Magdy Sep 09 '19 at 20:17
  • `` saves time typing, but it makes build times close to an order of magnitude slower. Build the code two-three times and you've eaten up the time savings (or you type too slowly to stand a chance). – user4581301 Sep 09 '19 at 20:29
  • That header also makes it a pain-in-the-neck to have to remove and replace with the proper headers if the compiler is Visual Studio. Can you imagine a programmer applying for a job, only learned from those "competitive websites", and not know the first line of code if they were given Visual Studio as the compiler? – PaulMcKenzie Sep 09 '19 at 20:30
  • You can use `x += ('0' + (n & 1));` instead of calling `tostring`. Your binary digit characters will be `'0'` or `'1'`. – Thomas Matthews Sep 09 '19 at 20:55

1 Answers1

1

This is your bin function reduced to the bare minimum:

string bin(int n){
    while(n!=0)
    {
        n%=2;
    }  
    return {};
}

If n is even you will set it to 0 on the first iteration, otherwise you set it to 1 and never change it afterwards (1%2==1). Hence you have a endless loop. I won't spoil you the "fun" of completing the exercise, so I will just point you to using a debugger. If you step trough your code line by line you could have observed how n never changes and why the loop wont stop.

PS: (spoiler-alert) you might want to take a look at std::bitset (end of spoiler)

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185