#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;
}
Ten kod nie działa, ponieważ pobiera pierwiastek kwadratowy zmiennej. Jeśli zmienisz sqrt(x)
, na sqrt(20.0)
, kod działa dobrze, dlaczego? Proszę wytłumacz.C programowanie funkcji sqrt
Ponadto, w jaki sposób uzyskać pierwiastek kwadratowy zmiennej (co jest tym, czego naprawdę potrzebuję)?
WYJŚCIE:
[email protected]:~/Programming/C.progs/Personal$ vim sqroot1.c
[email protected]:~/Programming/C.progs/Personal$ cc -c sqroot1.c
[email protected]:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
[email protected]:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
[email protected]:~/Programming/C.progs/Personal$ vim sqroot2.c
[email protected]:~/Programming/C.progs/Personal$ cc -c sqroot2.c
[email protected]:~/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
[email protected]:~/Programming/C.progs/Personal$
UWAGA: sqroot1 jest sqroot 20,0. sqroot2 to sqroot zmiennej.
[email protected]:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c -lm
[email protected]:~/Programming/C.progs/Personal$ ./sqroot2
4.472136
[email protected]:~/Programming/C.progs/Personal$
SOLVED.
post jak to nie działa, ponieważ to działa ... –
Ten kod wygląda dobrze do mnie. Jaki wynik uzyskujesz po uruchomieniu? –
Mówisz, że to nie działa: co widzisz? To powinno zadziałać, chociaż ze względu na styl umieściłbym zmienną wyników na innej linii. Poza tym brakuje ci/przed ostatnim n w twoim printf. – winwaed