-1

My assignment is about working with dynamic integers with instructions as follow: Programming Exercise 11 in Chapter 8 explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects and functions to set, retrieve, and print the values of objects. Write a program to test your class.

I am currently getting some issues with my addition function, the main issue im experiencing is that when the first two numbers of the integers add up to 10+, I cant figure out how to properly get it to print the 1. for example 805+805 is return 610

 largeIntegers.h
static const int SIZE = 25;
#ifndef H_largeIntegers
#include <string>
#define H_largeIntegers  

class largeIntegers{

    public:
    int a = 0;
    int b = 0;
    void setNum1(std::string num1);
    void setNum2(std::string num2);
    void setResults(int a ,int indexValue);
    int getNum1(int indexLocation);
    int getNum2(int indexLocation);
    int getResults(int indexLocation);
    void subtract();
    void multiply();
    void compare();
    void printLists();
    void add();
    largeIntegers(std::string num1 = "10", std::string num2 = "10");

    private:
    int *num1Ptr;
    int *num2Ptr;
    int *resultPtr;
    int bigNumber1[SIZE]{};
    int bigNumber2[SIZE]{};
    int result[SIZE]{};
    int x1 = 0;
    int x2 = 0;
};
#endif
 '

largeIntegers.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "largeIntegers.h"
using namespace std;

void largeIntegers::setNum1(string num1){
    x1 = num1.size();
    int x = num1.size(), a = 0;
    num1Ptr = new int[SIZE];
    while (0 <= x) {
        num1Ptr[x-1] = num1[a] - '0';
        x--;
        a++;
    } 
}
void largeIntegers::setNum2(string num2){
   x2 = num2.size();
   int x = num2.size(), a = 0;
   num2Ptr = new int[SIZE];
   while (0 <= x) {
        num2Ptr[x-1] = num2[a] - '0';
        x--;
        a++;
    }
}
void largeIntegers::setResults(int a, int indexValue){
  resultPtr[a] = indexValue;
}
int largeIntegers::getNum1(int indexLocation){
  return num1Ptr[indexLocation];
}
int largeIntegers::getNum2(int indexLocation){
  return num2Ptr[indexLocation];
}
int largeIntegers::getResults(int indexLocation){
  return resultPtr[indexLocation];
}
void largeIntegers::add(){
int a = 0;
int result = 0;
int x = x1;
int y = x2;
resultPtr = new int[SIZE];
int indexSize = 0;
int longest = 0;
if (x > y) {
  longest = x;
}else {
  longest = y;
}   
    while (a < longest){
        result = num1Ptr[a] + num2Ptr[a]; 
        if(result>=10){
          result = result % 10;
          resultPtr[a-1] += 1;
          resultPtr[a] = result;
          cout << "This is result " << endl;
        }else{
          resultPtr[a] = result;
    }
       setResults(a, result);
       a++;
       indexSize++;
  }
    for (int a = indexSize - 1; a >= 0; a--) {
    cout << resultPtr[a];
    }
    cout << " " << endl;
}
void largeIntegers::subtract(){
}
void largeIntegers::multiply(){
};
void largeIntegers::compare(){
};

void largeIntegers::printLists(){
  for (int a = x1 - 1; a >= 0; a--) {
    cout << num1Ptr[a];
    }
    cout << " " << endl;
  for (int b = x2 - 1; b >= 0; b--) {
    cout << num2Ptr[b];
    }
    cout << " " << endl;
}
largeIntegers::largeIntegers(string num1, string num2){
  setNum1(num1);
  setNum2(num2);
}

