HELP!

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.
Do you have any coding knowledge?

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.
Get the formula for solving a quadratic equation first. We can go from there.
I'm guessing it doesn't have to be a specific formula so I just make up a random one?

So i can ill use: x^2 - 7x + 10

and i know you have to use #include <cmath> to enter that. is this correct?
I'm guessing it doesn't have to be a specific formula so I just make up a random one?


No, you use x = ( (-b)+ sqrt(b*b - 4*a*c) ) / 2a

The intent is that the user enters values for a, b and c, and you provide the values for x.


#include <cmath> to enter that. is this correct?
<cmath> provides no such facility.

Last edited on
Oh okay. So how would I go about writing a program to find all this information out?
So far this is what I got:

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main ()
{
Do you have any math knowledge?

For example, how to solve a quadratic equation?
Yeah I know how to solve a quadratic equation. Im in Calculus 1 right now.
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?
Last edited on
We are currently at relational operators and if if/else and if/else if statements
Before you got there, did you cover variables and input and output?
yeah we did. cout and cin and double and int and all that?
Right, good.

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.

That of any help?
Then the following should not be beyond you:

- 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
That is beyond helpful!!! Thank ya'll so much!!
Topic archived. No new replies allowed.