addition of 2 numbers stored as vectors

Hi, I'm trying to write a program to add 2 numbers, stored as vectors of characters. the larger number is a vector max with size lmax (int) and the smaller number min with size lmin(int).




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
// addition for overlapping digits, starting from the last character in each vector

int carry = 0;

int presum = 0;

int s = lmax;

vector <char> sum(s + 1);


for (i = lmin-1; i >= 0; i--)

{ 

 presum = min[i]+max[i + lmax - lmin] + carry;

 carry = presum / 10;

 sum[s] = static_cast <char> (presum % 10);

 s--;

}


// addition for the previous carry and the remaining digits of the bigger number


int z = lmax - lmin - 1;


for (z = lmax - lmin - 1; z >= 0; z--)

{

 presum = max[z] + carry;

 carry = presum / 10;

 sum[z] = static_cast <char> (presum %10);

}



// adds the carry over if remaining to the first digit of the sum


if (carry != 0)

{

 sum[max.size()+1] = static_cast <char> (carry);

}



when i run the program it doesn't output the addition result and gives an error warning after the user inputs the two numbers max and min. any idea what's wrong with the code? thankss
Because your sum vector is the result of static_cast <char> (presum %10); the only values you could have in there are 0..9. Theses (0...9) are not printable characters. To see what is in sum do
1
2
3
4
   for (i=0;i<sum.size();i++){
     cout << int(sum[i]);
   }
   cout << endl;

As for the adding, I'm not sure you indexes are correct. Assuming you have the vectors min and max like this
1
2
3
4
5
6
7
8
   vector<char> Max(5);
   vector<char> Min(3);
   // Set Max = 12345
   for (i=0;i<5;i++)
      Max[i] = i+1;
   // Set Min = 123
   for (i=0;i<3;i++)
      Min[i] = i+1;
The value I get when adding is
120468
Which is not the answer for 12345+123 = 12468, if I did what you are doing, code wise that is.
Fixed it, thank you!!
I got an update email but the post is not showing up. But I got your code from the email it sends.
now my code works for numbers with different digits but doesn't output
the addition of numbers with same digits eg. 2+2. help pleasee!

I ran your code and it worked for me. Here is what I have. There might be some stuff that is unused, just ignore it.
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(){
   int carry(0),i(0);
   int presum(0);
   vector<char> max(1);
   vector<char> min(1);

   // Set the input to 2 and 2
   for (i=0;i<5;i++)
      max[i] = i+0x32;
   for (i=0;i<3;i++)
      min[i] = i+0x32;

   int lmax(max.size()),maxIdx(lmax-1);
   int lmin(min.size()),minIdx(lmin-1);
   int s(lmax);

   int mini = 0;
   int maxi = 0;
   int sumi = 0;
   int x = 0;
   char y = '0';

   vector<char>sum(0);


   for (i = lmin-1; i >= 0; i--) { 
      mini = static_cast<int> (min[i]) - static_cast<int>('0');
      maxi = static_cast<int> (max[i+lmax-lmin]) - static_cast<int>('0');
      presum = mini + maxi + carry;
      carry = presum / 10;
      x = presum % 10;
      char y = x + '0';
      // sum is stored in reverse order
      sum.push_back(y);
   }


   // addition for the previous carry and the remaining digits of the bigger number
   if (lmax != lmin) {
      int z = lmax - lmin - 1;
      for (z = lmax - lmin - 1; z >= 0; z--) {
         presum = (static_cast<int>(max[z]) - static_cast<int>('0')) + carry;
         carry = presum / 10;
         x = presum % 10;
         char y = x + '0';
         sum.push_back(y);
      }
   }

   // adds the carry over if remaining to the first digit of the sum
   if (carry != 0) {
      char y = carry + '0';
      sum.push_back(y);
   }


   // output the result of summation
   for (i=0; i<min.size(); i++)
      cout << min[i];
   cout << " + ";
   for (i=0; i<max.size(); i++)
     cout << max [i];
   
   cout << " = ";
   for (i=(static_cast<int>(sum.size())-1); i>=0; i--)
     cout << sum[i];
   cout << "\n";

   return 0;   
}
$ ./a.out
2 + 2 = 4
$ 

Worked!
Topic archived. No new replies allowed.