main.cpp
#include <iostream>
#include "largeIntegers.h"
using namespace std;
int main(){
  int indexLocation =0;
  string num1 = " ";
  string num2 = " ";

  cout<< "Enter the first integer" << endl;
  cin >> num1;
  cout<< "Enter the second integer" << endl;
  cin >> num2;
  largeIntegers customList(num1, num2);

  cout << "The numbers entered are "<< endl;
  customList.printLists();
  cout << "When added together they equal " << endl;
  customList.add();
  cout << "The bigger integer minus the shorter integer is " << endl;
  customList.subtract();


  return 0;
}
Tyler
  • 1
  • 2
  • 3
    The include guards are very strange. `#ifndef H_largeIntegers` and `#define H_largeIntegers` should be the first two lines of the file. Nothing before or in-between them. – Retired Ninja Jun 19 '22 at 22:38
  • Thank you for informing me @RetiredNinja I have updated that in my code – Tyler Jun 19 '22 at 22:42
  • 1
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jun 19 '22 at 22:47
  • @SamVarshavchik This is on cengage, so its debugging isnt super detailed. When I run the code everything runs and no errors are found, it just printing out the 1 into a index prior to the first number i cant figure out how to do – Tyler Jun 19 '22 at 22:49
  • @Tyler What's the reason for using any pointers in the code? All you need are arrays that are `SIZE` in length (according to the requirements). Also, what happens if you add two numbers of `SIZE` length? What do you do with the overflow? Also, as stated in the answer given to you, each class should represent one value. Then the `add()` would be `add(const largeInteger& li)`, where you are adding the value of `li` to the current value in `this`. That makes a *lot* more sense than what you have now. – PaulMcKenzie Jun 20 '22 at 01:28
  • You increased the size of numbers from 20 to 25. That isn't really dynamic. Shouldn't your large integers be `std::vector`? – Goswin von Brederlow Jun 20 '22 at 03:48
  • You should really make a class to hold a single large integer and define a constructor taking a string, operator +, operator -, operator <<, operator >>. – Goswin von Brederlow Jun 20 '22 at 03:51

1 Answers1

0

You are storing your digits in reverse order, right? So you have the one's digit first. But look at your code for handling carry:

        if(result>=10){
          result = result % 10;
          resultPtr[a-1] += 1;
          resultPtr[a] = result;
          cout << "This is result " << endl;
        }else{

If you get a carry, you are bumping the PREVIOUS digit, not the NEXT digit. That should be

         resultPtr[a+1] ++;

So when you do 805 + 805 = 610, that 1 is coming from the 8+8, not from the 5+5. Better testing would have told you that.

The next issue is that, when you store a carry, you are not including the carry in the next digit cycle. And finally, when you have a carry out of the high-order digit, you are not bumping the number of digits, so you don't print the 1 you just added. This function works:

void largeIntegers::add(){
    int a = 0;
    int result = 0;
    int x = x1;
    int y = x2;
    resultPtr = new int[SIZE];
    int longest = x > y ? x : y;
    int carry = 0;
    while (a < longest){
        result = carry + num1Ptr[a] + num2Ptr[a]; 
        resultPtr[a] = result % 10;
        carry = result / 10;
        a++;
    }
    if( carry )
        resultPtr[a++] = 1;
    while( a-- )
        cout << resultPtr[a];
    cout << " " << endl;
}

And, by the way, this is absolutely the wrong way to organize this code. You should not have one class with two numbers. What if you want to add three numbers, as you will eventually have to do?

Each largeInteger object should hold ONE number. The addition and subtraction operators, then, know how to add or subtract another largeInteger object, not just handle the things inside the class.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • This correction did not work, it changed my result from 610 to 600. the -1 is putting into the index to the left not right. When adding 5+5 I want the 1 to the left other wise i would return 01. It works fine for everything EXCEPT if the first digits sum is 10+ – Tyler Jun 19 '22 at 23:00
  • No, you are wrong. On the FIRST digit, `a=0`. You'll do 5+5, you'll store the 0 into element 0, then you try to store the carry into element `-1`. The problem now is that you are overwriting the carry when you do the next digit, and you are not extending the number when you have a carry out. I'll post a fix shortly. – Tim Roberts Jun 19 '22 at 23:19
  • Oh alright, that makes more sense, I thought I would be able to create a new index to throw in before to store it. Thank you! – Tyler Jun 19 '22 at 23:36