SlideShare a Scribd company logo
1 of 3
/////////////////////////////////////////////////////////////////////////////////////////
// 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

What's hot

Profile backup mail_atma_script (disk drumu/export)
Profile backup mail_atma_script (disk drumu/export)Profile backup mail_atma_script (disk drumu/export)
Profile backup mail_atma_script (disk drumu/export)Anar Godjaev
 
Indicadores
IndicadoresIndicadores
Indicadoresviluvedu
 
You will learn RxJS in 2017
You will learn RxJS in 2017You will learn RxJS in 2017
You will learn RxJS in 2017名辰 洪
 
Tugas2
Tugas2Tugas2
Tugas2Av Ri
 
JavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet WetJavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet WetMichael Girouard
 
Operation Flow @ ChicagoRoboto
Operation Flow @ ChicagoRobotoOperation Flow @ ChicagoRoboto
Operation Flow @ ChicagoRobotoSeyed Jafari
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupJim Chang
 

What's hot (10)

Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 
Profile backup mail_atma_script (disk drumu/export)
Profile backup mail_atma_script (disk drumu/export)Profile backup mail_atma_script (disk drumu/export)
Profile backup mail_atma_script (disk drumu/export)
 
Indicadores
IndicadoresIndicadores
Indicadores
 
You will learn RxJS in 2017
You will learn RxJS in 2017You will learn RxJS in 2017
You will learn RxJS in 2017
 
Tugas2
Tugas2Tugas2
Tugas2
 
JavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet WetJavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet Wet
 
Operation Flow @ ChicagoRoboto
Operation Flow @ ChicagoRobotoOperation Flow @ ChicagoRoboto
Operation Flow @ ChicagoRoboto
 
Fork handout
Fork handoutFork handout
Fork handout
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful Soup
 

Similar to cosc 281 hw3

Similar to cosc 281 hw3 (20)

Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
901131 examples
901131 examples901131 examples
901131 examples
 
JavaScript Futures—ES2017 and Beyond
JavaScript Futures—ES2017 and BeyondJavaScript Futures—ES2017 and Beyond
JavaScript Futures—ES2017 and Beyond
 
Java p1
Java p1Java p1
Java p1
 
Java
JavaJava
Java
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdf
 
Set language and notation
Set language and notationSet language and notation
Set language and notation
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
Project hotel on hotel management fo
Project  hotel on hotel management foProject  hotel on hotel management fo
Project hotel on hotel management fo
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Ch4
Ch4Ch4
Ch4
 
Adder Presentation
Adder PresentationAdder Presentation
Adder Presentation
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
About Go
About GoAbout Go
About Go
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 

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