@@ -13,10 +13,13 @@ Convert every 3 binary digits (start from bit 0) to 1 octal digit, with binary t
13
13
Ex: 00001100010001 = 00 001 100 010 001 = 1 4 2 1 = 1421
14
14
+ 2. Input a dinamic array of chars
15
15
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
17
19
Output the value
18
20
4. Invalid input
19
21
5. Test
22
+ Check if all the memory was freed
20
23
6. Cpplint test
21
24
7. Add and push
22
25
*/
@@ -29,43 +32,46 @@ enum is_valid{
29
32
FALSE = 0
30
33
};
31
34
32
- int input_binary (char * pointer_to_binary_array );
35
+ int input_binary (int * array_lenght , char * pointer_to_binary_array );
33
36
void print_invalid_input ();
34
37
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);
36
39
37
40
int main () {
38
41
char * pointer_binary ;
42
+ int array_lenght = 0 ;
39
43
pointer_binary = (char * )malloc (1 * sizeof (char ));
40
44
if (pointer_binary == NULL ) {
41
45
printf ("Memory could not be allocated" );
42
46
} 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 );
45
50
if (is_valid == FALSE) {
46
51
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
+ // }
52
58
}
53
59
return 0 ;
54
60
}
55
61
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;
63
69
64
- }
65
- }
66
- }
70
+ // }
71
+ // }
72
+ // }
67
73
68
- }
74
+ // }
69
75
70
76
void print_binary_array (char * pointer_to_binary_array ) {
71
77
for (int i = 0 ; ;++ i ) {
@@ -80,10 +86,11 @@ void print_invalid_input() {
80
86
printf ("n/a" );
81
87
}
82
88
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 ;
85
92
char endline = '\n' , temp_char = '\0' ;
86
- for (int i = 0 ; i < array_lenght ; ++ i ) {
93
+ for (int i = 0 ; i < * array_lenght ; ++ i ) {
87
94
if (!scanf ("%c" , & temp_char )) {
88
95
if (pointer_to_binary_array ) {
89
96
free (pointer_to_binary_array );
@@ -103,8 +110,8 @@ int input_binary(char *pointer_to_binary_array) {
103
110
}
104
111
}
105
112
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 ));
108
115
}
109
116
}
110
117
return is_valid ;
0 commit comments