Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jun 1st, 2006, 3:50 AM   #1
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
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
 

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 10:38 AM.

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