Created
June 20, 2018 11:57
-
-
Save nguyenlinhnttu/d1e4b7f855b7f4c61d1a81c104927630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#define NUM_LINE 10 | |
float arrayIndex[NUM_LINE]; | |
int getArrayIndex(float min, float max, float unit) { | |
float tmp, grp_unit; | |
float grp_min, grp_max; | |
tmp = (max - min) / (NUM_LINE - 1); | |
grp_unit = unit; | |
if (tmp > unit) { | |
//ceil return smallest integer value greater than or equal param | |
grp_unit = ceil(tmp / unit) * unit; | |
} | |
//Caculating unit for graph | |
grp_min = 0; | |
grp_min = max - (NUM_LINE - 1) * grp_unit; | |
if (grp_min != min) { | |
tmp = (grp_min + min) / 2; | |
grp_min = (int)(tmp / unit) * unit; | |
} | |
int i; | |
for (i = 0; i < NUM_LINE; i++) { | |
arrayIndex[i] = grp_min + i * grp_unit; | |
} | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
int i, r; | |
float min, max, unit; | |
if (argc != 4) { | |
return -1; | |
} | |
min = atof(argv[1]); | |
max = atof(argv[2]); | |
unit = atof(argv[3]); | |
if (min < 0 || max < 0 || unit < 0 || min > max) { | |
return -1; | |
} | |
//printf("min: %3.1f max: %3.1f unit: %3.1f\n", min, max, unit); | |
r = getArrayIndex(min, max, unit); | |
if (r == -1) { | |
return -1; | |
} | |
for (i = 0; i < NUM_LINE; i++) { | |
printf("Num %d: %3.1f\n", i, arrayIndex[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment