no thing
no thing

Reputation: 21

Addition and subtraction of arrays

I have been working on this all day but just cant get the output right. What I want to do is input two numbers, which would be pushed into two arrays so we can subtract or add them and display the result. Seems simple, but there are few catches

  1. Input must be pushed into the array, from the user, one by one.
  2. In case I don't enter a value, code should assume it to be '0' or 'null'. '0' if its in the beginning and 'null' if its in the end. for example if 1st number is 234 and second number is 23 then code should make it into '023' and if I enter first number as 2, 2nd number as 3 but don't enter anything in the end then code should assume it to be null.

Problems

  1. I cant take 'carry' to the next set, in case the sum is greater than 10. Which means the value I m getting is just addition of two numbers doesn't matter if its greater than 10 or not. for example addition of 234 and 890 is giving me [10, 12, 4]

Here is the code.....

#include<iostream>
using namespace std;

main() {
    int first[10], second[10], result[10], c, n;

    cout << "Enter the number of elements in the array ";
    cin >> n;
    if (n > 10 || n < 0) {
        std::cout << "invalid number, you are a bad reader" << endl;
        system("PAUSE");
        return 0;
    }

    cout << "Enter elements of first array " << endl;

    for (c = 0; c < n; c++) {
        cin >> first[c];
        if (first[c] > 9 || first[c] < 0) {
            std::cout << "invalid number, you are a bad reader" << endl;
            system("PAUSE");
            return 0;
        }

    }
    cout << "Enter elements of second array " << endl;

    for (c = 0; c < n; c++)
        cin >> second[c];

    cout << "Sum of elements of two arrays " << endl;

    for (c = 0; c < n; c++)
        cout << first[c] + second[c] << endl;

    if ((first[c] + second[c]) > 9) {
        cout << "overflow" << endl;
    }

    //result[c] = first[c] + second [c];
    //cout << result[c] <<endl;
    system("PAUSE");
    return 0;
}

I would really appreciate some suggestions.

Upvotes: 0

Views: 6524

Answers (2)

jmucchiello
jmucchiello

Reputation: 18984

Use std::vector and learn how to use reverse iterators. So if someone enters 234 you push_back(2), push_back(3), push_back(4) and have [0]=2,[1]=3,[2]=4. Then if the next number is 23 you have [0]=2,[1]=3. Now walk both vector with reverse iterators so the first call to rbegin() will give a pointer to [2]=4 and the other vector will give [1]=3. Now add and carry using push_back into a third vector to store the result. Output the result using reverse iterators to print the result.

This looks like homework, so no sample code.

Upvotes: 0

Keugyeol
Keugyeol

Reputation: 2435

In case your intention is to have the result of e.g.

234 + 890 = 1124

then your summation loop should be in reverse order. (Since you are reading number of elements of the array from the prompt, you may use this information to input first/second numbers into each array in the order preferred for the following summation loop.)

For the carry problem, you need to setup a variable and use it in the loop like this, for example.

int sum[10] = {0};
int c;

int carry = 0;
for (c = 0; c < n; c++)
{
    sum[c] = first[c] + second[c] + carry;
    carry = sum[c] / 10;
}
if (c < n)
    sum[c] = carry;
else
    cout << "overflow";

Upvotes: 1

Related Questions