| | |
counting positive values
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 0
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
and then that would give me the total positive roots.
how would i do this in a function though?
here is the original code.
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)
if (root1 >= 0) { counter++ else if (root2 >= 0) counter++ }
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)
#include <fstream> // Header File for File Reading #include <iostream> // Header File for General I/O-put #include <math.h> using namespace std; // Use namespacing int main() { ifstream in_stream; ofstream out_stream; int a(0), b(0), c(0), Lasta(0), Lastb(0), Lastc(0); double bsqrd, fourac, root, root1, root2; in_stream.open("inputfile.txt"); while (1) { Lasta=a; Lastb=b; Lastc=c; in_stream >> a >> b >> c; if (a==Lasta && b==Lastb && c==Lastc){break;} // calculations bsqrd = b*b; fourac = 4*a*c; root = sqrt(bsqrd - fourac); root1 = (-b + root)/4; root2 = (-b - root)/4; cout << "the three values for this quadratic are" << " " << a << " " << b << " " <<c << endl; cout << "The value of x1 is: " << root1 << endl; cout << "The value of x2 is: " << root2 << endl; } in_stream.close(); return 0; }
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
0
#2 Nov 5th, 2009
I don't understand the problem...
C++ Syntax (Toggle Plain Text)
int NumberOfPositiveRoots(root1, root2) { int counter = 0; if (root1 >= 0) { counter++; } if (root2 >= 0) counter++; } return counter; }
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 0
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
•
•
•
•
I don't understand the problem...
C++ Syntax (Toggle Plain Text)
int NumberOfPositiveRoots(root1, root2) { int counter = 0; if (root1 >= 0) { counter++; } if (root2 >= 0) counter++; } return counter; }
![]() |
Similar Threads
- please help with bisection method!! (C++)
- need help adding positive and negative numbers (C++)
- Convert to Absolute Values (C)
- large number division (C++)
- Homework help ...sort of (Java)
- Sorting character arrays! (C++)
- C++ Data Types (C++)
- Need some help with my do-while and while loops (Java)
- i need ur help (C++)
Other Threads in the C++ Forum
- Previous Thread: Triangle.
- Next Thread: program writen to transfer galons into liters per mile
Views: 170 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





