Advertisement
Ne-Biolog

Untitled

Mar 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. //
  2. // main.c
  3. // lab2_8
  4. //
  5. // Created by Денис Домашевич on 23.02.2018.
  6. // Copyright © 2018 Денис Домашевич. All rights reserved.
  7. //
  8.  
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12.  
  13. //Проверка на ввод числа тип LongLong
  14. long long readLongLong() {
  15. long long res;
  16. while(true) {
  17. if(scanf("%lld", &res) && getchar() == '\n') {
  18. return res;
  19. } else {
  20. printf("Try again\n");
  21. while(getchar() != '\n') {}
  22. }
  23. }
  24. }
  25.  
  26. //Проверка на ввод числа типа Double
  27. double readDouble() {
  28. double res;
  29. while(true) {
  30. if(scanf("%lf", &res) && getchar() == '\n') {
  31. return res;
  32. } else {
  33. printf("Try again\n");
  34. while(getchar() != '\n') {}
  35. }
  36. }
  37. }
  38.  
  39. int main() {
  40.  
  41. printf("Enter x = \n");
  42. long long x = readLongLong(), iter = 0;
  43. printf("Enter eps = \n");
  44. double eps = readDouble(), currY = (double)x;
  45.  
  46. while(true) {
  47. iter++; // Подсчёт количества итераций
  48. double newY = (currY + (x / currY)) / 2;
  49. if(eps + newY >= currY) {
  50. break;
  51. }
  52. currY = newY;
  53. }
  54.  
  55. printf("Result of my function %lf\n", currY);
  56. printf("Number of iterations %lld\n", iter);
  57. printf("Result of standart function %lf\n", sqrt((double)x));
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement