@@ -24,7 +24,7 @@ The Octal of 79 is 117.
24
24
+ Function to count how many iterations of /8 there will be
25
25
+ Power function
26
26
+ Function to get the number int
27
- Reverse it
27
+ + Reverse it
28
28
5. Output
29
29
6. Test
30
30
7. Cpplint test
@@ -37,25 +37,44 @@ int input_decimal_number();
37
37
int number_of_eights (int decimal_number );
38
38
int power (int base , int exponent );
39
39
int convert (int decimal_number , int division_by_eight );
40
+ int reverse_number (int converted_number , int division_by_eight );
40
41
41
42
int main () {
42
43
int decimal_number = input_decimal_number ();
43
44
int division_by_eight = number_of_eights (decimal_number );
44
45
int converted = convert (decimal_number , division_by_eight );
46
+ int converted_and_flipped = reverse_number (converted , division_by_eight );
45
47
printf ("Division by eight = %d\n" , division_by_eight );
46
- printf ("Converted but not flipped = %d" , converted );
48
+ printf ("Converted but not flipped = %d, flipped = %d\n " , converted , converted_and_flipped );
47
49
return 0 ;
48
50
}
49
- // Conversion steps:
50
- // 1 Divide the number by 8.
51
- // 2 Get the integer quotient for the next iteration.
52
- // 3 Get the remainder for the octal digit.
53
- // 4 Repeat the steps until the quotient is equal to 0.
54
- // 79 % 8 == 7; quotient = 9
55
- // 9 % 8 == 1; q = 1
56
- // 1 % 8 == 1; q = 1
57
- // I will probably need a function to count how many iterations I will need to convert result into a number.
58
- // I'll need that function once again when I will flip the number
51
+
52
+ // 711 -> 117
53
+
54
+ int reverse_number (int converted_number , int division_by_eight ) {
55
+ int result = 0 , temp_converted = converted_number , digit , power_counter = division_by_eight - 1 ;
56
+ for (int i = 0 ; i <= division_by_eight ; ++ i ) {
57
+ digit = temp_converted % 10 ;
58
+ temp_converted /= 10 ;
59
+ result += digit * power (10 , power_counter );
60
+ -- power_counter ;
61
+ }
62
+ return result ;
63
+ }
64
+
65
+ /*
66
+ Conversion steps:
67
+ 1 Divide the number by 8.
68
+ 2 Get the integer quotient for the next iteration.
69
+ 3 Get the remainder for the octal digit.
70
+ 4 Repeat the steps until the quotient is equal to 0.
71
+ 79 % 8 == 7; quotient = 9
72
+ 9 % 8 == 1; q = 1
73
+ 1 % 8 == 1; q = 1
74
+ I will probably need a function to count how many iterations I will need to convert result into a number.
75
+ I'll need that function once again when I will flip the number
76
+ */
77
+
59
78
int convert (int decimal_number , int division_by_eight ) {
60
79
int result = 0 , quotient , remainder , temp_decimal = decimal_number , divider = 8 , power_counter = division_by_eight ;
61
80
for (int i = 0 ; i <= division_by_eight ; ++ i ) {
0 commit comments