counting positive values

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

Join Date: Oct 2009
Posts: 43
Reputation: Ponomous is an unknown quantity at this point 
Solved Threads: 0
Ponomous Ponomous is offline Offline
Light Poster

counting positive values

 
0
  #1
Nov 5th, 2009
Hey so my code below works. It finds the roots for values that are in the quadratic equation from a file.

What i want to do is write a function that will count the number of positive roots. What would be the easiest way to do this?

I was thinking i could add
  1. if (root1 >= 0)
  2.  
  3. {
  4. counter++
  5. else if (root2 >= 0)
  6.  
  7. counter++
  8.  
  9. }

and then that would give me the total positive roots.

how would i do this in a function though?

here is the original code.

  1. #include <fstream> // Header File for File Reading
  2. #include <iostream> // Header File for General I/O-put
  3. #include <math.h>
  4. using namespace std; // Use namespacing
  5.  
  6. int main()
  7. {
  8.  
  9. ifstream in_stream;
  10. ofstream out_stream;
  11.  
  12. int a(0), b(0), c(0), Lasta(0), Lastb(0), Lastc(0);
  13. double bsqrd, fourac, root, root1, root2;
  14.  
  15.  
  16. in_stream.open("inputfile.txt");
  17. while (1)
  18. {
  19. Lasta=a; Lastb=b; Lastc=c;
  20.  
  21. in_stream >> a >> b >> c;
  22. if (a==Lasta && b==Lastb && c==Lastc){break;}
  23.  
  24.  
  25.  
  26. // calculations
  27.  
  28. bsqrd = b*b;
  29. fourac = 4*a*c;
  30. root = sqrt(bsqrd - fourac);
  31. root1 = (-b + root)/4;
  32. root2 = (-b - root)/4;
  33.  
  34.  
  35. cout << "the three values for this quadratic are" << " " << a << " " << b << " " <<c << endl;
  36. cout << "The value of x1 is: " << root1 << endl;
  37. cout << "The value of x2 is: " << root2 << endl;
  38.  
  39.  
  40.  
  41. }
  42.  
  43.  
  44.  
  45. in_stream.close();
  46.  
  47.  
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 636
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #2
Nov 5th, 2009
I don't understand the problem...
  1. int NumberOfPositiveRoots(root1, root2)
  2. {
  3. int counter = 0;
  4. if (root1 >= 0)
  5. {
  6. counter++;
  7. }
  8. if (root2 >= 0)
  9. counter++;
  10. }
  11. return counter;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: Ponomous is an unknown quantity at this point 
Solved Threads: 0
Ponomous Ponomous is offline Offline
Light Poster
 
0
  #3
Nov 5th, 2009
ok so i clearly am an idiot and did missed a letter when re typing the function and thats why it wasnt working for me.... sigh.... thanks for the help tho


Originally Posted by daviddoria View Post
I don't understand the problem...
  1. int NumberOfPositiveRoots(root1, root2)
  2. {
  3. int counter = 0;
  4. if (root1 >= 0)
  5. {
  6. counter++;
  7. }
  8. if (root2 >= 0)
  9. counter++;
  10. }
  11. return counter;
  12. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC