C-Programmierung sqrt-Funktion

#include <math.h> 
#include <stdio.h> 

int main(void) 
{ 
    double x = 4.0, result; 

    result = sqrt(x); 
    printf("The square root of %lf is %lfn", x, result); 
    return 0; 
} 

Dieser code funktioniert nicht, weil es ist die Quadratwurzel der variable. Wenn Sie ändern die sqrt(x) zu sqrt(20.0), der code funktioniert Prima, warum? Bitte erklären.

Auch, wie bekomme ich die Quadratwurzel der Variablen (das ist, was ich wirklich brauche)?

AUSGABE:

matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c
/tmp/ccw2dVdc.o: In function `main':
sqroot2.c:(.text+0x29): undefined reference to `sqrt'
collect2: ld returned 1 exit status
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

HINWEIS:sqroot1 ist die sqroot von 20.0. sqroot2 ist die sqroot einer Variablen.

matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c -lm
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot2
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

GELÖST.

Nach wie es nicht funktioniert, weil es funktionieren würde...
Der code sieht in Ordnung für mich. Welches Ergebnis erhalten Sie, wenn Sie Sie ausführen?
Sie sagen, es funktioniert nicht: was sehen Sie? Sollte es funktionieren, obwohl es als eine Frage von Stil, den ich ' D setzen die Ergebnis-variable in einer anderen Zeile. Auch Ihnen fehlt ein / vor dem Finale n in Ihrem printf.
wie kommt es, das es nicht funktioniert? druckt "Die Quadratwurzel von 4.000000 ist 2.000000 n" für mich (codepad.org/Mv4NNVFU)
Bitte formatieren Sie Ihre Konsole Probe zu Hälfte anständig. Vielen Dank und viel Glück!

InformationsquelleAutor matthewmpp | 2010-11-08

Schreibe einen Kommentar