-
-
Save satheesh-chandran/33b2d01cedb0caf83b34c893f6539bd5 to your computer and use it in GitHub Desktop.
#include <stdio.h> | |
#include <stdlib.h> | |
#include "seperation.h" | |
int main(void) | |
{ | |
int numbers[] = {3, 1, 7, 4, 6, 5, 8, 2}; | |
int start_limit = 4, end_limit = 7; | |
int size = sizeof(numbers) / sizeof(int); | |
TWO_DIMENSIONAL_ARRAY *result = separate(numbers, size, start_limit, end_limit); | |
print_two_dimensional_array(result); | |
free_memory(result); | |
return 0; | |
} |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "seperation.h" | |
ARRAY* initialize_array(int length) | |
{ | |
ARRAY *array = malloc(sizeof(ARRAY)); | |
(*array).count = 0; | |
array->list = malloc(sizeof(int) * length); | |
return array; | |
} | |
TWO_DIMENSIONAL_ARRAY* initialize_two_dimensional_array(int length, int deep_array_size) | |
{ | |
TWO_DIMENSIONAL_ARRAY *array = malloc(sizeof(TWO_DIMENSIONAL_ARRAY)); | |
(*array).count = 0; | |
array->seperated_numbers = malloc(sizeof(ARRAY) * length); | |
for (int index = 0; index < length; index++) | |
{ | |
array->seperated_numbers[index] = initialize_array(deep_array_size); | |
} | |
return array; | |
} | |
void print_array(ARRAY * array) | |
{ | |
for (int index = 0; index < (*array).count; index++) | |
{ | |
printf("%d ", array->list[index]); | |
} | |
printf("\n"); | |
} | |
void print_two_dimensional_array(TWO_DIMENSIONAL_ARRAY *result) | |
{ | |
for (int index = 0; index < (*result).count; index++) | |
{ | |
print_array(result->seperated_numbers[index]); | |
} | |
} | |
void free_array_memory(ARRAY *array) | |
{ | |
free(array->list); | |
free(array); | |
} | |
void free_memory(TWO_DIMENSIONAL_ARRAY *array) | |
{ | |
for (int index = 0; index < (*array).count; index++) | |
{ | |
free_array_memory(array->seperated_numbers[index]); | |
} | |
free(array->seperated_numbers); | |
free(array); | |
} | |
TWO_DIMENSIONAL_ARRAY *separate(int *numbers, int size, int start, int end) | |
{ | |
TWO_DIMENSIONAL_ARRAY *array = initialize_two_dimensional_array(3, size); | |
for (int index = 0; index < size; index++) | |
{ | |
int num = numbers[index], pos = 1; | |
if (num < start) pos = 0; | |
if (num > end) pos = 2; | |
int position_of_numbers = (*array->seperated_numbers[pos]).count; | |
ARRAY *number_group = array->seperated_numbers[pos]; | |
number_group->list[position_of_numbers] = num; | |
(*number_group).count += 1; | |
} | |
(*array).count = 3; | |
return array; | |
} |
typedef struct { | |
int count; | |
int *list; | |
} ARRAY; | |
typedef struct { | |
int count; | |
ARRAY **seperated_numbers; | |
} TWO_DIMENSIONAL_ARRAY; | |
void print_array(ARRAY *); | |
void print_two_dimensional_array(TWO_DIMENSIONAL_ARRAY *); | |
void free_memory(TWO_DIMENSIONAL_ARRAY *); | |
ARRAY *initialize_array(int length); | |
TWO_DIMENSIONAL_ARRAY *initialize_two_dimensional_array(int length, int deep_array_size); | |
ARRAY *filter_numbers_less_than_limit(int *numbers, int length, int limit); | |
ARRAY *filter_numbers_greater_than_limit(int *numbers, int length, int limit); | |
ARRAY *filter_numbers_in_the_range(int *numbers, int length, int start, int end); | |
TWO_DIMENSIONAL_ARRAY *separate(int *numbers, int size, int start, int end); |
ARRAY and TWO_DIMENSIONAL_ARRAY do not tell that it is based on integers.
Why does TWO_DIMENSIONAL_ARRAY have seperated_numbers ? Field names and the Structures type should make sense together.
You need to review your code after it works and rename types, fields, variables & functions according to what they mean in the larger context.
Mention parameter names in the header file. It is hard to know the purpose of each parameter when there are many of the same type.
Use a spell checker, there are spelling mistakes like separate.
You might want to rethink the structure for the result. You know that you need exactly 3 lists and you also know which range is expected in each of those lists.
This is wrong:
typedef struct {
int *count;
int *list;
} ARRAY;
array->count = malloc(sizeof(int));
Why do you need a pointer to remember the count and allocating space too, it is an int inside a structure.
And why are not remembering the length? You are passing the structure data around, How will the consumer know whats the max length.
typedef struct {
int *count;
ARRAY **seperated_numbers;
} TWO_DIMENSIONAL_ARRAY;
ARRAY** does not make sense. You need an array of arrays. A single * would have done the job for you.
Inconsistent naming free_array_memory and free_memory. Not reviewing the code enough.
You are using three separate functions that do the same work. Can you simplify that? Also you have magic numbers like 4 and 7 hard coded.
Your TWO_DIMENSIONALY_ARRAY is an ARRAY **. That is somewhat confusing. On top of that you have somethings that return an TWO_D_ARRAY *
Simplify this.
I also do not understand what you have written on line 67. Why is this if block needed?
https://gist.github.com/satheesh-chandran/33b2d01cedb0caf83b34c893f6539bd5#file-seperation-c-L67