Adding numbers in an array

Nov 30, 2011 at 7:45am
I am having trouble with a program I am working on that takes in 2 numbers from the user as a string, converts the number to individual integer values stored in arrays, then displays the sum of those two numbers to the screen. My problem comes when two of the numbers that are in the same factor of 10 relative to each other add up to more than 9. I can't figure out a way to carry over the resulting one to the next factor of 10. This is what I have so far.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    do {
    int place_holder1 = 0;
    int place_holder2 = 0;
    int *val_num1; 
    int *val_num2;
    int *val_fin;
    int size;
    cout << "Please enter the size of your number: ";
    cin >> size;
    val_num1 = new int[size - 1];
    val_num2 = new int[size - 1];
    val_fin = new int[size - 1];
    string number1;
    string number2;
    cout << "Please enter your first number, then press enter: ";
    cin >> number1;
    
    for (int i = size - 1; i >= 0; i--) {
        switch (number1[i]) {
            case '0':
                val_num1[size - 1 - i] = 0;
                break;
            case '1':
                val_num1[size - 1 - i] = 1;
                break;
            case '2':
                val_num1[size - 1 - i] = 2;
                break;
            case '3':
                val_num1[size - 1 - i] = 3;
                break;
            case '4':
                val_num1[size - 1 - i] = 4;
                break;
            case '5':
                val_num1[size - 1 - i] = 5;
                break;
            case '6':
                val_num1[size - 1 - i] = 6;
                break;
            case '7':
                val_num1[size - 1 - i] = 7;
                break;
            case '8':
                val_num1[size - 1 - i] = 8;
                break;
            case '9':
                val_num1[size - 1 - i] = 9;
                break;
            default:
                place_holder1++;
                break;
        }
    }
    
    
    cout << "\nPlease enter your second number, then press enter: ";
    cin >> number2;
    
    for (int i = size - 1; i >= 0; i--) {
        switch (number2[i]) {
            case '0':
                val_num2[size - 1 - i] = 0;
                break;
            case '1':
                val_num2[size - 1 - i] = 1;
                break;
            case '2':
                val_num2[size - 1 - i] = 2;
                break;
            case '3':
                val_num2[size - 1 -i] = 3;
                break;
            case '4':
                val_num2[size - 1 - i] = 4;
                break;
            case '5':
                val_num2[size - 1 - i] = 5;
                break;
            case '6':
                val_num2[size - 1 - i] = 6;
                break;
            case '7':
                val_num2[size - 1 - i] = 7;
                break;
            case '8':
                val_num2[size - 1 - i] = 8;
                break;
            case '9':
                val_num2[size - 1 - i] = 9;
                break;
            default:
                place_holder2++;
                break;
        }
    }
    
    for (int i = 0; i < size; i++) {
        val_num1[i] = val_num1[i + place_holder1];
        val_num2[i] = val_num2[i + place_holder2];
        val_fin[size - i - 1] = val_num1[i] + val_num2[i];
    }
    
    for (int i = 0 ; i < size ; i++) {
            cout << val_fin[i];
    }    
}


I have tried the following . . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < size; i++) {
        val_num1[i] = val_num1[i + place_holder1];
        val_num2[i] = val_num2[i + place_holder2];
        if ((val_num1[i] + val_num2[i]) % 10 == val_num1[i] + val_num2[i])
            val_fin[size - i - 1] = val_num1[i] + val_num2[i];
        else {
            val_fin[size - i] = (val_num1[i] + val_num2[i]) % 10;
            val_num1[i + 1] = val_num1[i + 1] + 1;
        }
    }
    
    for (int i = 0 ; i < size ; i++) {
            cout << val_fin[i];
    }   


but I have found that this does not work. I know that one solution would involve taking the sum of the two numbers and using mod 10 to check if it is a single digit number, but I can't figure out how to get the one to carry over. Any help would be greatly appreciated. I would also be more than happy to hear about any inefficiencies or errors in conventional coding syntax for my own improvement.
Nov 30, 2011 at 10:24am
I'd say something like this:
1
2
3
4
5
6
// Outside loop: 
int carry(0);
// Inside loop:
val_fin[i] = val1[i] + val2[i] + carry;
carry = val_fin[i]/10;
val_fin[i] -= 10*carry;


For example, the numbers 88 and 34:
It1: val_fin[0] = 4 + 8 + 0 = 12.
      carry = 12/10 = 1.
      val_fin[0] - 10 = 2.
It2: val_fin[1] = 8 + 3 + 1 = 12.
      carry = 12/10 = 1.
      val_fin[1] -= 10 = 2.
It3: val_fin[2] = 0 + 0 + 1 = 1.
      carry = 1/10 = 0.
      val_fin[2] - 0 = 1.

val_fin is now {2, 2, 1}, or 122, the sum of 88 and 34.

This can easily be expanded to 3, 4, ... n numbers. carry will always have the correct count of "tens", making sure that the current digit doesn't exceed 10 and the following digit will have all the carry-over.

You can also do it with the modulo operator (replace the 3rd line with val_fin[i] %= 10), but the modulo operator is quite expensive and since you need the carryover anyway, I find it more logical to do it this way.
Last edited on Nov 30, 2011 at 10:25am
Topic archived. No new replies allowed.