2

I'm relatively new to programming :).

Suppose I wanted to create a program which prompts a user to enter two positive numbers up to 50 digits long, which then subtracts the second number from the first number.

For example:

The user enters the first positive number: 239834095803945862440385983452184985298358

second number: 939542309853120721934217021372984729812

===========================================================================

Program outputs the difference: 238894553494092741718901766430812000568564

OR, if negative: -29837430045

===========================================================================

Each digit of the numbers will be stored as individual elements in an array. Here is how I am currently taking in the user input:

int read_array(int int_array[], int MAX_SIZE) {
    char number;
    int count = 0;
    //set all array entries to 0. 
    for (int i = 0; i < MAX_SIZE; i++){
        int_array[i] = 0;
    }

    do { //processes each individual char in the istream
        cin.get(number);
        // puts char on to the array until it hits the
        // end of the number (end of the line)
        if(( number != '\n') && (count < MAX_SIZE) && (isdigit(number))) {
            int_array[count] = int(number) - int('0');
        }  
        count++; //increments count
    } while (number != '\n');

    //tests if number is too large
    int digitcount = count - 1;
    if (digitcount > MAX_SIZE) {
        cout << endl << "ERROR: The number is above 50 digits!" << endl;
        return 0;
    }

THE PROBLEM:

HOW to do the subtraction is eluding me.I have been trying to solve this problem for two weeks and it's most likely something trivial I have missed.

I have tried:

  1. Converting array of elements back into one whole int
  2. Writing my own program to do long subtraction on the numbers

etc...

However, the output is ONLY successful up until a certain number of digits and/or if they're positive/negative numbers. I'm stumped and I'm not sure what the best way is to subtract two positive number arrays to get a successful output that can accommodate for positive and negative numbers as shown in the example. ANY HELP GREATLY APPRECIATED :).

EDIT: my attempts:

#include "long_sub.h"
#include <sstream>
#include <vector>

using namespace std;

int long_sub(int a[], int b[], const int size) {
    stringstream ss;
    int const sizes = 50;
    int c = 0; //borrow number
    int borrow = 1; // the '1' that gets carried to the borrowed number
    int r[sizes];

    for (int i = 0; i < size; i++) {
        r[i] = 0;
    }    
    //initialise answer array to 0.
    for (int i = size - 1; i >= 0; i--) {
        //handles zeros
        if (a[i] < b[i] && a[i]) {
            //takes the borrow from the next unit and appends to a.
            ss << borrow << a[i];
            ss >> c;
            ss.clear(); // clears stringstream for next potential borrow.

            int temp = c - b[i];
            r[i] = abs(temp);
        } else {
            int temp = a[i] - b[i];
            r[i] = abs(temp);
        }
    }

    for (int i = 0; i <= size - 1; i++ ) {
        cout << r[i];
    }
    cout << endl;
    return r[sizes];
}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Mark
  • 87
  • 1
  • 2
  • 11

1 Answers1

4

So the solution to this is pretty much the same as when you do it by hand.

If we have:

 4321
-1234

You'd take the last digits in the two numbers, subtract the one below from the one above, 1 - 4 - which of course means you have to borrow from the next digit up, so we remmeber that, and then come up with 7. Now, take the next digit [and remember the "borrow"], and subtract 3 from 2-1 = 8.

The exact same thing is how you do subtraction on large numbers in computer - do one bit at a time, and if you "borrow", then you need to take that with you to the next step.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks for your quick response :). I did in fact write a program that did this. It worked up to a point. However, it got stuck on zeros and borrowing from zeros... however, it just occurred to me I didn't have anything to keep track of the number length, I will try to include that and see if it keeps giving me dodgy output :) – Mark Feb 14 '13 at 13:12
  • Of course, if working through your long numbers digit by digit is irksome, you can always treat, say, groups of 3 digits (counting from the least significant digit) and perform arithmetic on those, not forgetting the carries. – High Performance Mark Feb 14 '13 at 13:13
  • I added an example of my long subtraction code for you to rip apart at your own leisure, ha ha :). – Mark Feb 14 '13 at 13:17