| | |
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: 636
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
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





