0

(The program below should calculate sine of an angle without using c math library.The angle should be in radian otherwise the program will convert it to radian.The program gives me the wrong output which probably means that it has some logic errors.I think the problem could be with the formula i used or maybe the way i called the functions in the main.I tried hard to locate and solve them but i could not manage to do so.So please help me.Thanks in advance.

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

double sum();
void for_r();
void for_d();

int main()
{
	double sum, t, x, PI;
	int n =1, i = 1;	
	char choice;
		
	cout<<"Please enter an angle value => ";
	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.Try again!"<<endl;
    }
    cout<<"MySin(x) = "<<sum<<endl;
  return 0;
}		
	
void for_r()
{	
	int n = 3, i = 1;
	double sum, x;
	
	for(n = 1; n > 1; n++)
	
	sum = n-2*(-1) * pow(x,2)/ (n * (n-1));
	n = n + 2;
}
    
void for_d()
{
	int n = 3, i = 1;
	double sum, x, R, PI;
	
	R = (x * (PI/180));
	
	for(n = 1; n > 1; n++)
	
	sum = n-2*(-1) * pow(R,2)/ (n * (n-1));
	n = n + 2;
}

double sum()
{
	double sum, x, R, PI;
	int n = 3;
	
	sum = (n-2*(-1) * (pow(x,2)/ (n-1)));
		
	return (sum);
}
2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by sarehu
0

I wrote a whole bunch of stuff, but really, you just need to get out a pencil and a sheet of paper, and simulate your program by hand. You must do that, and you must be able to do that. Also, review things like the scoping rules for variables.

Here is some writing about the problems with your program:

You have problems with your for loops, in that they never terminate. You have problems with the communication of information in and out of your functions, since you don't seem to understand how variables' scoping works. For example, you are modifying sum in for_r , but that does not mean the variable sum as declared in your main function will be modified. For it is a completely different instance of a variable, and the fact that they share the same name should be regarded as coincidental. The same goes for other variables in your functions like x . You need to explicitly pass those as parameters.

You also do some bizarre things that do not make sense. You have n = n + 2; at the end of your for_r function: did you mean to put that inside the for loop? But then you'd be incrementing n by 3 each time through the loop...


I could go on. There are so many ways to find mistakes in this code. You need to get out some pencil and paper and simulate this code by hand.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.