Skip to content

Instantly share code, notes, and snippets.

@satheesh-chandran
Last active April 23, 2020 10:39
Show Gist options
  • Save satheesh-chandran/33b2d01cedb0caf83b34c893f6539bd5 to your computer and use it in GitHub Desktop.
Save satheesh-chandran/33b2d01cedb0caf83b34c893f6539bd5 to your computer and use it in GitHub Desktop.
Program to separate the numbers between a range from a list of numbers
#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);
@vharidas
Copy link

vharidas commented Apr 23, 2020

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.

@vharidas
Copy link

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.

@vharidas
Copy link

Inconsistent naming free_array_memory and free_memory. Not reviewing the code enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment