This is a program I was told to create for one of my classes and I'm so lost!
Write a program that will calculate both roots of a quadratic equation. The program should ask the user to input a, b, and c and then write both roots to standard output (the screen) and a file named "program.txt".
Your program should print the roots ordered from least to greatest.
If the quadratic equation has a negative discriminant your program should output "NO REAL ROOTS".
If the two roots are the same only report one root.
My first step would be to get the quadratic formula down in code form first. Then you can deal with the different outputs a bit later. Take it step-by-step.
I have zero coding knowledge. This is my first class in computer science and we are using C++. The class said no previous comp sci knowledge is required but I think they were lying because we are moving so fast and I am so lost, but everybody else in my class seems to be doing just fine. And this problem is our first graded problem so I'm kind of freaking out.
Ok, as Moschops has detailed above, the quadratic formula is there and, as you'll know, that formula will solve any quadratic equation (be it two real, different or no roots).
When Moschops gave the formula, (s)he almost wrote it completely in code form.
What stage have you got up to in your CompSci class? Variable declarations? Data types?
So you'll most likely have covered data types, variable assignments and general program flow.
So step-by-step, you just need to go about building this program up.
1 2 3 4 5 6 7
// We need some variables for a, b and c. Declare those.
// We also need to store the solutions, so two more variables (call them x1, x2 or whatever)
// Ask the user to input a value for each variable (Have you used cin?)
// Set solution (x1) to the solution of the quadratic equation using +
// Set solution (x2) to the solution of the quadratic equation using -
// Compare the two solutions
// Print out in ascending order or output no real roots
Note, when I say 'set solution of quadratic equation', I mean the users inputted variables should be substituted into the equation at the appropriate places.
- ask user for the value of a.
- get value of a.
- ask user for the value of b.
- get value of b.
- ask user for the value of c.
- get value of c.
- calculate value of discriminant and assess from the value if real roots exist
- if real roots exist, calculate them
- present real roots to user