1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

add to subtraction

Discussion in 'C' started by nanochan1, Oct 27, 2010.

  1. nanochan1

    nanochan1 New Member

    Joined:
    Oct 27, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  3. nanochan1

    nanochan1 New Member

    Joined:
    Oct 27, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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)
    
     
  4. nanochan1

    nanochan1 New Member

    Joined:
    Oct 27, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. nanochan1

    nanochan1 New Member

    Joined:
    Oct 27, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 [I]0[/I] instead of three [I]0s[/I] isn't it?)
    
    Code:
    input1: 0005
    input2: 0005
    output: 0000 (it should automatically become one [I]0[/I] instead of three [I]0s[/I] isn't it?)
    

    appreciate all assistance render.
     

Share This Page