GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jan-2009, 09:29
Tishtish Tishtish is offline
New Member
 
Join Date: Jan 2009
Posts: 1
Tishtish is on a distinguished road

Simple stuff... or so you'd think!


I was wondering if you could help. I need to find out how to write an addition program without assuming the computer already knows how to add two numbers together.

Has anyone a clue how to go about this?
  #2  
Old 22-Jan-2009, 11:09
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Simple stuff... or so you'd think!


If the computer can loop and increment/decrement, here is how you would add two integers:

CPP / C++ / C Code:
int Add( int a, int b )
{
  int i;
  int result = a;

  if( b < 0 )
  {
    for(i=0;i>b;--i) --result;
  }
  else
  { 
    for(i=0;i<b;++i) ++result;
  }
 
  return result;  
}
  #3  
Old 22-Jan-2009, 13:13
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Simple stuff... or so you'd think!


Quote:
Originally Posted by Tishtish
I was wondering if you could help. I need to find out how to write an addition program without assuming the computer already knows how to add two numbers together.

Has anyone a clue how to go about this?

Not as simple as fakepoo's, but interesting is:

CPP / C++ / C Code:
int add(const int a, const int b)
{
    int c, s, t;
    if(a > 0 || b > 0)
    {
        s = a ^ b;
        c = a & b;
        while(c > 0)
        {
            c = c << 1;
            t = s;
            s = s ^ c;
            c = c & t;
        }
    }
    else
    {
        s = add(-a, -b);
        s = -s;
    }
    return s;
}


MxB
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
In method `void InfixExpression::brCalc(double **, char **, int, int, int, int)' takachi C++ Forum 12 18-Feb-2008 13:36
Simple login reveals extra menu/buttons... skuliaxe MySQL / PHP Forum 1 15-Feb-2008 13:44
Please help me to convert a simple C++ program into an object-oriented one. zekesteer C++ Forum 5 05-Jan-2008 11:05
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE elcrazy C++ Forum 1 11-Mar-2007 22:42
[PROGRAM] Simple 8-Puzzle Game Max Payne MS Visual C++ / MFC Forum 0 09-May-2004 22:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.