Advertisement
Checosnake

Lab 07

Mar 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  2. // Name: Sergio A. Nuñez
  3. // Date: 3/28/16
  4. // Class: CSCI 2380
  5. // Semester: Spring 2016
  6. // Program Name: Lab 07
  7. // Program Description: Counts digits in different bases
  8. //  For instance 232223 has 4 2's in base 10, 15 has 4 1's in base 2
  9. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  10.  
  11. /*
  12. For the binary, think through an example
  13.  
  14. 11 mod 2 = 1, divide by 2
  15. 5 mod 2 = 1,  divide by 2
  16. 2 mod 2 = 0,  divide by 2
  17. 1 mod 2 = 1,  divide by 2
  18. 0
  19.  
  20. 11 base 2 is 1011
  21.  
  22. For another base the process is similar
  23. Take 14 base 3
  24.  
  25. 14 mod 3 = 2, divide by 3
  26. 4 mod 3 = 1,  divide by 3
  27. 1 mod 3 = 1,  divide by 3
  28. 0
  29.  
  30. 14 base 3 is 112
  31.  
  32. */
  33.  
  34.  
  35. #include<iostream>
  36. using namespace std;
  37.  
  38.  
  39.  
  40. //This should return the number of 1's in the binary representation
  41. //of the input number (in decimal)
  42. int CountBinOnes(int dec_number)
  43. {
  44.     //base case
  45.     if (dec_number == 0)
  46.     {
  47.         return (0 + (dec_number/2));
  48.     }
  49.     //recursive case
  50.     else
  51.     {
  52.         if (dec_number % 2 == 1)
  53.         {
  54.             return(1+CountBinOnes(dec_number/2));
  55.         }
  56.         else if (dec_number % 2 == 0)
  57.         {
  58.             return(0 + CountBinOnes(dec_number / 2));
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. //This should return the number of 'digits' in the 'base' representation
  65. //of the input number (in decimal)
  66. //This only has to work for bases between 2-10 and digits 0-9
  67. int CountBaseDigits(int dec_number, int base, int digit)
  68. {
  69.     //base case
  70.     if (dec_number == 0)
  71.     {
  72.         return (0 + (dec_number / base));
  73.     }
  74.     //recursive case
  75.     else
  76.     {
  77.         if (dec_number % base == digit)
  78.         {
  79.             return(1 + CountBaseDigits((dec_number / base), base, digit));
  80.         }
  81.         else
  82.         {
  83.             return(0 + CountBaseDigits((dec_number / base), base, digit));
  84.         }
  85.     }
  86. }
  87.  
  88. //This should display the number in the 'base' representation
  89. void PrintBaseDigits(int dec_number, int base)
  90. {
  91.     //base case
  92.     if (dec_number != 0)
  93.     {
  94.         PrintBaseDigits(dec_number / base, base);
  95.         cout << (dec_number % base);
  96.     }
  97. }
  98.  
  99. //a shortcut function to do error checking and ensure we have an integer
  100. void GetInput(int &input)
  101. {
  102.     //get inputs
  103.     cin >> input;
  104.     while (!cin)
  105.     {
  106.         cin.clear();
  107.         cin.ignore(2000, '\n');
  108.         cout << "Not a legal number" << endl;
  109.         cout << "Again: ";
  110.  
  111.         cin >> input;
  112.     }
  113. }
  114.  
  115.  
  116. //Main program
  117. int main()
  118. {
  119.     int input_dec; //the main number in decimal
  120.     int input_base; //the base
  121.     int input_digit; //the digit we're counting
  122.  
  123.                      //get the input number
  124.     cout << "Please enter a number > 0:";
  125.     GetInput(input_dec);
  126.     //get the base
  127.     cout << "Enter a base:";
  128.     GetInput(input_base);
  129.     //get the digit to find in the representation
  130.     cout << "Enter a digit:";
  131.     GetInput(input_digit);
  132.  
  133.     //output the results
  134.     cout << endl << "Number of 1's in binary:" << CountBinOnes(input_dec) << endl;
  135.     cout << endl << "Number of " << input_digit << "'s in base " << input_base << ":";
  136.     cout << CountBaseDigits(input_dec, input_base, input_digit) << endl;
  137.  
  138.     cout << "The number " << input_dec << " in base " << input_base << " is ";
  139.     PrintBaseDigits(input_dec, input_base);
  140.     cout << endl;
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement