-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab07.cpp
More file actions
136 lines (113 loc) · 3.21 KB
/
lab07.cpp
File metadata and controls
136 lines (113 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//
// Name: Eric Lepki
// Date: 03/19/2017
// Class: CSCI 2380
// Semester: Spring 18
//
// Program Name: Lab 07
// Program Description: Counts digits in different bases
// For instance 232223 has 4 2's in base 10, 15 has 4 1's in base 2
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
/*
For the binary, think through an example
11 mod 2 = 1, divide by 2
5 mod 2 = 1, divide by 2
2 mod 2 = 0, divide by 2
1 mod 2 = 1, divide by 2
0
11 base 2 is 1011
For another base the process is similar
Take 14 base 3
14 mod 3 = 2, divide by 3
4 mod 3 = 1, divide by 3
1 mod 3 = 1, divide by 3
0
14 base 3 is 112
*/
#include<iostream>
using namespace std;
//This should return the number of 1's in the binary representation
//of the input number (in decimal)
int CountBinOnes(int dec_number)
{
//base case
if (dec_number == 0) {
return 0;
}
//recursive case
else if (dec_number % 2 == 1) {
//This adds one every time dec_number % 2 is equal to one
return 1 + CountBinOnes(dec_number/2);
}
else {
return CountBinOnes(dec_number / 2);
}
}
//This should return the number of 'digits' in the 'base' representation
//of the input number (in decimal)
//This only has to work for bases between 2-10 and digits 0-9
int CountBaseDigits(int dec_number, int base, int digit)
{
//base case
if (dec_number == 0) {
return 0;
}
//recursive case
else if (dec_number % base == digit) {
//This adds one every time the decimal_number % base is equal to
// what ever digit the user enters
return 1 + CountBaseDigits(dec_number / base, base, digit);
}
else {
return CountBaseDigits(dec_number / base, base, digit);
}
}
//This should display the number in the 'base' representation
void PrintBaseDigits(int dec_number, int base)
{
if (dec_number / base) {
PrintBaseDigits(dec_number / base, base);
}
//The putchar function takes an integer argument to write it to stdout
//The integer is converted to unsigned char and written
putchar(dec_number % base + '0');
}
//a shortcut function to do error checking and ensure we have an integer
void GetInput(int &input)
{
//get inputs
cin >> input;
while (!cin)
{
cin.clear();
cin.ignore(2000, '\n');
cout << "Not a legal number" << endl;
cout << "Again: ";
cin >> input;
}
}
//Main program
int main()
{
int input_dec; //the main number in decimal
int input_base; //the base
int input_digit; //the digit we're counting
//get the input number
cout << "Please enter a number > 0:";
GetInput(input_dec);
//get the base
cout << "Enter a base:";
GetInput(input_base);
//get the digit to find in the representation
cout << "Enter a digit:";
GetInput(input_digit);
//output the results
cout << endl << "Number of 1's in binary: " << CountBinOnes(input_dec) << endl;
cout << endl << "Number of " << input_digit << "'s in base " << input_base << ":";
cout << CountBaseDigits(input_dec, input_base, input_digit) << endl;
cout << "The number " << input_dec << " in base " << input_base << " is ";
PrintBaseDigits(input_dec, input_base);
cout << endl;
return 0;
}