Click here to Skip to main content
15,891,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<iostream>
using namespace std;
int main()
{

bool ar[4],arr[4] ;
int i=0,h=0,j,k;
int n,m,carry=0,result[5];
cout<<"Enter the first positive number:"<<"\n";
cin>>n;
while(n>0)
{
    ar[i]=n%2;
    i++;
    n=n/2;

}

cout<<"Binary number is:";
for (j=i-1;j>=0;j--)
{
    cout<<ar[j];
    }
cout<<"\n"<<"Enter the 2nd positive number:"<<"\n";
cin>>m;
while(m>0)
{
    arr[h]=m%2;
    h++;
    m=m/2;

}

cout<<"Binary number is:";
for (k=h-1;k>=0;k--)
{
    cout<<arr[k];
    }

    cout<<"\n";
Posted
Comments
CHill60 4-Oct-15 16:35pm    
What is your question?
Mpaphi Map'z Dambe 4-Oct-15 16:51pm    
the question was to design a 4 bit adder .
It should convert two decimal to 4 bit binary then add the two binary numbers.for example 9 and 10.9=1001 and 10=1010 adding the two will result in 10011 carry=1
CHill60 4-Oct-15 17:08pm    
No - that's your homework assignment.
You've posted some code - what's wrong with it.
Mpaphi Map'z Dambe 4-Oct-15 17:49pm    
Im stuck
i was able to make a code that can convert a decimal to binary(storing each bit in an array)but now im failing to produce a code that will add the two array...thats what i need help with

From your description it is not obvious how much you need to implement in terms of hardware-like full adder? If it's just about producing the result, you may very much simplify the code, e.g. (not tried with the compiler - from brain to paper ;-)):
C++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
    unsigned int a;
    unsigned int b;
    cin >> a >> b;
    if (a < 0x10 && b < 0x10)
    {
        unsigned int r = a + b;
        unsigned int c = r & 0x10 ? 1 : 0;
        r &= 0x0F;
        cout << "carry = " << c << ", 4-bit result = " << r << endl;
    }
}

Since it was neither clear if you need to dump the result as binary or as decimal, I decided for the least effort: decimal ;-).
Regards
Andi
 
Share this answer
 
v3
Comments
Patrice T 4-Oct-15 20:05pm    
I think that building a 4 bits adder should exclude using the addition.
Andreas Gieriet 5-Oct-15 0:21am    
Well, that's reading between the lines ;-). My point is, that if the "requirement" is not clear enough, the solution will be just "anything".
Regards
Andi
PS: But you are right, the intent was maybe to mimic hardware implementation.
Patrice T 5-Oct-15 1:49am    
:)
Mpaphi Map'z Dambe 5-Oct-15 7:12am    
Andreas the code has to add 2 4bit numbers.first it has to convert 2 decimal to binary then add them.
Andreas Gieriet 5-Oct-15 15:04pm    
You don't have to repeat your title here again. It is obviously ambiguous, or more precisely: leaves open crucial "details". May I use the plus operator? in which form is the output expected (you mention that you enter decimal numbers to be converted to binary - so, the result is decimal again - or binary?). What do you need in the end? Mimic hardware? E.g. have a 4-bit adder build out of four full adders connected by carries, and each full adder composed by two half adders plus an or gate? We cannot know what the goal of this exercise is, what your background is, etc.
A possible implementation, given your C++ tag is to create components (classes) in C++ which represent the various pieces of hardware and communicate over variables (which may play the role of wires). What is the whole context? What are you expected to learn from this exercise?
If it's about mimicking hardware, I suggest to at least make functions for the individual functional parts.
Regards
Andi
The code you provided is already buggy: ar and arr are not initialised to zero.
One way to correct it is:
C#
for(i=0; i < 4, i++)
{
    ar[i]=n%2;
    n=n/2;
}

This code will fill ar and zero it at the same time.

What have you done about the bits adder ?
By the way we don't do your homework for you, we just help if you have a problem in the work you have done.
 
Share this answer
 
v2
Comments
Mpaphi Map'z Dambe 4-Oct-15 17:56pm    
that part is sorted i used a while function while(n>0)
{
ar[i]=n%2;
i++;
n=n/2;
you can run the code,its working(so far it can convert decimal to binary),im struggling to add the two array ar[] and arr[] .
i need to code such that
ar[3] +arr[3]=result[3]
ar[2]+arr[2]=result[2]
ar[1]+arr[1]=result[1]
ar[0]+arr[0]=result[0]

which will give my final result[5]
Patrice T 4-Oct-15 18:22pm    
I fear you can't use an addition to make an adder.
Matt T Heffron 5-Oct-15 12:04pm    
Hint: XOR is a half-adder and AND is for the carry.
Mpaphi Map'z Dambe 4-Oct-15 18:43pm    
Thats what im struggling with, an operation or function to use to add the 2 arrays
Patrice T 4-Oct-15 18:53pm    
What is your code to add 2 bits ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900