Hi,

I am working on a project that I had to get 2 number from a user display them in 10base. Then convert them into 2 base and display them in multiple of 8. I did this part. The next part I am suppose to do is add and subtract them using binary addition and subtraction(2 compliment for subtraction. I have written some code but I don't know why my addition function isn't doing anything. I am not getting any errors but I can't seem to pass the vectors to the function. Well thanks here's the code I have.

``````#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using namespace std;

void binConvert(int,  vector<int>);//function prototype
void binAddCal ( vector<int>,vector<int> );//function prototype
int  findMax(int, int);//function prototype

int main()
{
//	int maxNumber, num=3;
int userNum1, userNum2;
vector<int> num1Vec;
vector<int> num2Vec;
vector<int> num1BinVec;
vector<int> num2BinVec;

cout<<"Please enter a base 10 number"<<endl;
cin>> userNum1;
cout<<"Please enter a base 10 number"<<endl;
cin>>  userNum2;

cout<<"The binary number for "<< userNum1 <<" is:  "<<endl;
binConvert(userNum1, num1Vec);
cout<<endl;
cout<<"The binary number for "<< userNum2 <<" is:  "<<endl;
binConvert(userNum2, num2Vec);
cout<<endl;

cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl;

cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
//	maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
//	if (maxNumber ==userNum1)
//	{
//	}
//	else

return 0;
}

void binConvert( int num1 , vector<int>vecNum)
{
vector<int> vecBinary;
int num = 3; //intialize the num to set the - in the binary display.
do
{
vecNum.push_back(num1);  //To fill a  vector to hold the results from the divsion of the base 10 numbers.
num1 = num1 / 2;
}while(num1 != 0);

for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder
{
vecBinary.push_back(vecNum[i] % 2);  //To fill a vector with the binary number of the base number entered.
}

if (vecBinary.size() < 8)   //To check to see if the vector is small than 8 bits.
{
while(vecBinary.size() < 8)  //To loop until vector becomes 8 bits.
{
vecBinary.push_back(0);	//To add 0 to make it an 8 bit display.
}
}
else
{
while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
{
vecBinary.push_back(0);	//To add 0 to make a multiple of 8 bit display.
}
}

//To reverse the vector so the binary number with display correctly.
reverse(vecBinary.begin(), vecBinary.end());
for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector.
{
cout<< vecBinary[i]<< " ";	 //To display each element in the num1 binary vector.

if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed.
{
cout<<"- ";  //To place a - in the binary display for readablity.
num+=4;  //To increment by 4 so it shows up every 4 bits.
}

}

return ;

}
int findMax(int num1,int num2)
{

if (num1>num2)
return num1;
else
return num2;

return 0 ;
}

{

:

int carry, num = 3;

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

if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
carry = 0;
}
if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
carry=1;
}
if(vec1[i] == 0 && vec2[i]==1&& carry==0)
carry=0;
}
if(vec1[i]== 0&& vec2[i]==1&& carry==1)
carry=1;
}
if(vec1[i]== 1&& vec2[i]==0 && carry==0)
carry=0;
}
if(vec1[i] == 1 && vec2[i]==0 && carry==1)
carry=1;
}
if(vec1[i]== 0&& vec2[i] && carry==0)
carry=0;
}
if(vec1[i] == 0 && vec2[i]==0 && carry==1)
carry=0;
}
}

for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector.
{
cout<< addBinVec[i]<< " ";	 //To display each element in the num1 binary vector.

if (addBinVec.size() < 8)   //To check to see if the vector is small than 8 bits.
{
while(addBinVec.size() < 8)  //To loop until vector becomes 8 bits.
{
}
}
else
{
while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
{
addBinVec.push_back(0);	//To add 0 to make a multiple of 8 bit display.
}
}
}

return ;
}``````

## All 15 Replies

your code is too long, better if u mark in ur code the problem area.

Ok sorry didn't realize how long my code was. I can fiqure out why my binAddCal function is not working it is the last funtion in the code. Thanks

I asssume you meant you can't fiqure out why my binAddCal function is not working. Assuming that's the case, here's how I'd

Given:
Usually we write numerical values with largest placeholder to the left and lowest to the right. However, that is convention only. It could be the other way around just as easily.

And:
One of the more common ways to represent binary numbers is using an array of digits restricted in value to 0 or 1.

Then:
To add binary numbers you could first reverse the usual order so the lowest placeholder has index 0, the second has index 1, etc. Then to add two binary numbers begin with a carry value of zero and add the two elements with index of zero and the carry value together. If the modulus of the sum is zero then the value of the element at index zero of the binary sum is zero, too, else it is one. If the sum is greater than one then the carry value to 1 else it is zero and go to the next set if indexes. Repeat the proces by looping through all elements of the shortest array. Then add the last carry value to the next element of the longer array. Treat the sum as you did before and repeat until this process until the sum is less than 2 or the last element of the longer array had been used. If the last element of longer array has been found and the carry is 1 then put one in the next element of the binary sum array. Now if you reverse the binary sum array you have the binary number is the usual left largest placeholder value form again.

There are probably other ways to do it too, but this seems most logical to me at this point.

This sort of boolean arithmetic is probably simplest with a recursive algorithm.

for the integers, a and b..

Take a temporary 'carrier' number which records the result of `a AND b` bit-shifted one place left.

if the carrier evaluates to zero, the result of the calculation becomes `a XOR b` if the carrier is nonzero (meaning, some bits needed to be carried), call the recursive function using the result, and the carrier.

Incidentally, C++ has an STL container called a bitset - it might be better to use that than a vector of int's or bool's or whatever. Bitsets are designed with binary operations in mind :)

Thanks for all of your input, I have to use vectors as part of the problem. My problem is I can't figure out why the vector isn't being passed to the function. I the vectors as agruments of the function but it keeps saying the size is zero and I don't know why.

hi
try wid this code, i was nt heaving much time to correct it for addition of more thn 8 bit numbers. it should work fine for less thn 8 bit addition.

``````#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using namespace std;

void binConvert(int,  vector<int> &);//function prototype
void binAddCal ( vector<int>,vector<int> );//function prototype
int  findMax(int, int);//function prototype

int main()
{
//	int maxNumber, num=3;
int userNum1, userNum2;
vector<int> num1Vec;
vector<int> num2Vec;
vector<int> num1BinVec;
vector<int> num2BinVec;

cout<<"Please enter a base 10 number"<<endl;
cin>> userNum1;
cout<<"Please enter a base 10 number"<<endl;
cin>>  userNum2;

cout<<"The binary number for "<< userNum1 <<" is:  "<<endl;
binConvert(userNum1, num1Vec);
cout<<endl;

cout<<"\n****num1Vec****"<<endl;
for(int i=0; i< (int)num1Vec.size();++i) {
cout<<num1Vec[i] ;
}
cout<<"\n********"<<endl;

cout<<"The binary number for "<< userNum2 <<" is:  "<<endl;
binConvert(userNum2, num2Vec);

cout<<"\n*****num2Vec***"<<endl;
for(i =0; i<(int)num2Vec.size();++i) {
cout<<num2Vec[i] ;
}
cout<<"\n********"<<endl;

cout<<endl;

cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl;

cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
//	maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
//	if (maxNumber ==userNum1)
//	{
//	}
//	else

return 0;
}

void binConvert( int num1 , vector<int>& vecNum)
{
vector<int> vecBinary;
int num = 3; //intialize the num to set the - in the binary display.
do
{
vecNum.push_back(num1);  //To fill a  vector to hold the results from the divsion of the base 10 numbers.
num1 = num1 / 2;
}while(num1 != 0);

/*cout<<"\n****VVV****"<<endl;
for(int kki =0; kki<(int)vecNum.size();++kki) {
cout<<vecNum[kki] ;
}
cout<<"\n****VVV****"<<endl;*/
for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder
{
vecBinary.push_back(vecNum[i] % 2);  //To fill a vector with the binary number of the base number entered.
}

if (vecBinary.size() < 8)   //To check to see if the vector is small than 8 bits.
{
while(vecBinary.size() < 8)  //To loop until vector becomes 8 bits.
{
vecBinary.push_back(0);	//To add 0 to make it an 8 bit display.
}
}
else
{
while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
{
vecBinary.push_back(0);	//To add 0 to make a multiple of 8 bit display.
}
}

//To reverse the vector so the binary number with display correctly.
reverse(vecBinary.begin(), vecBinary.end());
for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector.
{
cout<< vecBinary[i]<< " ";	 //To display each element in the num1 binary vector.

if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed.
{
cout<<"- ";  //To place a - in the binary display for readablity.
num+=4;  //To increment by 4 so it shows up every 4 bits.
}

}
vecNum.clear() ;
for( int kki =0; kki<(int)vecBinary.size();++kki) {
vecNum.push_back(vecBinary[kki]) ;
}
//reverse(vecNum.begin(), vecNum.end());
/*cout<<"\n****If Right****"<<endl;
for( kki =0; kki<(int)vecBinary.size();++kki) {
cout<<vecNum[kki] ;
}
cout<<"\n****VVVVVVV****"<<endl;*/
return ;

}
int findMax(int num1,int num2)
{

if (num1>num2)
return num1;
else
return num2;

return 0 ;
}

{

int carry = 0, num = 3;
int i=0;

if(vec1 == 1 && vec2==1) {
carry = 1;
}
else if(vec1 == 0 && vec2==0) {
carry = 0;
}
else {
carry = 0;
}
for( i = 1; i <vec1.size(); i++)
{

if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
carry = 0;
}
if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
carry=1;
}
if(vec1[i] == 0 && vec2[i]==1&& carry==0)
carry=0;
}
if(vec1[i]== 0&& vec2[i]==1&& carry==1)
carry=1;
}
if(vec1[i]== 1&& vec2[i]==0 && carry==0)
carry=0;
}
if(vec1[i] == 1 && vec2[i]==0 && carry==1)
carry=1;
}
if(vec1[i]== 0&& vec2[i]==1&& carry==0)
carry=0;
}
if(vec1[i] == 0 && vec2[i]==0 && carry==1)
carry=0;
}
}

//for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector.
for( i = 0; i < addBinVec.size(); i++) //To loop through the num1 binary vector.
{
cout<< addBinVec[i]<< " ";	 //To display each element in the num1 binary vector.

//if (addBinVec.size() < 8)   //To check to see if the vector is small than 8 bits.
//	{
//		while(addBinVec.size() < 8)  //To loop until vector becomes 8 bits.
//			{
//				addBinVec.push_back(0);	//To add 0 to make it an 8 bit display.
//			}
//	}
//else
//	{
//		while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
//			{
//				addBinVec.push_back(0);	//To add 0 to make a multiple of 8 bit display.
//			}
//	}
}
return ;
}`````` er that's wrong.

I tried it with 23 and 34...didn't work.

Tee he he. Try again

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

If you have to use vectors for this (Wouldn't be my first choice, a deque would to allow you to use push_front - eliminating the annoying step of reversing the bit order), rather than vector<int> - you are only holding 1s and 0s, so use vector<bool> - this could simplify your code a fair bit.

You could also reduce alot of repeated code if you used a seperate container for your carrier, rather than a single variable.

this may solve one of the problems with your binAddCal() function, that you are using the carry variable without initialising it. (result - undefined behaviour)

As I mentioned in the other post, there is a much slimmer way to do this using bitwise operators in a recursive function

``````int binaryAdd(int a, int b)
{
return (a&b)? binaryAdd( ( (a&b) << 1 ) , a^b ) : a^b;
}``````

You could emulate this function using STL containers - you'd need to emulate the bitwise operators used: left-shift, XOR and AND. It's not his homework - and it's wrong anyway. :rolleyes:

Nice one dubeyprateek. Please tell me you don't really work for a software development company.

Yikes. Tee he he.

Have a look here. It might even give you ideas about implementing two's complement.
http://en.wikipedia.org/wiki/Two's_complement

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

Hi, iamthwee,
for ur kind notice, i do work as software developer n i m doing fine, next i tried to help him bounding myself in all the restrictions imposed by the code, i try my best to just add some more lines rather thn removing anything, whole code was outcome of 5 mins review, i guess i hav helped him more thn wat u are doing by giving some links.
and u tell me one thing do u work as a tester who is frustrated enough. better try correcting the code.

please check it once again, i hope i have solved the problem this time :mrgreen:

``````#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using namespace std;

void binConvert(int,  vector<int> &);//function prototype
void binAddCal ( vector<int>,vector<int> );//function prototype
int  findMax(int, int);//function prototype

int main()
{
//	int maxNumber, num=3;
int userNum1, userNum2;
vector<int> num1Vec;
vector<int> num2Vec;
vector<int> num1BinVec;
vector<int> num2BinVec;

cout<<"Please enter a base 10 number"<<endl;
cin>> userNum1;
cout<<"Please enter a base 10 number"<<endl;
cin>>  userNum2;

cout<<"The binary number for "<< userNum1 <<" is:  "<<endl;
binConvert(userNum1, num1Vec);
cout<<endl;

cout<<"\n****num1Vec****"<<endl;
for(int i=0; i< (int)num1Vec.size();++i) {
cout<<num1Vec[i] ;
}
cout<<"\n********"<<endl;

cout<<"The binary number for "<< userNum2 <<" is:  "<<endl;
binConvert(userNum2, num2Vec);

cout<<"\n*****num2Vec***"<<endl;
for(i =0; i<(int)num2Vec.size();++i) {
cout<<num2Vec[i] ;
}
cout<<"\n********"<<endl;

cout<<endl;

cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl;

cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
//	maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
//	if (maxNumber ==userNum1)
//	{
//	}
//	else

return 0;
}

void binConvert( int num1 , vector<int>& vecNum)
{
vector<int> vecBinary;
int num = 3; //intialize the num to set the - in the binary display.
do
{
vecNum.push_back(num1);  //To fill a  vector to hold the results from the divsion of the base 10 numbers.
num1 = num1 / 2;
}while(num1 != 0);
for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder
{
vecBinary.push_back(vecNum[i] % 2);  //To fill a vector with the binary number of the base number entered.
}

if (vecBinary.size() < 8)   //To check to see if the vector is small than 8 bits.
{
while(vecBinary.size() < 8)  //To loop until vector becomes 8 bits.
{
vecBinary.push_back(0);	//To add 0 to make it an 8 bit display.
}
}
else
{
while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
{
vecBinary.push_back(0);	//To add 0 to make a multiple of 8 bit display.
}
}

//To reverse the vector so the binary number with display correctly.
reverse(vecBinary.begin(), vecBinary.end());
for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector.
{
cout<< vecBinary[i]<< " ";	 //To display each element in the num1 binary vector.

if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed.
{
cout<<"- ";  //To place a - in the binary display for readablity.
num+=4;  //To increment by 4 so it shows up every 4 bits.
}

}
vecNum.clear() ;
for( int kki =0; kki<(int)vecBinary.size();++kki) {
vecNum.push_back(vecBinary[kki]) ;
}
return ;

}
int findMax(int num1,int num2)
{

if (num1>num2)
return num1;
else
return num2;

return 0 ;
}

{

int i =0 ;
int term ;
bool bvec = false ;
if(vec1.size() >vec2.size()) {
i = vec1.size() ;
bvec = true; ;
}
else {
i = vec2.size() ;
bvec = false ;
}
if(vec1.size() != vec2.size()) {
if(bvec) {
reverse(vec2.begin(), vec2.end());
while(vec1.size() != vec2.size()) {
vec2.push_back(0);
}
reverse(vec2.begin(), vec2.end());
}
else {
reverse(vec1.begin(), vec1.end());
while(vec1.size() != vec2.size()) {
vec1.push_back(0);
}
reverse(vec1.begin(), vec1.end());
}
}
int carry = 0, num = 3;
if(vec1[vec1.size()-1] == 1 && vec2[vec2.size()-1]==1) {
carry = 1;
}
else if(vec1[vec1.size()-1] == 0 && vec2[vec2.size()-1]==0) {
carry = 0;
}
else {
carry = 0;
}
for( i =(addBinVec.size()-2) ; i >=0 ; --i)
{
if(i==-1) {
break;
}
if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
carry = 1;
continue ;
}
if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
carry=1;
continue ;
}
if(vec1[i] == 0 && vec2[i]==1&& carry==0)
carry=0;
continue ;
}
if(vec1[i]== 0&& vec2[i]==1&& carry==1)
carry = 0;
continue ;
}
if(vec1[i]== 1 && vec2[i]==0 && carry==0)
carry=0;
continue ;
}
if(vec1[i] == 1 && vec2[i]==0 && carry==1)
carry=0;
continue ;
}
if(vec1[i]== 0&& vec2[i]==0&& carry==0)
carry=0;
continue ;
}
if(vec1[i] == 0 && vec2[i]==0 && carry==1)
carry=0;
continue ;
}
}
for( i = 0; i < addBinVec.size(); i++) //To loop through the num1 binary vector.
{
cout<< addBinVec[i]<< " ";	 //To display each element in the num1 binary vector.

}
cout<<endl;
return ;
}`````` >i guess i hav helped him more thn wat u are doing by giving some links.

Wrong! Doing his homework is far from helping. I pitty whoever you work for. :)

Seriously. Tee he he.

ThanQ

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

>>>Wrong! Doing his homework is far from helping.
help shoul worth something dear, just saying tat do this or do tha does not make any sence.

>>>I pitty whoever you work for.
Yeah u can, but interesting is they feel proud on me..
he he he..! >just saying tat do this or do tha does not make any sence

So what you are effectively saying is that the only way to explain something is to provide the entire code?

How then does that help the person coding? The answer is of course it doesn't. Offering advice, a link or even a small sample snippet will be a thousand times more productive than what you are doing right now.

FACT

>Yeah u can, but interesting is they feel proud on me..

Good for you. But I doubt anyone else with a brain would feel the same dear :)
Tee he he

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

>> How then does that help the person coding? The answer is of course it doesn't. Offering advice, a link or even a small sample snippet will be a thousand times more productive than what you are doing right now.

what should be offered can not be determined by a rule or FACTs, it entirely depends on the requirement. i felt my code will help him more thn ur link, so i said.

>>Good for you. But I doubt anyone else with a brain would feel the same dear
you talk about urself not about "anyone's" brain, this will make u smarter...!!
ho ho ho

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.