Defribillators puzzle discussion


#352

[python]
I had the same issue and I was 100% that my code was good. After a while I found an error in my count_distance() formule. In my case it was the cosinus function, instead of dividing the lambda(latitude) I had cos(lambda(latitude)) / 2 :wink:
After change I have 100% score.


#353

Please note. This also requires Degrees and not rads. It dont work with rads


#354

Thanks, you’re the G, I didn’t think about having reversed it while filling my defib class.

I verified many times my formula and applied it in different ways but I still had this failing.

It now all passes.

Thanks bro


#355

i’m stuck. What does Single possibility mean? I’m stuck at 75%.


#356

I don’t fully understand the problem of replace, i used the same function but i don’t see the issue. Can you explain it further ?
Btw,I have the same problem(only the 2 first tests work and the distance is still correct)


#357

Still same problem…
I can’t see the problem… here is my code if you can help :slight_smile:
https://pastebin.com/3qfGtyJa


#358

OK, j’ai réussi, c’était juste ma méthode pour transformer la virgule en point qui ne marchait pas bien…


#359

Hello everyone, Im new to programming and im trying to solve the defibrillators problem.
I will share my code to you so that i will get some help.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <math.h>

typedef struct defib {
    char *name;
    double lon;
    double lat;
}def;

double distance(double lon_a, double lat_a, double lon_b, double lat_b) {
    double x, y, d;
    x = (lon_b-lon_a) * cos((lat_a+lat_b)/2);
    y = lat_b - lat_a;
    d = (sqrt(x*x+y*y)) * 6371.0;
    return d;
}

double char_to_double(char *a) {
    while(*a!='\0') {
        if(*a==',') {
            *a = '.';
        }
        a++;
    }
    return atof(a);
}

char *gotoNext(char c, char *str) {
    while(*str != c) {
        str++;
    }
    return str;
}

void getDefib(char *line, def *res) {
    line = gotoNext(';',line);
    line++;
    res->name = line;
    for(int k=0;k<3;k++) {
        line = gotoNext(';',line);        
        *line = '\0';
        line++;
    }
    char *lon = line;
    line = gotoNext(',',line);
    *line = '.';
    line = gotoNext(';',line);
    char *lat = ++line;
    line = gotoNext(',',line);
    *line = '.';
    res->lon=atof(lon);
    res->lat=atof(lat);
}   

int main(int argc, char **argv) {
    char strlon[50], strlat[50];
    int N;
    scanf("%s", strlon);
    scanf("%s", strlat);
    scanf("%d", &N);
    
    double lon = char_to_double(strlon);
    double lat = char_to_double(strlat);
    double min = DBL_MAX;
    char output[100];
    double d;
    char line[256];
    def res;
    
    for(int i=0;i<N;i++) {
        fgets(line,256,stdin);
        getDefib(line,&res);
        d = distance(lon,lat,res.lon,res.lat);
        if(d<min) {
            min=d;
            strcpy(output,res.name);
        }
    }
    printf("%s\n", output);
    return 0;
}

And this is the output :
Segmentation fault.

at Answer.c. function gotoNext (c=59 ‘;’, str=0x7ffffffff000 <error: Cannot access memory at address 0x7ffffffff000>) on line 32

at Answer.c. function getDefib (line=0x7fffffffed13 “\033rvw\375\320x86_64”, res=0x7fffffffe7f0) on line 43

at Answer.c. function main (argc=1, argv=0x7fffffffeb08) on line 75

For some reason there is a segmentation fault that i cannot understand and i cannot solve for enough days.
Could somebody help me? I would higly appreciate your help.Thanks in advance.