here's my program:
// quadratic problem
// peter flynn
//
// this program takes the solves quadratic equations using the user - inputted
// coefficients y = Ax^2+BX+C
#include <iostream.h>
#include <math.h>
void main()
{
// declare variables
float a, b, c, discriminant;
float positiveAnswer, negativeAnswer, answer;
// get values for a, b, and c
cout << "What is the value of a?\n";
cin >> a;
cout << "What is the value of b?\n";
cin >> b;
cout << "What is the value of c?\n";
cin >> c;
cout << '\n';
// calculate the discriminant
discriminant = ((pow(b,2))-(4*a*c));
// if statements
if (discriminant < 0)
{
cout << "There are two imaginary solutions,\n";
cout << "I'd like to tell you, but I can't.\n";
}
if (discriminant > 0)
{
negativeAnswer = (-b - sqrt(discriminant))/(2*a);
positiveAnswer = (-b + sqrt(discriminant))/(2*a);
cout << "Your answers are " << negativeAnswer << ",\n";
cout << "and " << positiveAnswer << ".\n";
}
if (discriminant == 0)
{
answer = (b * -1) / (2 * a)
cout << "Your answer is " << answer << ".\n";
}
} // end main()
"The book depository, depository, depository...damn! he could be anywhere!!"
Officer Barbrady, South Park
come see my band's website:
http://www.geocities.com/SunsetStrip/Stage/7044/index.html
This comment at the top is very good. Only improvement I can think
of: put the year in, so you'll know it when you look at the code
in year 23. Eh, 2023.
> #include <iostream.h>
> #include <math.h>
>
> void main()
Although most compilers allow this, and 1996 draft standard included
examples with "void main", the draft standard now says that main
must return an int.
>
> {
>
> // declare variables
>
> float a, b, c, discriminant;
> float positiveAnswer, negativeAnswer, answer;
Ok.
>
> // get values for a, b, and c
>
> cout << "What is the value of a?\n";
> cin >> a;
> cout << "What is the value of b?\n";
> cin >> b;
> cout << "What is the value of c?\n";
> cin >> c;
When using line-buffered input, as here, you might risk that
extranous characters at the end of one input line is read in
as data by the next input operation. To avoid it, empty the
input buffer, which you can do by calling "ignore" (I think
it was), or making your own function which just eats up
characters till it has eaten the newline character.
> cout << '\n';
Here I would write out the equation, to see the numbers in
context.
>
> // calculate the discriminant
>
> discriminant = ((pow(b,2))-(4*a*c));
Here you're limited by the language: there's no standard way
of catching floating point exceptions. Yet, in a real world
program, you would want to do so. That's one terribly
complicated and non-portable area you will want to learn more
about. You could do it sort of portable in C (see "signal").
>
> // if statements
>
> if (discriminant < 0)
>
> {
> cout << "There are two imaginary solutions,\n";
> cout << "I'd like to tell you, but I can't.\n";
> }
Whoops, analysis! That's important. The two solutions are
not necessarily imaginary, they're general complex numbers
(a complex conjugate pair, meaning they're mirrored about
the real number axis).
>
> if (discriminant > 0)
>
> {
> negativeAnswer = (-b - sqrt(discriminant))/(2*a);
> positiveAnswer = (-b + sqrt(discriminant))/(2*a);
>
> cout << "Your answers are " << negativeAnswer << ",\n";
> cout << "and " << positiveAnswer << ".\n";
> }
Since the cases are mutually contradictory, use the "else if"
construction. Then, the next "if" reduces to just "else", with
a comment.
>
> if (discriminant == 0)
>
> {
> answer = (b * -1) / (2 * a)
Be consistent. Although your compiler will most likely optimize
this away, write just "-b" instead of "b*-1".
>
> cout << "Your answer is " << answer << ".\n";
> }
>
> } // end main()
No need to duplicate code in comments: "}" implies "end". But
the idea of the comment itself is very good. Every function should
have a comment at the end, like this. It's especially useful when
scrolling upwards in an editor.
All in all, very nice code you've produced.
Hth.,
- Alf
<BIG snip>
> can anyone tell me the code i would have to use to detect leap years in
> an if statement. ni...@interlog.com
Here is a clause embedded in a statement for test purposes. I am sure you can
cut and paste the interesting part into an if statement.
void test1()
{
while(1)
{
// n must have 4 digits, eg: 1996
cin >>n;
int leap = (n%4==0 && !(n%100==0)) || (n%400 == 0 && !n%100 == 0);
cout << leap;
}
}