Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (8 votes)
See more:
write program in c++ to compute and print the sum and carry of two 5 bit binary input from user using full adder ?

This is the code I have sofar:

[Inserted from non-solution on OP's behalf] -MRB
C
#include <iostream.h>
using namespace std;
 
void  main()
{
      int ar[5];
      int ar1[5];
      cout<<"Enter the First Binary number: "<<endl;
      for(int i=0;i<5;i++)
          cin>> ar[5];
      cout<<"Enter the Second Binary number: "<<endl;
      for(int j=0;i<5;j++)
        cin>> ar1[5];
     if(ar[i]+ar1[j]==0)
        cout<<"sum =0 ";
     if(ar[i]+ar1[j]==1)
       cout<< "sum= 1" ;
     if(ar[i]+ar1[j]==2)
       cout<<"sum=0 ";
    if(ar[i]+ar1[j]==3)
    cout<<" sum=1 ";
}


Can anybody tell me what's wrong with this code?

Thanks in advance!
Posted
Updated 23-May-11 2:40am
v3
Comments
Richard MacCutchan 23-May-11 8:53am    
Can anybody tell me what's wrong with this code?

Well it shows that you need to spend a lot more time with your course notes or programming manuals to learn the basics such as array indexing, loops, calculations etc. Also the basics of addition and how carries propagate. Take another look at my answer below and give that idea a go; there is no point in trying to write code when you do not understand how to solve your problem logically.
Sergey Alexandrovich Kryukov 23-May-11 11:43am    
Right. I would even say "can anybody tell..." is rude. What, any expert is considered to be an incapable idiot? Why telling it is another question...
--SA
Sergey Alexandrovich Kryukov 23-May-11 11:46am    
Please never post what you posted below as solution. Use "Add comment", "Improve question" or reply to comment instead.
--SA

Nobody here is going to do your homework for you. It's not because we feel like we're better than you, or are somehow elite coders - it's because you won't learn anything. Your instructor should have given you all of the guidance you need to complete the task.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:44am    
Tell them, John! My 5.
--SA
Try thinking about the problem in non-programming terms and write on paper the steps needed to accomplish this. You can then work through those steps to check that your logic is correct before converting to actual C++ code.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:43am    
Good idea :-) My 5.
--SA
1. The first input loop uses the wrong index
cin>> arr[5]
// here   ^ 


2. Typo in second loop header:
for(int j=0;i<5;j++)
// here     ^


3. One line later, same error as described in 1.

4. The whole code block after the input refers to single array elements using the index values i and j, but these values are never changed - you are missing a loop here.

Think about what a half adder does, and then how you call it. Rather than write the code for the half adder into main(), write a separate function, and call that from main - remember that as you have 5 digits to deal with, you need to call that half adder at least 5 times.

I'd think that most of the above paragraph should be in your instructions already, or in whatever book the instructor is basing the lessons on. Look there for more details.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:44am    
Good catch, a 5.
--SA
Here is what is wrong.Array Index starts from 0 not 1 so if you have something like

C
int ar[5];
int ar1[5];


and if want to access 5th element then you should do like

cin>>ar[4];
cin>>ar[4];


and in this programe i thing you should do something like

cout<<"Enter the First Binary number:<< endl;
for(int i=0;i<5;i++)
cin>>ar[i];


Now the if statment
if(ar[i]+ar1[j]==0)


does not make any sense as value of i and j is 5 and max index of array is 4. So asRichard
Said Spend some time with books.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:44am    
Good catch, a 5.
--SA
CS2011 23-May-11 13:26pm    
Thank you

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