I've got this snippet of code that checks for the biggest set of numbers in a formatted file and even if the algorithm works, getting the position of the said values, the output is always the last read row. What is happening?
int main() {
int n, north_key, east_key;
char *identity, *time, *eastIdentity, *eastTime, *northIdentity, *northTime;
float latitude, longitude, max_east = MIN, max_north = MIN;
input = fopen("level2-1.in", "r");
fscanf(input, "%d", &n);
for(int i = 0; i <= n; i++) {
char line[MAX];
fgets(line, MAX, input);
identity = strtok(line, ",");
time = strtok(NULL, ",");
char *aux = strtok(NULL, ",");
latitude = std::atof(aux);
aux = strtok(NULL, "\n");
longitude = std::atof(aux);
if(max_north < latitude) {
max_north = latitude;
north_key = i;
northIdentity = identity;
northTime = time;
}
if(max_east < longitude) {
max_east = longitude;
east_key = i;
eastIdentity = identity;
eastTime = time;
}
printf("%d, %d\n", north_key, east_key);
}
printf("%s,%s, %s,%s\n", northIdentity, northTime, eastIdentity, eastTime);
fclose(input);
return 0;
}
Please login or Register to submit your answer