Skip to content

Commit 9cc53f8

Browse files
committed
Added length calculation
1 parent a87c6b2 commit 9cc53f8

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

For loop/53. Binary number to octal.c

+33-26
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ Convert every 3 binary digits (start from bit 0) to 1 octal digit, with binary t
1313
Ex: 00001100010001 = 00 001 100 010 001 = 1 4 2 1 = 1421
1414
+ 2. Input a dinamic array of chars
1515
3. Convertion:
16-
Iterate over an array, assign each pair of three from the end of the array octal value
16+
Return the lenght from the array input
17+
Flip the array
18+
Iterate over an array by three elements at the time, assign each pair of three from the end of the array octal value
1719
Output the value
1820
4. Invalid input
1921
5. Test
22+
Check if all the memory was freed
2023
6. Cpplint test
2124
7. Add and push
2225
*/
@@ -29,43 +32,46 @@ enum is_valid{
2932
FALSE = 0
3033
};
3134

32-
int input_binary(char *pointer_to_binary_array);
35+
int input_binary(int *array_lenght, char *pointer_to_binary_array);
3336
void print_invalid_input();
3437
void print_binary_array(char *pointer_to_binary_array);
35-
void convert_to_octal(char *pointer_to_octal_array, char *pointer_to_binary_array);
38+
// void convert_to_octal(char *pointer_to_octal_array, char *pointer_to_binary_array);
3639

3740
int main() {
3841
char *pointer_binary;
42+
int array_lenght = 0;
3943
pointer_binary = (char*)malloc(1 * sizeof(char));
4044
if (pointer_binary == NULL) {
4145
printf("Memory could not be allocated");
4246
} else {
43-
int is_valid = input_binary(pointer_binary);
44-
// print_binary_array(pointer_binary);
47+
int is_valid = input_binary(&array_lenght, pointer_binary);
48+
print_binary_array(pointer_binary);
49+
printf("\n%d\n", array_lenght - 1);
4550
if (is_valid == FALSE) {
4651
print_invalid_input();
47-
} else {
48-
char *pointer_octal;
49-
pointer_octal = (char*)malloc(1 * sizeof(char));
50-
convert_to_octal(pointer_octal, pointer_binary);
51-
}
52+
}
53+
// else {
54+
// char *pointer_octal;
55+
// pointer_octal = (char*)malloc(1 * sizeof(char));
56+
// convert_to_octal(pointer_octal, pointer_binary);
57+
// }
5258
}
5359
return 0;
5460
}
5561

56-
void convert_to_octal(char *pointer_to_octal_array, char *pointer_to_binary_array) {
57-
int array_lenght = 1, one_step = 3, index = 0, biggest_binary = 8, temp_octal = 0, binary_divisor = 2;
58-
char current_element = pointer_to_binary_array[index];
59-
while(current_element != '\0') {
60-
for (int i = 0; i <= 3; ++i) {
61-
if (current_element == 1) {
62-
temp_octal += biggest_binary / binary_divisor;
62+
// void convert_to_octal(char *pointer_to_octal_array, char *pointer_to_binary_array) {
63+
// int array_lenght = 1, one_step = 3, index = 0, biggest_binary = 8, temp_octal = 0, binary_divisor = 2;
64+
// char current_element = pointer_to_binary_array[index];
65+
// while(current_element != '\0') {
66+
// for (int i = 0; i <= 3; ++i) {
67+
// if (current_element == 1) {
68+
// temp_octal += biggest_binary / binary_divisor;
6369

64-
}
65-
}
66-
}
70+
// }
71+
// }
72+
// }
6773

68-
}
74+
// }
6975

7076
void print_binary_array(char *pointer_to_binary_array) {
7177
for (int i = 0; ;++i) {
@@ -80,10 +86,11 @@ void print_invalid_input() {
8086
printf("n/a");
8187
}
8288

83-
int input_binary(char *pointer_to_binary_array) {
84-
int array_lenght = 1, is_valid = TRUE;
89+
int input_binary(int *array_lenght, char *pointer_to_binary_array) {
90+
int is_valid = TRUE;
91+
*array_lenght = 1;
8592
char endline = '\n', temp_char = '\0';
86-
for (int i = 0; i < array_lenght; ++i) {
93+
for (int i = 0; i < *array_lenght; ++i) {
8794
if(!scanf("%c", &temp_char)) {
8895
if (pointer_to_binary_array) {
8996
free(pointer_to_binary_array);
@@ -103,8 +110,8 @@ int input_binary(char *pointer_to_binary_array) {
103110
}
104111
}
105112
pointer_to_binary_array[i] = temp_char;
106-
++array_lenght;
107-
pointer_to_binary_array = (char*)realloc(pointer_to_binary_array, (array_lenght)*sizeof(char));
113+
++*array_lenght;
114+
pointer_to_binary_array = (char*)realloc(pointer_to_binary_array, (*array_lenght)*sizeof(char));
108115
}
109116
}
110117
return is_valid;

0 commit comments

Comments
 (0)