When I compile the program it returns "error C2065: 'answer' : undeclared identifier". I thought answer was declared from the function and would call answer. Any tips where I'm going wrong on this. The program is made to out put Radius, cosx(x), and a user defined cosx in 3 rows.

double cosx (int x, int answer);

int main () 
{
for (double x = 0.0; x <= 3.1; x = x + .1) 
{
	cout << "Radians" << setw(6) << "Cousines by cos(x)" << setw(15) << "Cousines by user defined" << endl; 
	cout << setprecision(2) << fixed << x << setw(6) << cos(x) << setw(15) << cosx (x, answer) << endl;
}
}

double cosx (int x, int answer)
{
	int n, num, denom, ans, term, count; 
	n = 2; 

	while (ans >= .000001)
	{
		for (int i = 0; i <= n; n++)
		{
			num = num*n;
		}
		for (int  k = num; k >= 1; k--)
		{
			denom = denom*k; 
		}
		term =  num/denom; 

		if ((term%11)== 0)
			ans = ans + term; 
		else 
			ans = ans - term; 
		count++; 
		n+= 2; 
	}
	return ans; 
}

Recommended Answers

All 4 Replies

A function can't "call a variable". It may be called (from another functions) and may call another functions.
The parameter name "answer" in cosx header does not bear a relation to a word "answer" in the main function body. You must declare int variable (called answer or what else) in main and assign a value to this variable before using it in cosx call.
No need in the 2nd parameter of cosx at all. Look at cosx body: you don't use the 2nd parameter.
There are lots of another defects in your code. You declare the 1st parameters of cosx as int but obviously treat it as double. You compare ans (why int again? ) unitialized variable with 0.00001 double value in while loop conditional expression. You don't initialize num and denom variables too.
Correct the code and try again...

I am on my second attempt on this program but it doesn't output right.

#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 
using std::fixed;

#include <iomanip> 
using std::setw;
using std::setprecision; 

#include <cmath> 


double cosx (double x);

int main () 
{
	double x = 0.0, result1, result2; 
 

		cout << " Radians" << "Cousines by cos(x)" << setw(10) << "Cousines by user defined" << endl; 

	
		while(x < 3.2)
		{
			result1 = cos(x); 
			result2 = cosx(x); 

			cout << setw(10) << fixed << setprecision(2) << x << setw(10) << setw(10) << result1 << setw(10)
				<< result2 << endl; 
			x = x + .1; 
		}

return 0; 
}

double cosx (double x)
{
	double num, denom, ans, term;
	int n, count; 
	n = 2;
	ans = 1; 
	term = 1; 
	count = 1; 

	while (fabs(term) >= .000001)
	{
		num =1; 
		for (int i = 1; i <= n; n++)
			{
				num = num*n;
			}

		denom = 1; 
		for (int  k = n; k >= 1; k--)
		{
				denom = denom*k; 
		}
		
		term =  num/denom; 
		
		if ((count%2) == 0)
		{
			ans = ans + term; 
		}
		else 
		{
			ans = ans - term; 
		}
		count++; 
		n+= 2; 
	}

	return ans; 
}

You calculate n! instead of x^n (see num calculation in the loop).
Think about less expensive calculation of the next series members...

commented: Helped me in a great manner kudos +1

Thanks it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.