In: Computer Science
language is c++
I need to take a num and put it into an array one number at a time suck that the hundredths place is at 10^3, tens is 10^2 and so on
For milestone 1 you need to build a constructor that converts a int to a bigInt
You need to peel off each digit from the int and place it into the appropriate location in the array of int. To do this you need to use integer division and mod.
bigint(int) int digit, num = 128; digit = num % 10; // num mod 10 is 8 in this case. num = num / 10; // num div 10 is 12. digit = num % 10; // digit is now 2 num = num /10; // num is now 1 digit = num % 10; // digit is now 1 num = num /10; // num is now 0
So you can build a loop that goes until num == 0 and peel off each digit, putting them in the array.
//C++ program
#include<iostream>
using namespace std;
class bigInt{
private :
int arr[20];
int length;
public:
bigInt(int num){
length=0;
while(num){
arr[length++] = num%10;
num/=10;
}
}
void printInt(){
for(int
i=length-1;i>=0;i--)cout<<arr[i];
}
};
int main(){
int num=128;
bigInt b1(num);
b1.printInt();
cout<<"\n\n";
num=1234567;
bigInt b2(num);
b2.printInt();
return 0;
}