SHARE
TWEET

Untitled

a guest Mar 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Robert Kristof, Mateusz Urbanek Task 3
  2. //This program calculates Cos(x) without use of cmath library
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. double Power(double number, int power) {
  9.     if (power == 0) { return 1; }
  10.     else {}
  11.     double Powered = number;
  12.     for (int i = 1; i < power; i++) {
  13.         Powered *= number;
  14.     }
  15.     return Powered;
  16. }
  17.  
  18. double Factorial(double Fact) {
  19.  
  20.     if (Fact < 0) { return 0; }     //Factorial of number less than 0 does not exist
  21.     if (Fact == 0) { return 1; }    //Factorial of 0 is always equal to 1
  22.  
  23.     double Result = 1;
  24.     for (int i = 1; i <= Fact; i++) {
  25.         Result *= i;
  26.     }
  27.     return Result;
  28. }
  29.  
  30. double Cosinus(double x, double y) {
  31.  
  32.     int True = 1;
  33.     double PreviousX = 1;
  34.     double ResultOfCosine = 0;      //Result of Cosinus(x) function
  35.     int Iter = 1;
  36.  
  37.     while (True == 1) {
  38.         //cout << "Iteration number     " << Iter << endl;
  39.         double FirstVariable = Power(-1, Iter);
  40.         //cout << "I did first one     " << FirstVariable << endl;
  41.         double SecondVariable = Power(x, 2 * Iter);
  42.         //cout << "I did second one     " << SecondVariable << endl;
  43.         double ThirdVariable = Factorial(2 * Iter);
  44.         //cout << "I did third one     " << ThirdVariable << endl;
  45.         ResultOfCosine = ((FirstVariable * SecondVariable) / ThirdVariable) + PreviousX;
  46.         //cout << "afterfirst Result" << endl;
  47.         //cout << ResultOfCosine << endl;
  48.         //cout << "Previous      " << PreviousX << endl;
  49.         if (((PreviousX - ResultOfCosine <= y) && (PreviousX - ResultOfCosine >= 0)) || ((ResultOfCosine - PreviousX <= y) && (ResultOfCosine - PreviousX >= 0))) {
  50.             True = 0;
  51.             //cout << "Setting True" << endl;
  52.         }
  53.         else { True = 1; }
  54.         PreviousX = ResultOfCosine;
  55.         Iter++;
  56.     }
  57.     cout << "Your cosinus of x is equal to:\t" << ResultOfCosine << endl;
  58.     cout << "Number of iterations is equal to:\t" << Iter - 1 << endl;
  59.     return 1;
  60. }
  61.  
  62. int main() {
  63.     double x0;              //x0
  64.     double Acc;             //Accuracy
  65.     cout << "Input x0" << endl;
  66.     cin >> x0;
  67.     cout << "What is your accuracy?" << endl;
  68.     cin >> Acc;
  69.     Cosinus(x0, Acc);
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top