zadasd_zest2/asdzes2zad3heron/main.c

42 lines
768 B
C

/* Zad 3 z zestawu 2. Ćwiczenia z ASD.
* Oblicza pierwiastek kwadratowy z x za pomocą algorytmu Herona,
* wypisując na wyjściu kolejne wyrazy ciągu w kolejnych iteracjach
* Przyjmuje na wejściu liczbę x oraz docelową dokładność
* 24 sty 2019
* Michał Krzysztof Feiler (s444368) <archiet@wmi.amu.edu.pl>
* */
#include <math.h>
#include <stdio.h>
static long double c;
static long double eps;
static long double x;
static long long unsigned n;
int next()
{
long double p = x;
x = (p + (c / p)) / 2;
n++;
return fabsl(x - p) >= eps;
}
void print()
{
printf("x_%llu=%Lf, ", n, x);
}
int main()
{
scanf("%Lf %Lf", &c, &eps);
x = c;
n = 0;
print();
while (next())
print();
print();
return 0;
}