Advertisement
Guest User

awful

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. int factorial(unsigned int a)
  10. {
  11.     if (a== 0)
  12.         return 1;
  13.     else
  14.     {
  15.         unsigned int i = 1;
  16.         unsigned long int sum = 1;
  17.         while (i <= a)
  18.         {
  19.             sum = sum*i;
  20.             i++;
  21.         }
  22.         //cout << sum << "\n";
  23.         return sum;
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     double xval, yval, eps, sum, sumprev;
  30.     unsigned int i;
  31.     cout << "Enter a value for x: ";
  32.     cin >> xval;
  33.     eps = 0;
  34.     while (eps != 1 && eps != 2 && eps != 3)
  35.     {
  36.         cout << "\nChoose a value for epsilon for computing sin(x):\n1) 0.1\n2) 0.01\n3) 0.001\n";
  37.         cin >> eps;
  38.     }
  39.     if (eps == 1)
  40.         eps = 0.1;
  41.     else if (eps == 2)
  42.         eps = 0.01;
  43.     else if (eps == 3)
  44.         eps = 0.001;
  45.     sum = 0;
  46.     sumprev = 99;
  47.     i = 0;
  48.     while (abs(sum - sumprev) > eps)
  49.     {
  50.         if (2 * i + 1 > 13)
  51.         {
  52.             cout << "Maximum factorial reached\n"; //Cannot compute factorials over 14 due to size limit of long type.
  53.             sumprev = sum;
  54.         }
  55.         else
  56.         {
  57.             sumprev = sum;
  58.             sum = sum + (pow(-1, i) / factorial(2 * i + 1))*pow(xval, 2 * i + 1);
  59.             cout << "at n=" << i << ", sin(" << xval << ") = " << sum << "\n";
  60.             i++;
  61.         }
  62.     }
  63.  
  64.     sum = 0;
  65.     for(unsigned int cosLoopI = 0; cosLoopI <= 7; cosLoopI++) //Cannot compute factorials over 14 (2*7) due to size limit of long type.
  66.     {
  67.         sum = sum + (pow(-1, cosLoopI) / factorial(2 * cosLoopI))*pow(xval, 2 * cosLoopI);
  68.         cout << "at n=" << cosLoopI << ", cos(" << xval << ") = " << sum << "\n";
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement