Skip to content

Commit a95924b

Browse files
committed
Finished with the convert func but need to flip the number now
1 parent f06711c commit a95924b

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

For loop/50. Decimal to octal.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The Octal of 79 is 117.
2323
4. Convert:
2424
+ Function to count how many iterations of /8 there will be
2525
+ Power function
26-
Function to get the number int
26+
+ Function to get the number int
2727
Reverse it
2828
5. Output
2929
6. Test
@@ -36,25 +36,40 @@ The Octal of 79 is 117.
3636
int input_decimal_number();
3737
int number_of_eights(int decimal_number);
3838
int power(int base, int exponent);
39-
int convert(int decimal_number);
39+
int convert(int decimal_number, int division_by_eight);
4040

4141
int main() {
4242
int decimal_number = input_decimal_number();
4343
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);
4747
return 0;
4848
}
4949
// Conversion steps:
5050
// 1 Divide the number by 8.
5151
// 2 Get the integer quotient for the next iteration.
5252
// 3 Get the remainder for the octal digit.
5353
// 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
5457
// I will probably need a function to count how many iterations I will need to convert result into a number.
5558
// 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;
5873
}
5974

6075
int power(int base, int exponent) {

0 commit comments

Comments
 (0)