Question:
C++ Programming...? Please Help!?
RK
2010-03-25 17:40:43 UTC
Write a program that will implement a function for the quadratic formula to determine both possible values of x given input coefficients a, b, and c.
Your program should read the coefficients a, b, and c from the input text file provided. Each row of the text file has one set of coefficients (a, b, and c in that order) for a single equation. The input text file will be called "input".
Contents of "input" text file:
1 3 2
1 6 9
3 -2 -1

1 -1 0
1 0 -4
2 2 -2

Each set of coefficients should be passed to your function that will carry out the quadratic formula. Your function should compute the two possible values of x for each set of coefficients.

Your program should also implement a second (value-returning) function called “num_positive” that takes the two possible x values (previously computed by your quadratic formula function) as parameters and returns how many of the x values are positive numbers. For example, if passed the x values 3 and 5, your function should return 2. (Assume that 0 is a positive number for this function.)

Your main() function should implement a loop that prints out three things:
1. The values of a, b, and c read from the file.
2. The two x values computed by your quadratic formula function.
3. How many of the x values computed by your quadratic formula function are positive numbers (using your value-returning “num_positive” function).

Sample output of one loop iteration:

a = 3, b = -2, c = -1
The value of x1 is: 1
The value of x2 is: -0.333333
1 of the x values is/are positive.

The loop in your program should continue to read sets of a, b, and c coefficients from the input file and compute the x values until it reaches the end of the file. Your functions should only employ pass by reference parameters when necessary.

Any help is appreciated in writing this code. Even if this question is a month old, I would like an answer if you know it. Thankyou.
Five answers:
MichaelInScarborough
2010-03-25 19:06:13 UTC
I added an exception, which actually handles the case that the expression under the square root is negative. In this case you would get (not necessarily compliant with your assignment) the output: The result is not a real number. I hope this is helpful for you.



http://pastebin.com/Jc1tXJj0



Good luck
moviebuff
2010-03-25 18:01:59 UTC
I don't think anybody is going to write the code for you, but you need to know the quadratic formula to write this program. A quadratic equation is of the form:



ax² + bx + c = 0



and the solution to any quadratic equation can be found with the quadratic formula:

..............._______

x = (-b ± √ b² - 4ac ) / 2a



(dots used to preserve spacing)



Your input file gives you the values of a, b, & c. You need to write the code that computes the two values of x using the quadratic formula...that is:

................._______

x1 = (-b + √ b² - 4ac ) / 2a

&.............._______

x2 = (-b - √ b² - 4ac ) / 2a



it sounds like you are supposed to write a single function that does that, with a function prototype that looks something like this:



void quadratic(int a, int b, int c, float &x1, float &x2);



plus do the other function that determines if 0, 1, or 2 of these solutions are greater than 0 and produce the desired output.



Your course lectures & textbook should have provided you with the tools to take it from here and produce the C++ code to do that.



Good luck.
glauser
2016-12-12 23:29:28 UTC
not anymore, at one time C++ replaced into only an extension of C. when you consider that then, C has replaced, and so has C++. even however that being mentioned that is common for an identical software to convey at the same time the two C and C++. the technique is an identical, yet there are basically some adjustments int he libraries which you incredibly want the compiler to link to.
SF
2010-03-25 17:49:45 UTC
What do you have so far? I can't help unless I know what you've tried.
Frecklefoot
2010-03-25 17:52:38 UTC
Start with this:



int main( int argc, char* argv[] )

{

}



Please ask if you need more help, and show what you've done so far so we know where you need help. HTH


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...