Skip to content

Instantly share code, notes, and snippets.

@htruong
Created December 24, 2020 06:53
Show Gist options
  • Save htruong/72d342c2734b7a41e40dcddcc1e7b4d7 to your computer and use it in GitHub Desktop.
Save htruong/72d342c2734b7a41e40dcddcc1e7b4d7 to your computer and use it in GitHub Desktop.
TD_HEART.C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printArr(int arr_len, int * arr) {
int i;
printf("Array dump [%2d]: ", arr_len);
for (i = 0 ; i < arr_len; i++) {
printf("[%2d] %2d ", i, arr[i]);
}
printf("\n");
}
// Write your function here...
int warningsCount(int measurement_count, int* measurement, int d) {
int ret = 0;
const int max_num=256;
int buffer[max_num];
int i;
int j;
for (i = 0; i < max_num; buffer[i] = 0, ++i);
for (i = 0; i < d; ++buffer[measurement[i]], ++i);
bool odd = d % 2;
for (i = d; i < measurement_count; ++i) {
int count = (d+1) >> 1;
int med_x2 = -1;
for (j = 0; j < max_num; ++j) {
count -= buffer[j];
if ((med_x2 == -1) && (count <= 0)) {
med_x2 = j;
if (odd) {
med_x2 <<= 1;
break;
}
}
if ((!odd) && (count <= -1)) {
med_x2 += j;
break;
}
}
if (med_x2 <= measurement[i])
++ret;
++buffer[measurement[i]];
--buffer[measurement[i-d]];
}
return ret;
}
int main(int argc, char *argv[])
{
FILE *fptr = NULL;
int measurement_count;
int d;
int* measurement;
int i;
int result;
if (argc <= 1) {
printf("Usage: %s INPUT.TXT\n", argv[0]);
printf("INPUT is the input sequence\n");
exit(EXIT_FAILURE);
}
fptr = fopen(argv[1], "r");
fscanf(fptr, "%d", &measurement_count);
fscanf(fptr, "%d", &d);
measurement = (int*) malloc(measurement_count * sizeof(int));
for (i = 0; i < measurement_count; i++) {
fscanf(fptr, "%d", measurement + i);
}
result = warningsCount(measurement_count, measurement, d);
printf("%d\n", result);
fclose(fptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment