Move some functions to util

This commit is contained in:
Raffaele Mignone 2021-03-21 14:30:02 +01:00
parent 59053ba109
commit c25a0a5045
Signed by: norangebit
GPG Key ID: F5255658CB220573
3 changed files with 18 additions and 20 deletions

20
main.c
View File

@ -10,10 +10,6 @@
void find_end(int, char *, char *, int **, int *); void find_end(int, char *, char *, int **, int *);
void apply_shift(int, int *, int);
int sum_array(int *, int);
void initialize(); void initialize();
void distribute_text(); void distribute_text();
@ -229,19 +225,3 @@ void collect_results(int *match_number, int *matches) {
MPI_COMM_WORLD MPI_COMM_WORLD
); );
} }
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;
}

14
util.c
View File

@ -15,6 +15,20 @@ void print_array(int *array, int length) {
printf("\n"); printf("\n");
} }
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;
}
char *read_file(char *filepath, int *len) { char *read_file(char *filepath, int *len) {
FILE *f = fopen(filepath, "r"); FILE *f = fopen(filepath, "r");

4
util.h
View File

@ -5,4 +5,8 @@ void print_array(int *, int);
char *read_file(char *filepath, int *len); char *read_file(char *filepath, int *len);
void apply_shift(int, int *, int);
int sum_array(int *, int);
#endif //KMP_UTIL_H #endif //KMP_UTIL_H