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

USE C++ For this assignment, you will use vectors and its associated iterators.

ID: 3776195 • Letter: U

Question

USE C++

For this assignment, you will use vectors and its associated iterators. Suppose you were asked to print out (in decimal form) the value 2^1000. How would you do that? You can’t calculate the value, even using the data type “unsigned long long int” because that uses at most 64 bits, not 1000 bits. You would need to create a class that can handle these huge numbers. For this assignment create a class called LongDecimalInt. It will use a vector of integers to represent decimal integers (natural numbers so zero and positive integerss only). The vector will hold the decimal digits in reverse order, that is, a vector {1, 9, 2, 0, 5} will represent the number 50291. The content of LongDecimalInt.h is given below. First implement the constructor and the print() function and test them. The print function should use the const_reverse_iterator. Then implement doubleValue() which takes the number and doubles its value. In your test program, you can use it to calculate and print 2^10 (should be 1024), 2^20 (should be 1048576), and 2^1000. Approximately how may digits would you need to check to see that you got the right answer? For something like this, you can use a checksum function, which does not guarantee that the answer is correct, but is a reasonably good assurance that it is. For the checksum function, try adding the digits in the number and returning the sum. Implement it as addDigits() in the LongDecimalInt class. For 2^10, it should return 7; for 2^20, it should return 31; and for 2^1000, it should return 1366. Implement addition and multiplication.

The purpose of this assignment is to display the large number such as 2^1000

#ifndef LONG_DECIMAL_INT_H

#define LONG_DECIMAL_INT_H

#include #include

#include using namespace std;

class LongDecimalInt { public: LongDecimalInt ( unsigned long long int n = 0);

void doubleValue();

void print();

void addDigits();

LongDecimalInt operator+(const LongDecimalInt right);

private: vector value;

}; #endif

Explanation / Answer

PROGRAM CODE:


#include <iostream>
#include <vector>
#include <string>
#include "LongDecimalInt.h"
using namespace std;

LongDecimalInt::LongDecimalInt ( unsigned long long int n)
{
   string number = to_string(n);
   int length = number.length();
   int counter = length-1;
   value.resize(length);
   std::vector<int>::iterator rit = value.begin();
   for (; rit!= value.end() || counter>=0; ++rit)
   {
       char c = number.at(counter);
       int digit = c - '0';
       *rit = digit;
       counter--;
   }
}

void LongDecimalInt::doubleValue()
{
   std::vector<int>::iterator rit = value.begin();
   for (; rit!= value.end(); ++rit)
   {
       int i = *rit;
       *rit = i*2;
   }
}

void LongDecimalInt::addDigits()
{
   int total = 0;
   std::vector<int>::iterator rit = value.begin();
   for (; rit!= value.end(); ++rit)
   {
       total += *rit;
       cout<<*rit<<" ";
   }
   cout<<endl<<"total:"<<total<<endl;
}

void LongDecimalInt:: print()
{
   std::vector<int>::reverse_iterator rit = value.rbegin();
   for (; rit!= value.rend(); ++rit)
   {
       cout<<*rit;
   }
   cout<<endl;
}
int main() {
   LongDecimalInt decimal(2234);
   decimal.print();
   decimal.doubleValue();
   decimal.print();
   decimal.addDigits();
   return 0;
}

OUTPUT: