KMP-MPI/util.c

48 lines
870 B
C
Raw Normal View History

2021-03-15 20:04:23 +00:00
#include <stdio.h>
2021-03-19 16:56:57 +00:00
#include <malloc.h>
2021-03-15 20:04:23 +00:00
void print_array(int *array, int length) {
2021-03-19 16:56:57 +00:00
if (length == 0) {
printf("\n");
return;
}
2021-03-15 20:04:23 +00:00
printf("|");
for (int i = 0; i < length; ++i) {
printf("%d|", array[i]);
}
printf("\n");
}
2021-03-19 16:56:57 +00:00
2021-03-21 13:30:02 +00:00
void apply_shift(int shift, int *array, int size) {
for (int i = 0; i < size; ++i) {
array[i] += shift;
}
}
int sum_array(int *array, int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += array[i];
}
return sum;
}
2021-03-19 16:56:57 +00:00
char *read_file(char *filepath, int *len) {
FILE *f = fopen(filepath, "r");
fseek(f, 0, SEEK_END);
*len = ftell(f);
fseek(f, 0, SEEK_SET);
2021-03-25 18:00:59 +00:00
char *content = (char *) malloc(sizeof(char) * (*len));
2021-03-19 16:56:57 +00:00
fread(content, 1, *len, f);
2021-03-25 18:00:59 +00:00
*len = *len - 1;
2021-03-19 16:56:57 +00:00
content[*len] = '\0';
fclose(f);
return content;
}