Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you complete this code without adding any header file? Explain your code p

ID: 3597920 • Letter: C

Question

Could you complete this code without adding any header file?

Explain your code please(Like leave a comment on each loop and statement)

----------------------------------------------------------------------------------------------------------

#include <iostream>

#include <string>

using namespace std;

class BigInt {

private:

int* arr;

int size;

public:

// constructor with size and pointer to an array

BigInt(int s, int* a) {

size = s;

arr = a;

}

// this constructor should convert str into an array of ints,

// storing the values on the RIGHT SIDE of the array :)

BigInt(string str) {

size = str.length();

arr = new int [size];

for(int i = 0; i < size; i++) {

arr[i] = str[i] - '0';

}

}

// copy constructor

BigInt(const BigInt& other) {

size = other.getSize();

arr = new int [size];

for(int i = 0; i < size; i++) {

arr[i] = other[i];

}

}

// get the size of the array

int getSize() const {

return size;

}

// this function gets the digit at 'index' from arr

// and returns 0's if 'index' is negative

int operator [] (int index) const {

if(index >= 0) {

return arr[index];

} else {

return 0;

}

}

// this function should add the argument "num"

// to the private array and return the sum

BigInt operator + (BigInt num) {

}

// this function should subtract the argument "num"

// from the private array and return the difference

BigInt operator - (BigInt num) {

}

// this function will print the BigInt

void print() {

for(int i = 0; i < size; i++) {

cout << arr[i];

}

cout << endl;

}

};

int main() {

string str1, str2;

cout << "num 1: ";

cin >> str1;

cout << "num 2: ";

cin >> str2;

BigInt num1(str1);

BigInt num2(str2);

cout << endl;

cout << "num 1: ";

num1.print();

cout << "num 2: ";

num2.print();

cout << endl;

cout << "sum: ";

BigInt sum = num1 + num2;

sum.print();

cout << "difference: ";

BigInt diff = num1 - num2;

diff.print();

// pause and exit

getchar();

return 0;

}

Explanation / Answer

the modified code is as follows :

--------------------------------------------------------------------------------------------

#include <iostream>
#include <string>

using namespace std;

class BigInt {

private:

int* arr;
int size;

public:

// constructor with size and pointer to an array

BigInt(int s, int* a) {
    size = s;
    arr = a;
}

// this constructor should convert str into an array of ints,
// storing the values on the RIGHT SIDE of the array :)

BigInt(string str) {
    size = str.length();
    arr = new int [size];
    for(int i = 0; i < size; i++) {
      arr[i] = str[i] - '0';
    }
}
// copy constructor
BigInt(const BigInt& other) {
    size = other.getSize();
    arr = new int [size];
    for(int i = 0; i < size; i++) {
      arr[i] = other[i];
    }
}

// get the size of the array

int getSize() const {
    return size;
}

// this function gets the digit at 'index' from arr
// and returns 0's if 'index' is negative

int operator [] (int index) const {
    if(index >= 0) {
      return arr[index];
    } else {
      return 0;
    }
}

// this function should add the argument "num"
// to the private array and return the sum

BigInt operator + (BigInt num) {
    /* defining an array of size equal to size of num */
    int sum_arr[num.getSize()];
    /* looping over the num to add those values to num and assign it to defined array */
    for(int i =0;i < num.getSize();i++){
      sum_arr[i] = this->arr[i] + num[i];
    }
    /* creating object reference to BigInt with result array and returning it */
    BigInt sum(num.getSize(), sum_arr);
    return sum;
}

// this function should subtract the argument "num"
// from the private array and return the difference

BigInt operator - (BigInt num) {
    /* defining arrya of size equal to size of num */
    int sub_arr[num.getSize()];
    /* looping over the num to subtract those values from num and assign to defined array */
    for(int i =0;i < num.getSize();i++){
      sub_arr[i] = this->arr[i] - num[i];
    }
    /* creating object reference to BigInt with result array and returning it */
    BigInt sub(num.getSize(), sub_arr);
    return sub;
}

// this function will print the BigInt

void print() {
    for(int i = 0; i < size; i++) {
      cout << arr[i];
    }
    cout << endl;
}
};

/* main function definition */

int main() {
char n;
string str1, str2;
cout << "num 1: ";
cin >> str1;
cout << "num 2: ";
cin >> str2;
BigInt num1(str1);
BigInt num2(str2);
cout << endl;
cout << "num 1: ";
num1.print();
cout << "num 2: ";
num2.print();
cout << endl;
cout << "sum: ";
BigInt sum = num1 + num2;
sum.print();
cout << "difference: ";
BigInt diff = num1 - num2;
diff.print();
// pause and exit
std :: cin >>n;
return 0;
}

----------------------------------------------------------------------------------------------

/* hope this helps */

/* if any queries please comment */

/* thank you */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote