/////////////////////////////////////////////////////////////////////////////////////////
// Brian Goggins
// 2/17/2015
// COSC 281
/////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double factorial(double i)
{
double sum = 1;
if (i > 0)
{
while (i >= 1)
{
sum *= i;
i--;
}
return sum;
}
else if (i == 0)
{
return sum = 1;
}
}
double problem1a(double x, double n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += pow(x, i);
}
return sum;
}
void problem1b(double x, double n)
{
double sum = 0, sum2=0;
for (int i = 0; i <= n; i++)
{
sum += pow(x, i);
}
sum2 = (1 - pow(x, n + 1)) / (1 - x);
}
double problem2a(double x, double n)
{
double sum = 0, fact = 0;
for (int i = 0; i <= n; i++)
{
fact = factorial(i);
sum += (pow(x, i)) / fact;
}
return sum;
}
double problem2b(double x, double n)
{
double sum = 0, fact = 0;
for (int i = 0; i <= n; i++)
{
fact = factorial(2*i);
sum += ((pow(-1, i)) / (fact))*(pow(x, 2 * i));
}
return sum;
}
double problem2c(double x, double n)
{
double sum = 0, fact = 0;
for (int i = 0; i <= n; i++)
{
fact = factorial((2 * i) + 1);
sum += ((pow(-1, i)) / (fact))*(pow(x, (2 * i) + 1));
}
return sum;
}
double problem3(double n)
{
double sum = 0, fact = 0;
for (int i = 0; i <= n; i++)
{
sum += pow(-1, i) / ((2*i)+1);
}
return sum;
}
int main()
{
int exit1 = 0;
double answer = 0, answer2 = 0, pi=4*atan(1);
while (exit1 != 1)
{
//problem 1
answer = problem1a(3, 3);
cout << "(A) " << answer << endl;
answer = problem1a(3, 7);
cout << "(B) " << answer << endl;
answer = problem1a(.2, 3);
cout << "(C) " << answer << endl;
answer = problem1a(.2, 7);
cout << "(D) " << answer << endl;
//problem 2
//a
answer = problem2a(0, 6); answer2 = exp(0) - answer;
cout << "n(A) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl;
answer = problem2a(1, 7); answer2 = exp(1) - answer;
cout << "(A) N1=7 x=1 ttf(x)=" << answer << "tt" << fabs(answer2) << endl;
//b
answer = problem2b(0, 6); answer2 = cos(0) - answer; cout <<
"n(B) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl;
answer = problem2b(pi/4, 3); answer2 = cos(pi / 4) - answer;
cout << "(B) N1=3 x=pi/4 tf(x)=" << answer << "tt" << fabs(answer2) << endl;
answer = problem2b(pi/2, 4); answer2 = cos(pi / 2) - answer;
cout << "(B) N1=4 x=pi/2 tf(x)=" << answer << "t" << fabs(answer2) << endl;
//c
answer = problem2c(0, 6); answer2 = sin(0) - answer;
cout << "n(C) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl;
answer = problem2c(pi/4, 2); answer2 = sin(pi / 4) - answer;
cout << "(C) N1=2 x=pi/4 tf(x)=" << answer << "tt" << fabs(answer2) << endl;
answer = problem2c(pi/2, 4); answer2 = sin(pi / 2) - answer;
cout << "(C) N1=4 x=pi/2 tf(x)=" << answer << "ttt" << fabs(answer2) << endl;
//problem 3
answer = 4*(problem3(1000000)); fabs(pi - answer);
cout << "n(A) N=1000000 tt" <<fabs(pi - answer) << endl;
cout << "(B) Pi = " << setprecision(20) << pi << endl;
cout << "close<1> "; cin >> exit1;
}
return 0;}
OUTPUT
(A) 39
(B) 3279
(C) 0.248
(D) 0.249997
(A) N1=6 x=0 f(x)=1 0
(A) N1=7 x=1 f(x)=2.71825 2.78602e-005
(B) N1=6 x=0 f(x)=1 0
(B) N1=3 x=pi/4 f(x)=0.707103 3.56636e-006
(B) N1=4 x=pi/2 f(x)=2.47373e-005 2.47373e-00
(C) N1=6 x=0 f(x)=0 0
(C) N1=2 x=pi/4 f(x)=0.707143 3.62646e-005
(C) N1=4 x=pi/2 f(x)=1 3.54258e-006
(A) N=1000000 9.99999e-007
(B) Pi = 3.1415926535897931
close<1>
Pi approximation 3.14159 26535 89793 23846 26433 83279 50288 41971

More Related Content

DOC
Computer Science Investigatory Project class 12th
PPTX
我在 Mac 上的常用开发工具
PDF
3 rd animation
TXT
Angle debug
PDF
JavaOne2010 Groovy/Spring Roo
PDF
企业DevOps之旅
PPTX
Introducing to Asynchronous Programming
Computer Science Investigatory Project class 12th
我在 Mac 上的常用开发工具
3 rd animation
Angle debug
JavaOne2010 Groovy/Spring Roo
企业DevOps之旅
Introducing to Asynchronous Programming

What's hot (9)

DOCX
Profile backup mail_atma_script (disk drumu/export)
PDF
Indicadores
PDF
You will learn RxJS in 2017
DOCX
Tugas2
PDF
JavaScript from Scratch: Getting Your Feet Wet
PDF
Operation Flow @ ChicagoRoboto
PDF
PDF
C++ TUTORIAL 5
PDF
Parse The Web Using Python+Beautiful Soup
Profile backup mail_atma_script (disk drumu/export)
Indicadores
You will learn RxJS in 2017
Tugas2
JavaScript from Scratch: Getting Your Feet Wet
Operation Flow @ ChicagoRoboto
C++ TUTORIAL 5
Parse The Web Using Python+Beautiful Soup
Ad

