### Helllpp!

Im in a beginners course of programming for engineers and I am so lost. My teacher doesn't explain properly and doesn't wait for us to understand. Im already a couple assignments behind. Any help and explanation would be greatly appreciated!

One of the assignments is to program this problem
The roots of the quadratic equation (a x squared + b x + c = 0), a not equal to 0 are given by the following formula:
-b +/- sqroot of (b squared - 4 a c) / 2a

In this formula, the term b squared - 4 a c is called the discriminant. if b squared - 4 a c = 0 the the equation has a single (repeated root. if b squared - 54 a c > 0, the equation has two complex roots.

Write a program that prompts the user to input the value of a (the coefficient of x squared) b (the coefficient of x) and c (the constant) and outputs the type of roots of the equation furthermore, if b squared - 4 a c >= 0, the program should output the roots of the quadratic equation.

(Thank you so much in advance! So lost! please explain if possible.)
Well, you need to start by doing the bits you do understand. For example, get the values a, b and c from the input. Get as far as you can and post your code for more help.
(Im probably dead wrong on this) here goes nothing
#include iostream
int main ( )
cout <<"enter a value"<<endl;
cin>>a>>endl;
cout<<"enter b value"<<endl;
cin>>b>>endl;
cout<<"enter c value"<<endl;
cin>>c>>endl;

(thats what I have so far)
You need to declare some variables. For this type of calculation, most of your variables will be of type `double`. After you declared them (which allocates some storage to hold the value), you can then use `cin` to get the value.

Function main() needs an opening and closing brace, like this
 ```int main() { // your code // goes here return 0; }```

I know time is precious, but I think you'd be well advised to go through the tutorial, at least the basics. http://www.cplusplus.com/doc/tutorial/
Last edited on
Knew I was forgetting something

#include iostream
int main ( )
{double a
double b
double c
cout <<"enter a value"<<endl;
cin>>a>>endl;
cout<<"enter b value"<<endl;
cin>>b>>endl;
cout<<"enter c value"<<endl;
cin>>c>>endl;
return 0
}
Last edited on
I always find it useful to compile my program as I add new code. I can see a few errors (trivial ones) with your code, but the compiler will tell you what they are, so you can fix them.
Don't forget to use code tags when posting code. Makes it alot easier to read code. When you post or edit a post look to the right of the box and it'll be the <> button.
Thank you guys for your help. Im just going to go to school tomorrow and get help from where im at from one of the programming helpers. Thanks again!
So I finished it but it wont run. This is the code I have.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include using namespace std; int main(int argc, char *argv[]) { char reply; double a; double b; double c; double q1; double q2; do { cout<<"enter a value:"<>a; cout<<"enter b value:"<>b; cout<<"enter c value:"<>c; q1=(-b-sqrt(b*b - 4*a*c)/2/a (q2=(-b+sqrt(b*b - 4*a*c)/2/a) cout<<"The values of x are "<>reply; } while(reply="y"); system("PAUSE"); return EXIT_SUCCESS; }``````

It says there is a problem with this part
"cout<<"The values of x are "<<q1<<"and"<<q2<<endl; cout<<"do you want to find more x values?(Y/N)"<<endl;
First, I see you've made good progress since yesterday. Well done.

The compiler message in this case, and quite often in other programs, is due to a problem higher up.

Here it's lines 25 and 26. Neither line ends with a semicolon. That's one of the basics of C and C++. Each statement must end with a semicolon.

When you've done that, you need to re-read the original specification, regarding the "discriminant". In mathematical terms it will indicate whether the roots are real or imaginary.

But purely in the practical sense of writing a computer program, it becomes important too. The issue is, we cannot calculate the square root of a negative number (at least not using ordinary variables). So, before pushing ahead and calculating q1 and q2, it's necessary to test whether (b*b - 4*a*c) is negative. If so, the roots are imaginary. Output a message to that effect. otherwise, you can proceed with the calculation.

Last edited on
I wrote this program purely out of personal need, LOL. I had trigonometry, and was not going to do 50 quadratic equations using my brain (that would have taking a least 1.5 hours... :0) so I wrote one.

When the root is imaginary, you should have your program display what ((b^2) - ((4 * a) * c)) equals, that way, the user can construct the simpler equation from it.
Oh, and don't use system(). Read here for the reason why not: http://www.cplusplus.com/forum/articles/11153/ There is much better way:
 ``1234567891011`` ``````void PressEnterToContinue() { int c; cout << "Press ENTER to continue."; fflush(stdout); do { c = getchar(); } //end do while ((c != '\n') && (c != EOF)); } //end PressEnterToContinue ``````

Also, RC so you should `#include <complex> `. I trust you can derive the rest.
@Hotice
I don't do it that way. I just use a function from `conio.h`:

 ``12345678910111213141516171819202122`` ``````#include #include #include using namespace std; void cl() { Sleep(5); while(kbhit()) getch(); } void wait() { cout<< endl<< endl<< endl<< endl; cout<< "Press any key to continue..."<< endl; cl(); //function i wrote to clear the getch stream while(!kbhit()) //while no buttons have been pressed { } cl(); //clear the stream again }``````

Easy peasy. :)
@IWishIKnew These lines are resource-hungry.
 ``123`` `````` while(!kbhit()) //while no buttons have been pressed { }``````

This empty loop is busily consuming CPU cycles during the time that the program is seemingly doing "nothing". In special cases there may be a use for this, but as a general-purpose function it's something to be avoided.

Use task manager or process explorer on Windows to see the resource usage of a process. http://technet.microsoft.com/en-gb/sysinternals/bb896653
Interesting. I will look into that
I learned something here, from both of you. I hope I didn't confuse the living heck out of the original poster with my notation. (I can't help it sometimes...)
Regarding the problem I mentioned earlier, with this code:
 ``123`` `````` while(!kbhit()) //while no buttons have been pressed { }``````

Here's a quick solution. Add a Sleep() statement inside the loop. This may make the code ever so slightly less responsive (but I don't think you'd notice), and allow the operating system to make use of the time so released to handle background tasks. Try changing the length of the sleep too. Again, check with Process Explorer or task manager to see the effect.
 ``1234`` `````` while(!kbhit()) //while no buttons have been pressed { Sleep(100); // Reduces CPU usage }``````

Apologies to Trackett7493, I realise this is very much off topic. Please feel free to step back in, you will be welcome.
Last edited on
Topic archived. No new replies allowed.