Need Help in c++ program

I made a program in which decimal is converted into binary . Now I want to store each bit in an array and add both binary numbers and store in separate memory


using namespace std;
int main ()
{
int arr[15],a,b,i;
cout<<"Enter First Integer(must be last 2 digit of arid number) : ";
cin>>a;
for(i=0;a>0;i++)
{
arr[i]=a % 2;
a /= 2;
}
cout<<"First Binary Conversion : ";
for (int j = i - 1; j >= 0; j--) // prints form the last digit
{
cout << arr[j];
}
cout<<endl;
cout<<"Enter Second Integer(any number between 1-100) : ";
cin>>b;
for(i=0;b>0;i++)
{
arr[i]=b % 2;
b /= 2;
}
cout<<"Second Binary Conversion : ";
for (int k = i - 1; k >= 0; k--)
{
cout << arr[k];
}



return 0;
}
Your third for loop seems to overwrite the arr array, removing the bits from the first input. Try declaring a different array for the second input.

Now, for adding the binary values, you can save the first two user inputs as integers, add them, and convert the sum to binary.

If you want to add two of the binary numbers without using integers, try to create a sum array, and for each element assign if the elements of both addend arrays aren't rounded up or aren't both zeroes. Assign true to the next element of the sum array if they are rounded up. The next steps, if the elements are rounded, are pretty complicated. If they and the next pair of elements are rounded, just assign true to the next element of the sum array. If only one element is true, round up.

That is one approach to interpret. If you want, I can write the code for you if you don't understand.

Last edited on
Topic archived. No new replies allowed.