Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   add to subtraction (http://www.go4expert.com/forums/add-to-subtraction-t23683/)

nanochan1 27Oct2010 17:00

add to subtraction
 
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?

xpi0t0s 28Oct2010 11:45

Re: add to subtraction
 
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?

nanochan1 28Oct2010 19:43

Re: add to subtraction
 
Quote:

Originally Posted by xpi0t0s (Post 74168)
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)


nanochan1 28Oct2010 19:46

Re: add to subtraction
 
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.

nanochan1 28Oct2010 19:49

Re: add to subtraction
 
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.


All times are GMT +5.5. The time now is 04:47.