diff --git a/WorkingArrays/43Ex/input.txt b/WorkingArrays/43Ex/input.txt new file mode 100644 index 0000000..27c3a72 --- /dev/null +++ b/WorkingArrays/43Ex/input.txt @@ -0,0 +1 @@ +3 3 3 3 3 3 \ No newline at end of file diff --git a/WorkingArrays/43Ex/main.c b/WorkingArrays/43Ex/main.c new file mode 100644 index 0000000..8d43740 --- /dev/null +++ b/WorkingArrays/43Ex/main.c @@ -0,0 +1,23 @@ +#include +#include "tools.h" +#include "max_distance.h" + +int main(void) { + double * numbers; + int max_distance; + FILE * file = getFile(); + if (file == NULL) return -1; + + numbers = getList(file); + if (numbers == NULL) return 1; + + max_distance = maxDistance(&numbers[1], (int)numbers[0] - 1); + + if (max_distance == NO_LOCAL_MINIMA) printf("There are no local minima in the file"); + else if (max_distance == ONE_LOCAL_MINIMUM) printf("Only one local minimum"); + else printf("Max distance between local minimum is %d\n", max_distance); + + free(numbers); + + return 0; +} diff --git a/WorkingArrays/43Ex/makefile b/WorkingArrays/43Ex/makefile new file mode 100644 index 0000000..b0fef65 --- /dev/null +++ b/WorkingArrays/43Ex/makefile @@ -0,0 +1,11 @@ +all: main.o max_distance.o tools.o + gcc main.o max_distance.o tools.o && del *.o + +main.o: main.c + gcc -c main.c + +max_distance.o: max_distance.c + gcc -c max_distance.c + +tools.o: tools.c + gcc -c tools.c diff --git a/WorkingArrays/43Ex/max_distance.c b/WorkingArrays/43Ex/max_distance.c new file mode 100644 index 0000000..8de51b2 --- /dev/null +++ b/WorkingArrays/43Ex/max_distance.c @@ -0,0 +1,30 @@ +#include "max_distance.h" + +int maxDistance(double * array, int length) { + int end_first_min, start_last_min, len_local; + + end_first_min = 0; + start_last_min = 0; + len_local = 1; + + for (int i = 1; i < length; ++i) { + if (len_local != OUT) { + if ((array[i] - array[i - 1]) < -exp) { + len_local = 1; + } else if ((array[i] - array[i - 1]) > exp) { + if (end_first_min == OUT) end_first_min = i; + else if (start_last_min == OUT) start_last_min = i - len_local; + len_local = 0; + } else {++len_local;} + } else { + if ((array[i] - array[i - 1]) < -exp) len_local = 1; + } + } + + if (len_local != OUT) start_last_min = length - len_local; + + if (end_first_min == 0) return NO_LOCAL_MINIMA; + else if (start_last_min == 0) return ONE_LOCAL_MINIMUM; + else return start_last_min - end_first_min; +} + diff --git a/WorkingArrays/43Ex/max_distance.h b/WorkingArrays/43Ex/max_distance.h new file mode 100644 index 0000000..b6d860a --- /dev/null +++ b/WorkingArrays/43Ex/max_distance.h @@ -0,0 +1,11 @@ +#ifndef MAX_DISTANCE +#define MAX_DISTANCE + +#define exp 1.e-6 +#define OUT 0 +#define NO_LOCAL_MINIMA -1 +#define ONE_LOCAL_MINIMUM -2 + +int maxDistance(double * array, int length); + +#endif diff --git a/WorkingArrays/43Ex/tools.c b/WorkingArrays/43Ex/tools.c new file mode 100644 index 0000000..5fee269 --- /dev/null +++ b/WorkingArrays/43Ex/tools.c @@ -0,0 +1,57 @@ +#include "tools.h" + +FILE * getFile(void) +{ + FILE * file; + char filename[50]; + + printf("Enter filename: "); + if (scanf("%s", filename) == 1) + { + file = fopen(filename, "r"); + if (file == NULL) { + printf("Error file!\n"); + return NULL; + } else { + return file; + } + } else + { + printf("Empty name!\n"); + return NULL; + } +} + +double * getList(FILE * file) { + double current; + int i, length = 2; + double * numbers = NULL; + + if (fscanf(file, "%lf", ¤t) != 1) { + printf("File is empty!"); + return numbers; + } + + numbers = (double *)malloc(length * sizeof(double)); + i = 1; + + do { + if (i >= length) { + length *= 2; + numbers = (double *)realloc(numbers, (length * sizeof(double))); + } + numbers[i] = current; + i++; + } while (fscanf(file, "%lf", ¤t) == 1); + + numbers = (double *)realloc(numbers, i * sizeof(double)); + numbers[0] = i; + return numbers; +} + +void outputArray(double * array, int length) { + for (int i = 0; i < length; ++i) { + printf("%.1lf\n", array[i]); + } + printf("\n"); +} diff --git a/WorkingArrays/43Ex/tools.h b/WorkingArrays/43Ex/tools.h new file mode 100644 index 0000000..9c92e68 --- /dev/null +++ b/WorkingArrays/43Ex/tools.h @@ -0,0 +1,11 @@ +#ifndef TOOLS +#define TOOLS + +#include +#include + +FILE * getFile(void); +double * getList(FILE * file); +void outputArray(double * array, int length); + +#endif