944,148 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 312
  • C++ RSS
Nov 5th, 2009
0

counting positive values

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Ponomous is offline Offline
80 posts
since Oct 2009
Nov 5th, 2009
0
Re: counting positive values
I don't understand the problem...
C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 5th, 2009
0
Re: counting positive values
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


Click to Expand / Collapse  Quote originally posted by daviddoria ...
I don't understand the problem...
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Ponomous is offline Offline
80 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Triangle.
Next Thread in C++ Forum Timeline: program writen to transfer galons into liters per mile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC