add to subtraction

nanochan1's Avatar, Join Date: Oct 2010
Newbie Member
Code:
string addArray (int* a1, int* a2, int size1, int size2)
{
    string answer;
    int* big, *small;
    int temp, end1, end2;
    bool upNext = false;
    
    if (size1 > size2)
    {
        end1 = size1;
        end2 = size2;
        big = a1;
        small = a2;
    
    }
    else
    {
        end1 = size2;
        end2 = size1;
        big = a2;
        small = a1;
    }
    
        
    for (int i = 0; i < end1; i++)
    {
        temp = 0;
        
        if (upNext == true)
        {
            temp += 1;
            upNext = false;
        }
        
        if(i < end2)
        {    
            temp += *big + *small;
                        
            big++; 
            small++;
        }
        else
        {
            temp += *big; 
        
            big++;
        }
        
        if((temp-10) >= 0)
        {
            temp -= 10;
            upNext = true;
        }
            
        answer += static_cast<char>((temp + 48));
    }
    if (upNext == true)
        answer += static_cast<char>(49);
            
    return answer;
}
how to change it to become subtraction? any idea?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Not sure, could it be to change
Code:
temp += *big + *small;
to
Code:
temp += *big - *small;
?

But it really depends which addition you want to change to subtraction, which you haven't said. What does the current code do, and what do you want the modified code to do, with examples?
0
nanochan1's Avatar, Join Date: Oct 2010
Newbie Member
Quote:
Originally Posted by xpi0t0s View Post
Not sure, could it be to change
Code:
temp += *big + *small;
to
Code:
temp += *big - *small;
?

But it really depends which addition you want to change to subtraction, which you haven't said. What does the current code do, and what do you want the modified code to do, with examples?
thanks..

i've tried that but it merely works for single digit integers..

it doesn't bring over from the neighbour for the subtraction, any advise?

eg,

Code:
input1: 9
input2: 8
output: 1
but when i attempted to do this

Code:
input1: 10
input2: 9
output: 09   (answer should be 1)
and

Code:
input1: 8
input2: 9
output: /   (answer should be -1)
0
nanochan1's Avatar, Join Date: Oct 2010
Newbie Member
the subtraction should behind like the examples given above.

currently, my codes above can do addition with no issues.

eg. (for the above addition code that i've done)

Code:
input1: 10
input2: 9
output: 19
Code:
input1: 99
input2: 101
output: 200
Code:
input1: 1
input2: 9
output: 10
the addition part that i've done are all correct.
0
nanochan1's Avatar, Join Date: Oct 2010
Newbie Member
and if possible i would prefer the subtraction part when i tried to do subtraction it output in this manner:

Code:
input1: 000
input2: 000
output: 000 (it should automatically become one 0 instead of three 0s isn't it?)
Code:
input1: 0005
input2: 0005
output: 0000 (it should automatically become one 0 instead of three 0s isn't it?)

appreciate all assistance render.