Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2006, 4:50 AM   #1
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 340
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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 number

examples:

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
mrynit is offline   Reply With Quote
Old Jun 1st, 2006, 6:08 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 1st, 2006, 2:09 PM   #3
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 340
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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
mrynit is offline   Reply With Quote
Old Jun 1st, 2006, 5:10 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 1st, 2006, 5:50 PM   #5
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Hardware Adders
Why don't you let the hardware do the adding?
Harakim is offline   Reply With Quote
Old Jun 1st, 2006, 7:31 PM   #6
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
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
gryfang is offline   Reply With Quote
Old Jun 2nd, 2006, 12:18 AM   #7
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 340
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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
mrynit is offline   Reply With Quote
Old Jun 2nd, 2006, 1:01 AM   #8
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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;
	}
with this:
sum = n1 + n2
Hope this helps :-)
__________________
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 1:18 AM.
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC