Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3806184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:47:43+00:00 2026-05-19T14:47:43+00:00

I am writing a decimal multiplier in C++. The multiplier is implemented by taking

  • 0

I am writing a decimal multiplier in C++. The multiplier is implemented by taking two integers represented as vectors of digits. Each vector stores its digits in reverse order, for easier representation by powers of ten. For example, 3497 = 3 * 10^3 + 4 * 10^2 + 9 * 10^1 + 7 *10^0 and is stored in a vector as {7, 9, 4, 3}. Thus, each index of the vector represents that digit’s respective power of ten in the integer.

However, I’m having some bugs in my multiplication. 1-digit x 1-digit and 2-digit x 1-digit multiplication work perfectly, but it breaks down at 2-digit x 2-digit. I’m fairly certain that fixing this bug would fix all the other bugs with n-digit x m-digit. My code for both my multiplication and addition methods are below.

vector<int> multiply(vector<int> &a, vector<int> &b) {
    // check for emptiness
    if(a.size() == 0)
        return b;
    else if(b.size() == 0)
        return a;

    unsigned int i; // increment counter

    // compensate for size differences
    if(a.size() > b.size())
        for(i = 0; i < a.size()-b.size(); ++i)
            b.push_back(0);
    else
        for(i = 0; i < b.size()-a.size(); ++i)
            a.push_back(0);

    int c = 0; // carry value
    int temp; // temporary integer
    vector<int> p; // product vector
    vector<int> s; // sum vector
    s.push_back(0); // initialize to 0

    // multiply each digit of a by an index of b
    for(i = 0; i < b.size(); ++i) {
        // skip digits of 0
        if(b[i] == 0)
            continue;

        p.resize(i,0); // resize p and add 0s

        // multiply b[i] by each digit of a
        for(unsigned int j = 0; j < a.size(); ++j) {
            temp = c + b[i] * a[j]; // calculate temporary value

            // two cases
            if(temp > 9) {
                c = temp / 10; // new carry
                p.push_back(temp % 10);
            }
            else {
                c = 0;
                p.push_back(temp);
            }
        }

        // append carry if relevant
        if(c != 0)
            p.push_back(c);

        // sum p and s
        s = add(p, s);
        p.clear(); // empty p
    }

    return s; // return summed vector (total product)
}

vector<int> add(vector<int> &a, vector<int> &b) {
    // check for emptiness
    if(a.size() == 0)
        return b;
    else if(b.size() == 0)
        return a;

    unsigned int i; // increment counter

    // compensate size differences
    if(a.size() > b.size())
        for(i = 0; i < a.size()-b.size(); ++i)
            b.push_back(0);
    else
        for(i = 0; i < b.size()-a.size(); ++i)
            a.push_back(0);

    int c = 0; // carry value
    vector<int> s; // sum vector
    int temp; // temporary value

    // iterate through decimal vectors
    for(i = 0; i < a.size(); ++i) {
        temp = c + a[i] + b[i]; // sum three terms

        // two cases
        if(temp > 9) {
            c = temp / 10; // new carry
            s.push_back(temp % 10); // push back remainder
        }
        else {
            c = 0;
            s.push_back(temp);
        }
    }

    // append carry if relevant
    if(c != 0)
        s.push_back(c);

    return s; // return sum
}

A few test cases:

45 * 16 returns 740 (should be 720)
34 * 18 returns 542 (should be 532)
67 * 29 returns 2003 (should be 1943)
28 * 12 returns 336 (correct)

The only problem I could think of would be an issue with the carries, but everything seems to check out as I walk through the code. Can anyone see the error? Or am I taking the wrong approach to this entirely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:47:43+00:00Added an answer on May 19, 2026 at 2:47 pm

    You’re not clearing out the carry before (or after) the inner loop.

        // iterate through decimal vectors
        for(i = 0; i < a.size(); ++i) {
            ...
        }
    
        // append carry if relevant
        if(c != 0) {
            p.push_back(c);
            // add this line
            c=0;
        }
    

    Also, in add you probably want:

    // compensate size differences
    while (a.size() > b.size())
        b.push_back(0);
    while (b.size() > a.size())
        a.push_back(0);
    

    As-is, you’ll only be pushing a number of zeroes equal to (I believe) half of the initial difference between the arrays. Try an interactive debugger (as James suggests) and you may turn up other bugs (though there’s also something to be said for running code as short as this by hand).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a utility to calculate π to a million digits after the decimal.
I am writing a template class which takes a floating-point-like type (float, double, decimal,
I'm writing an iPhone app which uses a keypad that includes a decimal point.
I'm writing a simple tool in Qt which reads data from two GPX (XML)
I was writing a function for conversion between Decimal and Binary base number systems
I am writing a JavaScript function that can perform decimal place manipulation on a
I am writing a class for handling decimal numbers, e.g. 123.456. I want one
I am writing a SQL Function that will take in a decimal and return
I am writing a simple method that will calculate the number of decimal places
While writing a test with a value that gets represented as a BigDecimal, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.