Public Group

This topic is 2159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I will start off saying that this is a school assignment. I will not be asking for the solution to be coded for me or really anything of the sort. I am just looking for some basic ideas for some implementing the algorithm.

The program is asking for me to create a Ripple-Carry Adder. It is to support a maximum of 5 bits. It will add binary numbers only.

Example:
 1011+11=1000 

I've already written a class and some functions to get input from the user, accept some commands and some arguments with those commands. At this time the program gets the input from the user (I'm taking both numbers in as a string) and converts each one to an integer to get some quick test cases in really and output them to make sure it is taking in the correct information.

All that is left is the algorithm. The online book we are using doesn't explain the algorithm very well though I do understand it I believe. Here is what I am looking at or was thinking. Storing both numbers in their own string variable then go through and loop through each string character (with only a max of 5 bits, their would only really be 5 characters to loop through) to see what to do with it. If their should be a carry or not and all that. Though for some reason I just feel I am totally off and should not have to store it as strings and check each and every character.

I was also looking at holding each number in their own integer array and looping through the arrays to do the ripple carry addition and check for the carries and storing the answer in an int and just return that int.

I just wanted to get some ideas how some of y'all would approach this, more than likely, pretty simple problem. I know I can't ask for answers here and I would never want to as that is not teaching me anything. Though some brainstorming and maybe ideas on what I could do to try and implement it.

##### Share on other sites
I didn't enjoy assignments like this when I was at uni. It isn't particularly well defined.
The first question that springs to my mind is: What counts as a ripple-carry adder? This is something I would try to clarify with your lecturer.

Without knowing that, there are a variety of possibilities but it isn't clear whether some of them get you any/much credit.
For example:

* Convert binary to decimal integers using bit manipulation, then just add the integers. It's not a ripple-carry adder but it takes the same inputs and produces the same output as one.

* Your idea: Keep the binary numbers as strings (or as a sequence of booleans) and manipulate them with some simple rules. Does that count as a ripple-carry adder? Ripple-carry adders don't really loop through the bits, but does that matter for this exercise?

* Design a 1-bit full adder, chain 5 of them up to one another and parameterise with inputs from the user.

* Design a Wire that can be connected to other Wires via logical operations (logic gates). Connect Wires up so as to model the schematic design of a ripple-carry adder.

I think this is in-keeping with the spirit of GDNet's homework policies. I believe they are in place just to prevent blatant cheating (which doesn't help anyone in the long term) whereas I view this answer, and your question, as a way of helping you to help yourself - which can only be a good thing Plus you seem sensible about it, your post is well presented, it contains ideas that are yours and you have a respectable rating, that's a whole different thing to someone who's signed up just to paste in their homework sheet and expects an easy ride. Edited by dmatter

##### Share on other sites
I appreciate the post dmatter and appreciate your thoughts about my post and more.

I will take your ideas and thoughts into consideration and come back to let you know on my thoughts after I have had time to think it though and see if I could see those in code.

Thanks.

##### Share on other sites
Ok I just wanted to post back and say which solution I decided to go with.

I really created two different solutions really but turned in only one. The first solution I created was I just used bit manipulation and added up to get the answer. This of course gave me the same input and output like dmatter posted. Worked perfectly. I talked to my professor though and decided I'd go ahead and go with my original idea.

I took in a command and two arguments. I wrote a really basic processor to take commands and arguments.
Example:
 add 101 11 or quit 
These are the only two commands I'd accept. If the user entered the add command then I simply extracted the arguments as a string and then split them up into two different strings/binary numbers. This would give me two numbers to add By doing it this way I could easily do some error checking to make sure the numbers entered were binary, not negative, and did not produce an overflow or were above the max number bits allowed (program asked for that to be 5). I also added leading 0's to the beginning of the strings if both strings/numbers were not equal length. I finally just looped through the number(s) an checked against some simple rules.

if all 3 bits (carry bit also) were 0, I would write down 0 and didn't carry anything. If one of the three bits were 1, I wrote down 1 and carried 0 If two of the three were 1, then I would write down 0 and carry 1. if all three were 1 then I wrote down and carried 1.

In the end I checked to see if there was a carry out, if there was I wrote that down. This then gave me a sum string that I would return.

I felt doing it this way would be better than just turning in a program that just did bit manipulation to give me the output. I felt this "followed" the algorithm a bit more and used the rules of the algorithm.

##### Share on other sites
Glad that you figured this out, but one thing I'd just like to double-check:

Example:
1011+11=1000

Shouldn't 1011+11=1110

or am I missing something?

##### Share on other sites
You're right it should be.
I was writing the original post directly after work that day so I was more than likely tired and not thinking. Thanks for bringing if to my attention though. I'll check it out to see if it was a simple typo on my part or to make sure the example I used isn't giving me the wrong answers.

I think I took that example from my programs description actually. Makes me wonder if they made a typo...

EDIT: when I get home from classes today Ill post my code. I'd be interested in getting some input to see if their we're faster ways I could have used or maybe if people could see some hidden pitfalls in the code I didn't think of.

##### Share on other sites
The description of a ripple carry adder in Wikipedia seems pretty streight forward even though it's describing an electronic circuit rather than a program. The main feature of the adder would seem to be that output from adding the previous bits is used as an input when adding the next bits. I would figure understanding the technique the circuit uses is the point of the assignment and your program is expected to follow the technique. I would expect either in this assignment, or a future one, for user input to come into play in which case you'll probably be using strings for input. If it's not currently a requirement, then obviously you don't need to work with strings but knowing what to do may help later. How you manipulate the data and whether you should store your data as a string or an int would be up to you but you should check on any requirements for the output and plan accordingly.

Just noticed you said you handed in the assignment. It sounds to me like you went about it the right way. Edited by kseh

##### Share on other sites
Well I handed in the assignment today actually via the site the we use. When the site tested my program for all cases it compiled but it it only passed one input and output test. Though it confuses me and I believe something is wrong on their end.

Example:
 add 101 11 ripple_carry> 1000 ripple_carry> FAIL : Input "add 101 11" did not produce output "1000". 

It says my program did not output 1000? umm yes it did? It has my output above it. As you can see it did. The funny thing: a couple lines down it tires that exact same input again and it passed it somehow by outputting the same thing.

I have already emailed my department actually to see what the issue may be. It just doesn't make sense how it fails the input and then passes it couple lines down.

##### Share on other sites
Sounds like you chose the right solution in the eyes of your professor. When time permits prototyping multiple solutions is often a good thing to try! Seems like you got some worthwhile educational value of this exercise

I'd be interested to see your solution, my C++ is probably rustier than I'd like it to be though. Edited by dmatter

##### Share on other sites

Sounds like you chose the right solution in the eyes of your professor. When time permits prototyping multiple solutions is often a good thing to try! Seems like you got some worthwhile educational value of this exercise

I'd be interested to see your solution, my C++ is probably rustier than I'd like it to be though.

I'll be posting the solution soon. My C++ is actually rusty too. I have been studying and creating some Windows forms using C# and .NET lately.

Ok I'll go ahead and post it. I would love to have any suggestions on what to fix and look at in the code. Pitfalls? The only slight bug that I know that I have is if the user only enters in one argument. In that case the second argument is automatically said to be what the first argument was.

I myself do feel like that their is a way or algorithm to get away from having to get away from hard coding all the possibilities for the adder. I would love to hear of anyway someone would go about that. Been a while since I was in C++ so I have forgot what was exactly is available to me and what I might have reinvent the wheel with.

EDIT: I posted the code but the forum seemed to cut off some random chunks...I'll try it again in a reply to this one. Edited by Chad Smith

1. 1
2. 2
3. 3
JoeJ
20
4. 4
5. 5
frob
19

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632213
• Total Posts
3004850

×