subtraction with vectors

Hi, I have a code for subtraction of a larger number to a smaller number. It works for most numbers but I've found error with
1: 100-(1 to 9) = 99
2. the program doesn't output answers that are single digits.

I'm using functions to multiply and output answer, and the output function works fine to output single digits. I've also included functions called erase (to erase leading zeros) and flip (to flip the vectors, because the initial result is stored backwards).

Please help!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

// this function outputs a vector
void output (vector <char> v)
{
    for (i=0; i <= v.size(); i++ )
        cout << v[i];
}


// this function flips the order of a vector 
vector<char>flip(vector<char>num)
{
    vector<char>backwards(num.size());
    for (size_t i=0; i<num.size(); i++)
        backwards[i] = num[num.size() - i - 1];
    
    return backwards;
}

// this function erases leading zeros
vector <char> erase (vector <char> number)
{
    i=0;
    
    number = flip(number);
    
    while (number.size() > 1 && number[number.size()-1] == '0')
        number.pop_back();
   
    number = flip(number);
    number.pop_back();
    return number;
    
}// end of erase function
// this function substracts a larger number to a smaller number
vector <char> substraction(const vector <char> &max, const vector <char> &min)
{
    int mini=0;
    int maxi=0;
    int lmax = max.size();
    int lmin = min.size();
    int pretimes = 0;
    int carry = 0;
    char times;
    vector <char> result;
    
    for (i = lmin-1; i >= 0; i--)
        
    { 
        // the following is to properly cast the character in each slot of vector that is to be used into integer
        // the desired integer is the difference between the ask value of the character in question and the character '0'
        mini = static_cast<int> (min[i]) - static_cast<int>('0');
        
        maxi = static_cast<int> (max[i+lmax-lmin]) - static_cast<int>('0');
        
        // do substraction for overlapping digits
        if ((maxi-carry) >= mini)
        {
            pretimes = maxi - carry - mini;
            carry = 0;
        }        
        
        else 
        {
            pretimes = (maxi -carry + 10) - mini;
            carry = 1;
        }
        
        times = pretimes + '0';
        result.push_back(times);
        
    }
    
    if (lmax > lmin)
    {
        int z = lmax - lmin - 1;
        
        for (z = lmax - lmin - 1; z >= 0; z--)
            
        {
            if ((static_cast<int>(max[z]) - static_cast<int>('0') - carry) >= 0)
            {
                pretimes = (static_cast<int>(max[z]) - static_cast<int>('0')) - carry;
                carry = 0;
            }            
            
            else
            {
                pretimes = pretimes = (static_cast<int>(max[z]) - static_cast<int>('0') + 10) - carry;
                carry = 1;
                
            }
            
            times = pretimes + '0';
            result.push_back(times);
            
        }
        
    }
    
    result = flip(result);
    result = erase(result);
    return result;   
    
}
Removed the extra calls to flip() and removed a call to number.pop_back() to get it to work. Look for the comments with CHANGED and REMOVED.
It looks like it is working now, but test it some more.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <vector>
#include <iostream>
using namespace std;

// this function outputs a vector
void output (vector <char> v)
{
// CHANGED, added int infront of i
    for (int i=0; i < v.size(); i++ )
        cout << v[i];
// CHANGED, added endl
    cout << endl;
}


// this function flips the order of a vector 
vector<char>flip(vector<char>num)
{
    vector<char>backwards(num.size());
    for (size_t i=0; i<num.size(); i++)
        backwards[i] = num[num.size() - i - 1];
    return backwards;
}

// this function erases leading zeros
vector <char> erase (vector <char> number)
{
// CHANGED, added int infront of i
    int i=0;
    
// REMOVED
//number = flip(number);
    while (number.size() > 1 && number[number.size()-1] == '0'){
        number.pop_back();
    }
   
    number = flip(number);
// REMOVED
//number.pop_back();
    return number;
    
}// end of erase function
// this function substracts a larger number to a smaller number
vector <char> substraction(const vector <char> &max, const vector <char> &min)
{
    int mini=0;
    int maxi=0;
    int lmax = max.size();
    int lmin = min.size();
    int pretimes = 0;
    int carry = 0;
    char times;
    vector <char> result;
    
    for (int i = lmin-1; i >= 0; i--)
    { 
        // the following is to properly cast the character in each slot of vector
        // that is to be used into integer the desired integer is the difference 
        // between the ask value of the character in question and the character '0'
        mini = static_cast<int> (min[i]) - static_cast<int>('0');
        
        maxi = static_cast<int> (max[i+lmax-lmin]) - static_cast<int>('0');
        
        // do substraction for overlapping digits
        if ((maxi-carry) >= mini)
        {
            pretimes = maxi - carry - mini;
            carry = 0;
        }        
        
        else 
        {
            pretimes = (maxi -carry + 10) - mini;
            carry = 1;
        }
        
        times = pretimes + '0';
        result.push_back(times);
        
    }
    
    if (lmax > lmin)
    {
        int z = lmax - lmin - 1;
        
        for (z = lmax - lmin - 1; z >= 0; z--)
            
        {
            if ((static_cast<int>(max[z]) - static_cast<int>('0') - carry) >= 0)
            {
                pretimes = (static_cast<int>(max[z]) - static_cast<int>('0')) - carry;
                carry = 0;
            }            
            
            else
            {
// CHANGED
                pretimes = (static_cast<int>(max[z]) - static_cast<int>('0') + 10) - carry;
                carry = 1;
                
            }
            
            times = pretimes + '0';
            result.push_back(times);
            
        }
        
    }
// REMOVED
//result = flip(result);
    result = erase(result);
    return result;   
    
}

int main(){
  vector<char> a,b;

   a.push_back('1');
   a.push_back('0');
   a.push_back('0');

   b.push_back('9');
   b.push_back('7');

   output(a);
   output(b);
   cout << "------" << endl;
   output(substraction(a,b));

  return 0;
}
$ ./a.out
100
97
------
3

Got the output of 3 for 100-97.
yep it's working now, thank you so much!!!
Topic archived. No new replies allowed.