(I am trying to write a c++ program which computes sine of an angle without using c math library.I came up with a formula for this problem.The formula should use a factorial function in the program.I have a problem in doing this and hence i posted my code here for you to help me. The code is as below.Thank you in advance.)

#include<iostream>
#include<cmath>
using namespace std;

double num();
void for_r();
void for_d();
int factorial();

int main()
{
	double x2, x;
	char choice;
	
	cout<<"Please enter an angle value \n";
	cin>>x;
	cout<<"Is the angle value in Degree or Radian?"<<endl;
	cout<<"Type D if its in degree "<<endl;
	cout<<"Type R if its in radian "<<endl;
	cin >> choice;
	
	switch(choice)
{
	case 'R':
	for_r();
	break;
	case 'r':
	for_r();
	break;
	case 'D':
	for_d();
	break;
	case 'd':
	for_d();
	break;
	default:
	cout<<"Wrong input"<<endl;
	}
	cout<<"sin(x) = "<<num<<endl;
	return 0;
   }  
void for_r(){
	 
	int n = 3; 
	double x2= 0, b, PI,r;
	
	double sum = num();
			
	for(int i = 1;sum > 0.000001; i++){
			if(i == 1){
			   x2 = num() - (pow(num(),n) /factorial() ); 
			   n = n + 2;
	   }
		  else 
		    {
			   x2 = (x2 + ((pow(num(),n))/(factorial())) - (pow(num(),n+2) / factorial()+2));
			   n = n + 4;
		   }
	} 
 }
void for_d(){
	
	double x2= 0, PI, r;
    int  n = 3; 
	
	 r =( num() * (PI /180));
	     
	   double sum = num();
		for(int i = 1;sum > 0.000001; i++){
			if(i == 1){
			   x2 = num() - ((pow(num(),n) /factorial()));
			   n = n + 2;
	   }
		  else 
		    {
			   x2 = (x2 + ((pow(num(),n))/factorial() ) - (pow(num(),n+2) /(factorial()+2)));
			   n = n + 4;
		   }
	 }
 }
double num(){
	
	int n = 3;
	double x2, x;
	
	x2 =(x2 + ((pow(num(),n))/factorial())-(pow(num(),n+2)/(factorial()+2)));
	n = n + 4;
		
	return (x2);
	}

int factorial(){
	
	int n = 3;
   return (n * factorial ()-1);
}

>I have a problem in doing this and hence i posted my code here for you to help me.
That's nice, but you forgot to tell us what the problem is. No offense intended, but I have more interesting things to work on. I'm more likely to work on my own code than troubleshoot your code for an unspecified "problem".

>I have a problem in doing this and hence i posted my code here for you to help me.
That's nice, but you forgot to tell us what the problem is. No offense intended, but I have more interesting things to work on. I'm more likely to work on my own code than troubleshoot your code for an unspecified "problem".

The problem is that i can not call the factorial function.Also i think the formula i used might be wrong.

Factorial function is really pretty easy - often used in any tutorial about recursion, although a method using a loop is more efficient.

Keep in mind that factorial grows very fast - depending on how many iterations your calculation for sine goes, unsigned int may not be a suitable data type to return from your factorial function.

And review your material on writing functions - you need to be passing some arguments (parameters) to the ones you have in your program, but you're not.

Google is your friend.

This article has been dead for over six months. Start a new discussion instead.