Function not calling a variable

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 18
Reputation: Dontais is an unknown quantity at this point 
Solved Threads: 0
Dontais's Avatar
Dontais Dontais is offline Offline
Newbie Poster

Function not calling a variable

 
0
  #1
Nov 22nd, 2008
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.

  1.  
  2. double cosx (int x, int answer);
  3.  
  4. int main ()
  5. {
  6. for (double x = 0.0; x <= 3.1; x = x + .1)
  7. {
  8. cout << "Radians" << setw(6) << "Cousines by cos(x)" << setw(15) << "Cousines by user defined" << endl;
  9. cout << setprecision(2) << fixed << x << setw(6) << cos(x) << setw(15) << cosx (x, answer) << endl;
  10. }
  11. }
  12.  
  13. double cosx (int x, int answer)
  14. {
  15. int n, num, denom, ans, term, count;
  16. n = 2;
  17.  
  18. while (ans >= .000001)
  19. {
  20. for (int i = 0; i <= n; n++)
  21. {
  22. num = num*n;
  23. }
  24. for (int k = num; k >= 1; k--)
  25. {
  26. denom = denom*k;
  27. }
  28. term = num/denom;
  29.  
  30. if ((term%11)== 0)
  31. ans = ans + term;
  32. else
  33. ans = ans - term;
  34. count++;
  35. n+= 2;
  36. }
  37. return ans;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Function not calling a variable

 
0
  #2
Nov 22nd, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 18
Reputation: Dontais is an unknown quantity at this point 
Solved Threads: 0
Dontais's Avatar
Dontais Dontais is offline Offline
Newbie Poster

Re: Function not calling a variable

 
0
  #3
Nov 27th, 2008
I am on my second attempt on this program but it doesn't output right.

  1.  
  2. #include <iostream>
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. using std::fixed;
  7.  
  8. #include <iomanip>
  9. using std::setw;
  10. using std::setprecision;
  11.  
  12. #include <cmath>
  13.  
  14.  
  15. double cosx (double x);
  16.  
  17. int main ()
  18. {
  19. double x = 0.0, result1, result2;
  20.  
  21.  
  22. cout << " Radians" << "Cousines by cos(x)" << setw(10) << "Cousines by user defined" << endl;
  23.  
  24.  
  25. while(x < 3.2)
  26. {
  27. result1 = cos(x);
  28. result2 = cosx(x);
  29.  
  30. cout << setw(10) << fixed << setprecision(2) << x << setw(10) << setw(10) << result1 << setw(10)
  31. << result2 << endl;
  32. x = x + .1;
  33. }
  34.  
  35. return 0;
  36. }
  37.  
  38. double cosx (double x)
  39. {
  40. double num, denom, ans, term;
  41. int n, count;
  42. n = 2;
  43. ans = 1;
  44. term = 1;
  45. count = 1;
  46.  
  47. while (fabs(term) >= .000001)
  48. {
  49. num =1;
  50. for (int i = 1; i <= n; n++)
  51. {
  52. num = num*n;
  53. }
  54.  
  55. denom = 1;
  56. for (int k = n; k >= 1; k--)
  57. {
  58. denom = denom*k;
  59. }
  60.  
  61. term = num/denom;
  62.  
  63. if ((count%2) == 0)
  64. {
  65. ans = ans + term;
  66. }
  67. else
  68. {
  69. ans = ans - term;
  70. }
  71. count++;
  72. n+= 2;
  73. }
  74.  
  75. return ans;
  76. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Function not calling a variable

 
1
  #4
Nov 28th, 2008
You calculate n! instead of x^n (see num calculation in the loop).
Think about less expensive calculation of the next series members...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 18
Reputation: Dontais is an unknown quantity at this point 
Solved Threads: 0
Dontais's Avatar
Dontais Dontais is offline Offline
Newbie Poster

Re: Function not calling a variable

 
0
  #5
Nov 30th, 2008
Thanks it worked.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC