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 6806351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:42:00+00:00 2026-05-26T19:42:00+00:00

I’m attempting to make a class that holds a (in theory) infinite amount of

  • 0

I’m attempting to make a class that holds a (in theory) infinite amount of digits using c-strings, since ints have a limit. I am having a lot of trouble with multiplication and have begun to confuse myself. I am a student, so any help and mistakes I have not caught are very appreciated. I’ve been playing around with just the multiplication for 4 hours now.

The current problem is that I am calculating the results right (like if you try x = 12 and y = 4, I will only get 8 from it, then the final answer becomes 7 which is odd) but it isn’t storing properly.

definition:

MyInt operator* (const MyInt& x, const MyInt& y)
{
  MyInt steps[x.numDigits - 1]; // Create an array of MyInts

  int carry = 0;           // Holds the 'carry the one' 
  int result, xInt, yInt;  // For adding old-school


  // Add each digit separately.
  for (int i = 0; i < x.numDigits; i++)
  {
    steps[i].numDigits = y.numDigits;     // set the numDigits to y's

    // Resize the array to the size of numDigits
    steps[i].Resize(steps[i].numDigits);

    cout << "x.numDigits = " << x.numDigits << '\n';     // DELETE THESE
    cout << "numDigits = " << steps[i].numDigits << '\n';

    // Figure out xInt's value
    xInt = C2I(x.myNumber[x.numDigits - i - 1]);

    // Now multiply xInt by each digit of y
    for (int j = 1; j <= y.numDigits; j++)
    {
      // yInt's value for this run through
      yInt = C2I(y.myNumber[y.numDigits - j]);

      // Answer is xInt * yInt + the remainder
      result = ((xInt * yInt) + carry);
      carry = 0;               // Reset carry to zero

      // If the result is 10 or higher,  carry the excess
      if (result > 9)
      {
    carry = result / 10;
    result = result % 10;
      }

      // Assign result to the appropriate slot in the new number
      steps[i].myNumber[(steps[i].numDigits - j)] = I2C(result);

      cout << "ASSIGNED " << steps[i].myNumber[steps[i].numDigits - j] //DELETE THESE 
       << " TO SLOT " << (steps[i].numDigits - j)
       << " with a rem = " << carry << '\n';
    }
    cout << "YOU GOT OUT OF THE J FOR LOOP\n";
    // If carry wasn't reset to 0, that means the loop ended.
    // This means there is a # that cannot fit in the current
    // array size. We must resize, and then assign
    // the extra characters into the array.
    if (carry > 0)
    {
      int carryCopy = carry; // Copy of n for counting numDigits
      int carryCount = 0;    // Counts up how many digits are in carry

      while(carryCopy > 0)   // Figure out how many #'s there are
      {
    carryCopy = carryCopy / 10;
    carryCount++;
      }

      // Figure out the new size
      steps[i].numDigits = steps[i].numDigits + carryCount;

      // Resize to new size
      steps[i].Resize(steps[i].numDigits + carryCount);

      // Copy in the new digits
      for (int k = carryCount-1; k >= 0; k--)
      {
    steps[i].myNumber[k] = I2C(carry % 10);
    carry = carry / 10;
      }
    }
  }
  cout << "What you have so far is " << steps[0] << "\n"; // DELETE
  cout << "YOU GOT TO THE ADDING PART\n";                 // DELETE

  MyInt r = 0; // Create MyInt for total result

  // Add up all of the arrays in steps[] into r 
  for (int l = 0; l < x.numDigits - 1; l++)
    r = r + steps[l];

  return r;                   // Result
}

Header file

#include <iostream>// for ostream, istream
using namespace std;

class MyInt
{
   // these overload starters are declared as friend functions

   friend MyInt operator+ (const MyInt& x, const MyInt& y);
   friend MyInt operator* (const MyInt& x, const MyInt& y);

   friend bool operator< (const MyInt& x, const MyInt& y);
   friend bool operator> (const MyInt& x, const MyInt& y);
   friend bool operator<= (const MyInt& x, const MyInt& y);
   friend bool operator>= (const MyInt& x, const MyInt& y);
   friend bool operator== (const MyInt& x, const MyInt& y);
   friend bool operator!= (const MyInt& x, const MyInt& y);

   friend ostream& operator<< (ostream& s, const MyInt& n);
   friend istream& operator>> (istream& s, MyInt& n);

public:
   MyInt(int n = 0);        // first constructor
   MyInt(const char * n);       // second constructor
   ~MyInt();                    // Destructor

   MyInt(const MyInt & n);      // Copy Constructor
   MyInt& operator= (const MyInt & n); // Assignment operator

   // be sure to add in the second constructor, and the user-defined 
   //  versions of destructor, copy constructor, and assignment operator

private:

   // member data (suggested:  use a dynamic array to store the digits)
   unsigned int numDigits;  // The number of digits in myInt
   char * myNumber;             // Pointer to dynamic array of digits
   void Resize(unsigned int newSize); // Resize array
};

and the main program I am using to test:

int main()
{
  // demonstrate behavior of the two constructors and the << overload

  MyInt x(12345), y("9876543210123456789"), r1(-1000), r2 = "14H67", r3;
  char answer;
  cout << "Initial values: \nx = " << x << "\ny = " << y
       << "\nr1 = " << r1 << "\nr2 = " << r2 << "\nr3 = " << r3 << "\n\n";

  // demonstrate >> overload

  cout << "Enter first number: ";
  cin >> x;
  cout << "Enter second number: ";
  cin >> y;

  cout << "You entered:\n";
  cout << "  x = " << x << '\n';
  cout << "  y = " << y << '\n';

  // demonstrate assignment =
  cout << "Assigning r1 = y ...\n";
  r1 = y;
  cout << "  r1 = " << r1 << '\n';

  // demonstrate comparison overloads
  if (x < y)    cout << "(x < y) is TRUE\n";
  if (x > y)    cout << "(x > y) is TRUE\n";
  if (x <= y)   cout << "(x <= y) is TRUE\n";
  if (x >= y)   cout << "(x >= y) is TRUE\n";
  if (x == y)   cout << "(x == y) is TRUE\n";
  if (x != y)   cout << "(x != y) is TRUE\n";

  // demonstrating + and * overloads
  r1 = x + y;
  cout << "The sum (x + y) = " << r1 << '\n';
  r2 = x * y;
  cout << "The product (x * y) = " << r2 << "\n\n";
  cout << "The sum (x + 12345) = " << x + 12345 << '\n';
  cout << "The product (y * 98765) = " << y * 98765 << '\n';
}
  • 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-26T19:42:01+00:00Added an answer on May 26, 2026 at 7:42 pm

    You’re doing all the multiplication first, then the adding. This is inefficient and makes your code more complicated.

    Instead, you should add the results together as you compute each multiply stage.

    For example, instead of:

    A = 123
    B = 456
    S[0] = A * (B[2] * 10^2) = 123 * 400 = 49200
    S[1] = A * (B[1] * 10^1) = 123 * 50 = 6150
    S[2] = A * (B[0] * 10^0) = 123 * 6 = 738
    R = S[0] + S[1] + S[2] = 49200 + 6150 + 738 = 56088
    

    do this:

    A = 123
    B = 456
    R = 0
    R += A * (B[2] * 10^2) = 0 + 123 * 400 = 49200
    R += A * (B[1] * 10^1) = 49200 + 123 * 50 = 55350
    R += A * (B[0] * 10^0) = 55350 + 123 * 6 = 56088
    

    This gets rid of the need for the steps array (S in my example).

    Also, consider storing the digits in your array least-significant-digit first. This allows you to replace the * 10^N steps with index shifts.

    A = 123
    B = 456
    R = 0
    R[0:] += A * B[0] = 123 * 6 = 738()
    R[1:] += A * B[1] = 123 * 5 + 73 = 688(8)
    R[2:] += A * B[2] = 123 * 4 + 68 = 560(88)
    

    Where the () part is the digits shifted past by the indexing of R. This technique also makes it easier to add or remove most-significant-digits since they’re at the end of the array and not the beginning.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.