![]() |
adding of integers
this code is part of my calculator program im working on. the code is based on the simplification of addition to deal only with positive signle digit integers. you see when i was growing up i could not figure out how to add numbers whos sum was greater than ten. so i figured out some tricks.
:
given two positve integers between 0 and 9 inclusively: a1 and a 2 , such that a1 >= a2.:
examples:the idea is to only deal with simple adding and subtracting. addition is always going to be a single digit untile it is added to the carried 10 and other units if there are any. this consept applies to larger addtion such as six three digit numbers added up. only deal with addin up the one's unit and carry over any 10's then move to the next unit colum and repete. when combinding the above three adding rules with some logic i came up with the code below. :
/******************************************************************************\so far the code works. the random numbers are not so random but they will be replaced by user input. i ask of your leet coding knowledge to help me make the above code beter ie run faster and more efficiently. |
Why should you be concerned with speed and efficiency? You chucked all that when you chose your approach.
|
what im trying to do is figure out how to do addition of positive integers in the easist way. the above aproch was what i first came up with. believe the consept work so far. i know the aproch doesnt seem like it saves people any time but when i was working on it i thought it may have some corilation to how softwear/hardwear does addition. i have not looked into that part yet. i was wondering if the code does what it is intended to do in the most effient manner. could the logic be structured in a better way to achive the same goal?
|
Given your method as the basis of calculation, how do you get the subraction portrayed there? You're cheating.
|
Hardware Adders
Why don't you let the hardware do the adding? :) |
The subtraction is cheating a LOT.
Adding and subtracting is done in Binary, by dealing with it in Decimal you actually have to increment, decriment, add, and subtract in addition to the comparing (which is done in the hardware and XORs, ORs, ANDs, etc.) To improve your code I would suggest working in Binary and figuring out how to ADD without using addition (look at your else cases). |
thanks for the advice. i now see how the subtraction is cheating. the method i developed is just hanndy for ppl addition not compy addition. i'll start dealing with binrary addition. i glanced at the wiki on binary addition then thought that would be cheating my self. im going to try to figure as much as i can by my self before i start looking the stuff up. i have some prototype boards, transistors and LED's so this should be fun. :banana:
|
Replace this:
:
//sort n1 and n2 so that a1 is the largest of n1 and n2. a2 is the least.:
sum = n1 + n2 |
| All times are GMT -5. The time now is 8:04 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC