Wrote this as part of an assignment. It is supposed to use the infinite series for sin, cos, and atan and calculate the sum of a certain number of terms. But whenever I finish feeding it inputs, it stops responding.
#include <iostream>
using std::cout;
using std::cin;
double sinSeries(int, int);
double cosSeries(int, int);
double atanSeries(int, int);
int factorial(int);
int main(){
int input, choice, angle;
double result;
cout << "This program calculates a number of terms of the infinite series below:\n";
cout << "1. sine function\n2. cosine function\n3. inverse tangent function\n";
cout << "Choose a function (1-3): ";
cin >> choice;
cout << "Enter the number of terms to calculate: ";
cin >> input;
cout << "Enter the angle to calculate: ";
cin >> angle;
switch(choice){
case 1:
result = sinSeries(input, angle);
cout << "The result of adding " << input << "terms of the sine function series is ";
cout << result << "\n";
break;
case 2:
result = cosSeries(input, angle);
cout << "The result of adding " << input << "terms of the cosine function series is ";
cout << result << "\n";
case 3:
result = atanSeries(input, angle);
cout << "The result of adding " << input << "terms of the arctangent function series is ";
cout << result << "\n";
default:
cout << "Invalid choice. Program will now close.";
return 5;
}
return 0;
}
double sinSeries(int terms, int number){
bool addition = false;
int thingy = 1;
double outward;
for(int i = 1; i <= terms; i++){
if(addition){
outward += ((number^thingy)/factorial(thingy));
addition = false;
}
else{
outward -= ((number^thingy)/factorial(thingy));
addition = true;
}
thingy += 2;
}
return outward;
}
double cosSeries(int terms, int number){
bool addition = false;
int thingy = 0;
double outward;
for(int i = 1; i <= terms; i++){
if(addition){
outward += ((number^thingy)/factorial(thingy));
addition = false;
}
else{
outward -= ((number^thingy)/factorial(thingy));
addition = true;
}
thingy += 2;
}
return outward;
}
double atanSeries(int terms, int number){
bool addition = false;
int thingy = 1;
double outward = 1;
for(int i = 2; i <= terms; i++){
if(addition){
outward += ((number^thingy)/thingy);
addition = false;
}
else{
outward -= ((number^thingy)/thingy);
addition = true;
}
thingy += 2;
}
return outward;
}
int factorial(int thing){
int a;
if(thing = 0){
return 1;
}
for(int i = 1; i <= thing; i++){
a *= i;
}
return a;
}