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