problem with calling a function

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

Join Date: Mar 2008
Posts: 2
Reputation: dmsjr12 is an unknown quantity at this point 
Solved Threads: 0
dmsjr12 dmsjr12 is offline Offline
Newbie Poster

problem with calling a function

 
0
  #1
Mar 7th, 2008
I'm trying to create a function that uses loops to determine the roots of an equation using the Newton-Raphson method. I have my Newton function set up to take the input from my main prog, calculate the roots, and then return them back into the main prog. Somewhere along the lines something is going wrong. When I input my negative and positive values, I receive my first line of output "Call Newton<given guess> saves root and returns function" but then the program stops. I'm not sure if this is a problem with my loop, my Newton function, or with my program calling the Newton function. here is my code

Main program
  1. #include "head.h"
  2.  
  3. int main(void)
  4. {
  5.  
  6. double guess, root; //variables needed for Newton.
  7. char choice = 81; // 81 is decimal code for capital Q; 113 is lower case q
  8. int enterkey, i;
  9. do {
  10.  
  11. cout << "\nmenu displayed: Enter A for 6.a., B for 3., or Q to quit ";
  12.  
  13.  
  14. cout << "\nFor example, type B for Exercise 3 followed by Enter key ";
  15. choice = toupper(getchar( )); enterkey = getchar( );
  16. cout << choice <<":"<< enterkey << endl;
  17. switch (choice) // a decision structure to select only one case out of many
  18. {
  19.  
  20.  
  21.  
  22. case 'A':
  23. for(i=1;i<=2;i++)
  24. {
  25. cout<<"Enter a negative followed by a positive "<<endl;
  26. cin>>guess;
  27. cout<<"Call Newton(given guess), returns and saves root\n";
  28. cout<<"After Newton the root is "<<Newton(guess)<<endl;
  29. }//end for
  30. cout<<"After the loop i is "<<i<<endl;
  31. break;
  32. default:
  33. if (choice != 'Q')
  34. {cout << "\nIncorrect Choice: Enter (A,B, or Q as valid)\n";
  35. }//endif
  36. } // end switch
  37.  
  38. } while (choice != 'Q'); //end do-while
  39.  
  40. cout << " Fini\n";
  41.  
  42.  
  43. system("pause");
  44. return 0;

and here is my Newton function
  1. double guess, xi,root;
  2. int i;
  3. double Newton(double guess)
  4. {
  5. for (i=1,10;1;)
  6. {
  7. xi=guess;
  8. (xi)=(xi-(3*pow(xi,2)+2*xi-2)/(6*xi+2));
  9. }//end for
  10. root =xi;
  11. cout<<"in Newton\n";
  12. return root;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: dmsjr12 is an unknown quantity at this point 
Solved Threads: 0
dmsjr12 dmsjr12 is offline Offline
Newbie Poster

Re: problem with calling a function

 
0
  #2
Mar 7th, 2008
basically what i'm trying to do is make the newton function accept two numbers, plug them through the equation f(x)/f'(x) and return a root. I then want prog.cpp to return that root
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: crahhhhhb is an unknown quantity at this point 
Solved Threads: 0
crahhhhhb crahhhhhb is offline Offline
Newbie Poster

Re: problem with calling a function

 
0
  #3
Mar 7th, 2008
halp
Last edited by crahhhhhb; Mar 7th, 2008 at 2:54 pm.
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