Similar to cosc 281 hw3 (20)

DOCX
Tugas praktikukm pemrograman c++
PDF
Simpson and lagranje dalambair math methods
PPTX
C++ Programm.pptx
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
C++ TUTORIAL 7
PDF
JavaScript Futures—ES2017 and Beyond
PDF
PDF
PDF
java-introduction.pdf
PDF
Set language and notation
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
DOC
Quiz using C++
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
DOC
Project hotel on hotel management fo
PDF
Lab Manual IV (1).pdf on C++ Programming practice
PDF
C++ TUTORIAL 4
PPT
PPTX
Adder Presentation
PDF
ECMAScript 6
Tugas praktikukm pemrograman c++
Simpson and lagranje dalambair math methods
C++ Programm.pptx
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
C++ TUTORIAL 7
JavaScript Futures—ES2017 and Beyond
java-introduction.pdf
Set language and notation
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
Quiz using C++
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
Project hotel on hotel management fo
Lab Manual IV (1).pdf on C++ Programming practice
C++ TUTORIAL 4
Adder Presentation
ECMAScript 6
Ad

cosc 281 hw3

  • 1. ///////////////////////////////////////////////////////////////////////////////////////// // Brian Goggins // 2/17/2015 // COSC 281 ///////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <iomanip> #include <cmath> using namespace std; double factorial(double i) { double sum = 1; if (i > 0) { while (i >= 1) { sum *= i; i--; } return sum; } else if (i == 0) { return sum = 1; } } double problem1a(double x, double n) { double sum = 0; for (int i = 1; i <= n; i++) { sum += pow(x, i); } return sum; } void problem1b(double x, double n) { double sum = 0, sum2=0; for (int i = 0; i <= n; i++) { sum += pow(x, i); } sum2 = (1 - pow(x, n + 1)) / (1 - x); } double problem2a(double x, double n) { double sum = 0, fact = 0; for (int i = 0; i <= n; i++) { fact = factorial(i); sum += (pow(x, i)) / fact; } return sum; } double problem2b(double x, double n)
  • 2. { double sum = 0, fact = 0; for (int i = 0; i <= n; i++) { fact = factorial(2*i); sum += ((pow(-1, i)) / (fact))*(pow(x, 2 * i)); } return sum; } double problem2c(double x, double n) { double sum = 0, fact = 0; for (int i = 0; i <= n; i++) { fact = factorial((2 * i) + 1); sum += ((pow(-1, i)) / (fact))*(pow(x, (2 * i) + 1)); } return sum; } double problem3(double n) { double sum = 0, fact = 0; for (int i = 0; i <= n; i++) { sum += pow(-1, i) / ((2*i)+1); } return sum; } int main() { int exit1 = 0; double answer = 0, answer2 = 0, pi=4*atan(1); while (exit1 != 1) { //problem 1 answer = problem1a(3, 3); cout << "(A) " << answer << endl; answer = problem1a(3, 7); cout << "(B) " << answer << endl; answer = problem1a(.2, 3); cout << "(C) " << answer << endl; answer = problem1a(.2, 7); cout << "(D) " << answer << endl; //problem 2 //a answer = problem2a(0, 6); answer2 = exp(0) - answer; cout << "n(A) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl; answer = problem2a(1, 7); answer2 = exp(1) - answer; cout << "(A) N1=7 x=1 ttf(x)=" << answer << "tt" << fabs(answer2) << endl; //b
  • 3. answer = problem2b(0, 6); answer2 = cos(0) - answer; cout << "n(B) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl; answer = problem2b(pi/4, 3); answer2 = cos(pi / 4) - answer; cout << "(B) N1=3 x=pi/4 tf(x)=" << answer << "tt" << fabs(answer2) << endl; answer = problem2b(pi/2, 4); answer2 = cos(pi / 2) - answer; cout << "(B) N1=4 x=pi/2 tf(x)=" << answer << "t" << fabs(answer2) << endl; //c answer = problem2c(0, 6); answer2 = sin(0) - answer; cout << "n(C) N1=6 x=0 ttf(x)=" << answer << "ttt" << fabs(answer2) << endl; answer = problem2c(pi/4, 2); answer2 = sin(pi / 4) - answer; cout << "(C) N1=2 x=pi/4 tf(x)=" << answer << "tt" << fabs(answer2) << endl; answer = problem2c(pi/2, 4); answer2 = sin(pi / 2) - answer; cout << "(C) N1=4 x=pi/2 tf(x)=" << answer << "ttt" << fabs(answer2) << endl; //problem 3 answer = 4*(problem3(1000000)); fabs(pi - answer); cout << "n(A) N=1000000 tt" <<fabs(pi - answer) << endl; cout << "(B) Pi = " << setprecision(20) << pi << endl; cout << "close<1> "; cin >> exit1; } return 0;} OUTPUT (A) 39 (B) 3279 (C) 0.248 (D) 0.249997 (A) N1=6 x=0 f(x)=1 0 (A) N1=7 x=1 f(x)=2.71825 2.78602e-005 (B) N1=6 x=0 f(x)=1 0 (B) N1=3 x=pi/4 f(x)=0.707103 3.56636e-006 (B) N1=4 x=pi/2 f(x)=2.47373e-005 2.47373e-00 (C) N1=6 x=0 f(x)=0 0 (C) N1=2 x=pi/4 f(x)=0.707143 3.62646e-005 (C) N1=4 x=pi/2 f(x)=1 3.54258e-006 (A) N=1000000 9.99999e-007 (B) Pi = 3.1415926535897931 close<1> Pi approximation 3.14159 26535 89793 23846 26433 83279 50288 41971