![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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.
1. IF a2 >= 10 - a1 THEN a1 + a2 >= 10
i = 10 - a1
j = a2 - i
a1 + a2 = 10 + j
2. 9 + n = 10 + (n - 1) where 0 < n <= 9
3. 0 + n = n where n is any real numberexamples:
1. 5 + 7 = ?
a1 = 7 a2 = 5
5 >= 10 - 7 --> 5 >= 3 True
i = 10 - 7 --> i = 3
j = 5 - 3 --> j = 2
5 + 7 = 10 + 2 --> 12
2. 6 + 9 = ?
10 + (6 - 1) --> 10 + 5 --> 15
3. 0 + 4 = 4 duh!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. /******************************************************************************\ |programmer: Christopher |conact: mrynit@yahoo.com |date made: 5-31-06 |last mod: 5-31-06 |compiler: Borland C++ Compiler 5.5 |OS: XPpro SP2 |CPU: AMD Athlon 64 +3000 | |goal: the addtion of one digit positive integers by specific functions. | |license: free for personal and eduactional uses. please give acknowledgments |to me. | \******************************************************************************/ #include <iostream> #include <ctime> // For time() #include <cstdlib> // For srand() and rand() using namespace std; int main() { //varriable declaration int n1,n2,a1,a2,sum,i,count = 0; //generate two random integer between zero and nice inclusively srand( (unsigned)time( NULL) ); //get random seed from cpu clock n1 = rand()%10; n2 = rand()%10; //sort n1 and n2 so that a1 is the largest of n1 and n2. a2 is the least. if (n1 >= n2) { a1 = n1; a2 = n2; } else { a1 = n2; a2 = n1; } //if both a1 and a2 are zero then sum is zero if (a1 == 0) { if (a2 == 0) sum = 0; } //if a2 is zero then sum is a1 if (a2 == 0) sum = a1; //if a1 is nine and a2 is not zero then sum is a2 minus one if (a1 == 9) { if (a2 != 0) { sum = (a2 - 1); count++;//carry the 10 } } //if a2 is greater than or equal to ten minus a1, which equals i, //then a2 minus i is the sum. //if a2 is not greater than or equal to ten minus a1 //then sum is a1 plus a2. //the above logic takes care of all nine plus any number combinations //thus if a1 is nine this block of code does not need to be run. //this also prevents the carry of 10 being counted twice. if (a1 != 9) { i = (10 - a1); if (a2 >= i) { sum = a2 - i; count++;//carry the 10 } else sum = a1 + a2; } //display the sum of a1 and a2. cout << a1 << " + " << a2 << " = " << (sum + (count * 10)); //(sum + (count * 10)) will be changed when programm handdles larger numbers. return 0; } 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.
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Why should you be concerned with speed and efficiency? You chucked all that when you chose your approach.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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?
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Given your method as the basis of calculation, how do you get the subraction portrayed there? You're cheating.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
|
|
|
|
|
|
#6 |
|
Programmer
|
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).
__________________
-------------------- LOAD "*" ,8,1 God bless - Gryfang |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
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:
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Replace this:
//sort n1 and n2 so that a1 is the largest of n1 and n2. a2 is the least.
if (n1 >= n2)
{
a1 = n1;
a2 = n2;
}
else
{
a1 = n2;
a2 = n1;
}
//if both a1 and a2 are zero then sum is zero
if (a1 == 0)
{
if (a2 == 0)
sum = 0;
}
//if a2 is zero then sum is a1
if (a2 == 0)
sum = a1;
//if a1 is nine and a2 is not zero then sum is a2 minus one
if (a1 == 9)
{
if (a2 != 0)
{
sum = (a2 - 1);
count++;//carry the 10
}
}
//if a2 is greater than or equal to ten minus a1, which equals i,
//then a2 minus i is the sum.
//if a2 is not greater than or equal to ten minus a1
//then sum is a1 plus a2.
//the above logic takes care of all nine plus any number combinations
//thus if a1 is nine this block of code does not need to be run.
//this also prevents the carry of 10 being counted twice.
if (a1 != 9)
{
i = (10 - a1);
if (a2 >= i)
{
sum = a2 - i;
count++;//carry the 10
}
else
sum = a1 + a2;
}sum = n1 + n2
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot Last edited by uman; Jun 2nd, 2006 at 12:18 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|