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;
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 ;
}