1

I want to create a program which can handle integers greater than what C++ int can store.

I want to first store the integer that the user inputs in a integer array such that each digit of the input number is stored in each array slot,say, 968, it should be stored finally in the array such that arr[0] is 9, arr[1] is 6, arr[2] is 8. Instead, this time user will be inputting a very huge number with say, 1000 digits in it and I have to somehow get it to be stored in an integer array, with each digit of the number in each array element as cited above. So can someone please explain the usage of big int library?

Community
  • 1
  • 1
powersource97
  • 373
  • 5
  • 12

2 Answers2

2

You can just use a std::string object.

Each character will be between '0' and '9' and you can use that to do your computations.

For example to compute the sum of the first two digit of two numbers you can do

int sum = (a[0] - '0') + (b[0] - '0');

Hint: it's a bit easier to write the computation algorithms if you keep in num[0] the least significat digit (i.e. what is normally written to the right-most place).

6502
  • 112,025
  • 15
  • 165
  • 265
0

Instead, this time user will be inputting a very huge number with say, 1000 digits in it and I have to somehow get it to be stored in an integer array, with each digit of the number in each array element as cited above.

Answer: Allocate dynamic memory.

int variableSize;
char* theNumber;
std::cout << "What is your preferred size? ";
std::cin >> variableSize; /* do some error checking if necessary*/
int* myNumber = new int[variableSize];
std::cout << "Input the number now: ";
std::cin >> theNumber;
/* use a for loop to put each char into its respective array location;
do not forget to convert char to int... */

Prefer to use an std::vector (the preferred way to store dynamic memory)? Here is that approach:

int variableSize;
char* theNumber;
std::cout << "What is your preferred size? ";
std::cin >> variableSize; /* do some error checking if necessary*/
std::vector<int> myNumber(variableSize);
std::cout << "Input the number now: ";
std::cin >> theNumber;
/* use a for loop to put each char into its respective array location;
do not forget to convert char to int... */
Don Larynx
  • 685
  • 5
  • 15
  • Eww. That is not the answer. The answer is to use a `std::string` or `std::vector` and avoid doing manual memory operations – NathanOliver Aug 12 '15 at 13:19
  • @NathanOliver He wants an array, not a `std::vector`. Read the specs. – Don Larynx Aug 12 '15 at 13:20
  • @Don there is a flaw here. The number is very large and cannot be stored in the `int theNumber` in the first place. – powersource97 Aug 12 '15 at 13:21
  • And we should guide him into a better approach. If they asked if they what blade to use to cut off their hand to stop it from itching we shouldn't recommend a blade but instead point them to anti itch powder. – NathanOliver Aug 12 '15 at 13:22
  • Fixed. @NathanOliver I will edit my answer for this alternate approach. – Don Larynx Aug 12 '15 at 13:24