Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://pastebin.com/u/TheWhiteFang
- //Tutorial 3 Section B [base10tobase5]
- //the answer for base10(12345) is base5(343340)
- //i dunno why the tutorial answer is 050664 -.-'
- #include <iostream>
- using namespace std;
- int numDigits(int number) //function to check how many digits
- {
- int digits = 0;
- if (number < 0) digits = 1;
- while (number) {
- number /= 10;
- digits++;
- }
- return digits;
- }
- void base10toBase5(int base10, int base5[], int arraySize){
- for (int i = 0; i <= arraySize; i++){
- base5[i] = int(base10 % 5);
- base10 = base10 / 5;
- }
- for (int i = arraySize; i >= 0; i--){
- cout << base5[i];
- }
- }
- int main(){
- int input = 0; int digit = 0; int *arr;
- cout << "Enter a 5 digit integer in base10 number: " ;
- cin >> input;
- digit = numDigits(input); //value is returned to digit
- if (digit <= 5){
- arr = new int[digit] ; //memory allocated
- /*cout << "Correcto output " << digit << endl;*/ //digit checker
- base10toBase5(input, arr, digit);
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement