Method 2:
Just change the position of: int num[n];
#include
using namespace std;
int main(int argc, const char * argv[])
{
// method 1:
int n;
cout << "Enter a set of integers: "<< endl;
cin >> n;
int num[n];
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
cout << "num of arrays: "<< endl;
for (int i = 0; i < n; i++)
{
cout << num[i] << " ";
}
cout << endl;
for(int i = 0; i < n; i++)
{
int reminder = 0;
int numOfOnes = 0;
if(num[i] <= 0)
{
numOfOnes = 0;
}
else
{
while (num[i] > 0)
{
reminder = num[i] % 2;
num[i] = num[i] / 2;
//cout <<" Reminder = "<< reminder<<endl;
if( reminder == 1)
{
numOfOnes++;
}
}
}
cout <<"Number of ones= "<< numOfOnes << endl;
}
}
4
solved In c++, why is cout is so important?