Advertisement
TheWhiteFang

Tutorial 3 Section B [Base10toBase5]

Dec 16th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. //Tutorial 3 Section B [base10tobase5]
  3. //the answer for base10(12345) is base5(343340)
  4. //i dunno why the tutorial answer is 050664 -.-'
  5. #include <iostream>
  6. using namespace std;
  7. int numDigits(int number) //function to check how many digits
  8. {
  9.     int digits = 0;
  10.     if (number < 0) digits = 1;
  11.     while (number) {
  12.         number /= 10;
  13.         digits++;
  14.     }
  15.  
  16.    
  17.     return digits;
  18. }
  19.  
  20. void base10toBase5(int base10, int base5[], int arraySize){
  21.  
  22.  
  23.     for (int i = 0; i <= arraySize; i++){
  24.  
  25.         base5[i] = int(base10 % 5);
  26.         base10 = base10 / 5;
  27.  
  28.        
  29.     }
  30.  
  31.     for (int i = arraySize; i >= 0; i--){
  32.    
  33.         cout << base5[i];
  34.     }
  35. }
  36.  
  37. int main(){
  38.  
  39.     int input = 0; int digit = 0; int *arr;
  40.    
  41.     cout << "Enter a 5 digit integer in base10 number: " ;
  42.     cin >> input;
  43.     digit = numDigits(input); //value is returned to digit
  44.  
  45.     if (digit <= 5){
  46.  
  47.         arr = new int[digit] ; //memory allocated
  48.         /*cout << "Correcto output " << digit << endl;*/ //digit checker
  49.        
  50.         base10toBase5(input, arr, digit);
  51.     }
  52.  
  53.     cout << endl;
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement