GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2006, 11:35
lululg76 lululg76 is offline
New Member
 
Join Date: Apr 2006
Posts: 11
lululg76 is on a distinguished road

Please help Problem with binary addition.


Hey,

I really need help with binary addition. My program excpets 2 int numbers then it converts them to binary and display them in 8 bit multiples. Now I am trying to add the binary numbers together. I have to do this using binary addtion. I have written a function called binAddCal but it isn't working. When I run my program I don't get any errors but binAddCal doesn't produce any results.

Thanks. binAddCal is the last function in the code.

CPP / C++ / C Code:
#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;
	vector <int> addBinVec;



	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;
	binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.
//	maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
//	if (maxNumber ==userNum1)
//	{
	//	binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.
//	}
//	else
//		binAddCal(userNum2,userNum1,num2BinVec,num1BinVec);//To call add function if num2BinVec is bigger.
 
	
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 ;
}


void binAddCal(vector<int>vec1,vector<int>vec2)

{ 


vector<int>addBinVec;
:


int carry, num = 3;
	
	for(int i = 0; i <vec1.size(); i++)
	{
	
		if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
		  { addBinVec.push_back(1);
		     carry = 0;
		  }
			if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
		  {  addBinVec.push_back(0);
			 carry=1;
		  }
		  if(vec1[i] == 0 && vec2[i]==1&& carry==0)
		  {  addBinVec.push_back(1);
			 carry=0;
		  }
		  if(vec1[i]== 0&& vec2[i]==1&& carry==1)
		  { addBinVec.push_back(0);
			 carry=1;
		  }
		  if(vec1[i]== 1&& vec2[i]==0 && carry==0)
		  {  addBinVec.push_back(1);
	    	carry=0;
		  }
		  if(vec1[i] == 1 && vec2[i]==0 && carry==1)
		  {  addBinVec.push_back(0);
		   carry=1;
		  }
		  if(vec1[i]== 0&& vec2[i] && carry==0)
		  {  addBinVec.push_back(0);
	    	carry=0;
		  }
		  if(vec1[i] == 0 && vec2[i]==0 && carry==1)
		  {  addBinVec.push_back(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.
			{
				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 ;
}
  #2  
Old 18-Apr-2006, 12:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Please help Problem with binary addition.


Quote:
Originally Posted by lululg76
Hey,

I have written a function called binAddCal but it isn't working. When I run my program I don't get any errors but binAddCal doesn't produce any results.

If a function "isn't working", make the program tell you why:

put cout<< statements at strategic places to see what the program is seeing at that time:

Look at the function binAddCal. Why wouldn't it print something?

Well, does it even go through the loop? You could try something like this before the loop:
CPP / C++ / C Code:

  cout << "vec1.size() = " << vec1.size() << endl;
  cout << "vec2.size() = " << vec2.size() << endl;
  for(i = 0; i <vec1.size(); i++)
  {
    cout << "vec1[" << i << "] = " << vec1[i] << endl;
    // etc.
    if(vec1[i] == 1 && vec2[i]==1 && carry ==1)


If it turns out that the sizes are not what you expect, then make the program tell you what is passing to the function. In main(), you could do something like:

CPP / C++ / C Code:
  cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
  cout << " calling binAddCal: num1BinVec.size() = " << num1BinVec.size() << endl;
  // etc.
  binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.


Regards,

Dave
  #3  
Old 18-Apr-2006, 20:55
lululg76 lululg76 is offline
New Member
 
Join Date: Apr 2006
Posts: 11
lululg76 is on a distinguished road

Re: Please help Problem with binary addition.


Thanks Dave,

I tried what you said and it is telling me that the sizes are 0 but I don't understand why the vector array isn't being pass by the function.
  #4  
Old 18-Apr-2006, 21:41
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Please help Problem with binary addition.


Quote:
Originally Posted by lululg76
I tried what you said and it is telling me that the sizes are 0 but I don't understand why the vector array isn't being pass by the function.
You might try adding some more statements. Some to test and display the contents of the vector before it is passed to the function and after.
Maybe something like this.
CPP / C++ / C Code:
	//Test before
	if(!num1Vec.empty())
	{
		for(int i = 0; i < num1Vec.size(); ++i)
		{
			cout << num1Vec[i] << ' ';
		}
	}
	else
		cout << "Vectors is empty!" << endl;

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

	//Test after
	if(!num1Vec.empty())
	{
		for(int i = 0; i < num1Vec.size(); ++i)
		{
			cout << num1Vec[i] << ' ';
		}
	}
	else
		cout << "Vectors is empty!" << endl;
You could also use that in the function to test along the way.
  #5  
Old 19-Apr-2006, 01:00
lululg76 lululg76 is offline
New Member
 
Join Date: Apr 2006
Posts: 11
lululg76 is on a distinguished road

Re: Please help Problem with binary addition.


Thanks everyone for your help. I figured it out. I just had to convert in main instead of trying to use a funtion.

Thanks
  #6  
Old 19-Apr-2006, 09:21
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Please help Problem with binary addition.


Quote:
Originally Posted by lululg76
Thanks everyone for your help. I figured it out. I just had to convert in main instead of trying to use a funtion.

Thanks
You are welcome.

If you are happy with that solution that is fine. There is a solution that will let you use your functions. The issue was that you were passing a copy of the vector not the actual vector. So any changes made to the vector in the functions were to a copy of the vector and the copy was destroyed on function exit. So you need to pass the vector in a way that it is not a copy.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03
String problem vaha C Programming Language 3 24-May-2005 19:21
Please help! Dynamic binary tree problem robsmith C Programming Language 3 15-Mar-2005 22:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.