Posts: 119
Threads: 18
Joined: Jul 2011
Reputation:
1
01-17-2012, 03:22 AM
(This post was last modified: 01-17-2012, 03:23 AM by Clones.)
I am beginner to c++, I want to make a program for Quadratic formula, only the positive part, but I can't make it.. I am trying this one
quadratic formula:
http://www.regentsprep.org/Regents/math/...adLe21.gif
Code: #include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,c,s; float x, sq;
printf("\n Enter Values For a, b & c: ");
scanf("%d %d %d",&a,&b,&c);
s=b*b-1*4*a*c;
sq=pow(s,1/2);
x=(-1*b+s)/2*a;
printf("\n Positive solution of given quadratic equation is: %f",x);
getch();
}
please do corrections in it and help me
[url=http://myurl.pk/BLgzA][/url]
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
First off, what compiler do you use? printf() is old.
Posts: 119
Threads: 18
Joined: Jul 2011
Reputation:
1
(01-17-2012, 04:36 AM)AceInfinity Wrote: First off, what compiler do you use? printf() is old.
I myself was using cin and cout but in our text book, they have used printf()
I am using Borland C++
[url=http://myurl.pk/BLgzA][/url]
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
Either way your equations are wrong, remember that order of operation comes into play, so USE BRACKETS where ever applicable to make sure that certain numbers get calculated first over others:
This is wrong anyway:
where does the -1 come in? and
-4ac != -1*4*a*c
that would do:
= -4
= -4a
= c(-4a)
Posts: 119
Threads: 18
Joined: Jul 2011
Reputation:
1
(01-17-2012, 04:22 PM)AceInfinity Wrote: Either way your equations are wrong, remember that order of operation comes into play, so USE BRACKETS where ever applicable to make sure that certain numbers get calculated first over others:
This is wrong anyway:
where does the -1 come in? and
-4ac != -1*4*a*c
that would do:
= -4
= -4a
= c(-4a)
Hmm, there I am wrong, C++ does not support MINUS sign with a VARIABLE, I had this in mind and put it with a number too, but that does not matter anything. The problem is with SQUARE ROOT of Negative number...
[url=http://myurl.pk/BLgzA][/url]
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
01-18-2012, 08:53 AM
(This post was last modified: 01-18-2012, 10:47 AM by AceInfinity.)
You don't know about the square root methods in C++ or you're just having issues with it in any updated code so far?
What are you talking about C++ doesn't support Minus sign with a variable though???
Make sure your variable definitions have the "signed" attribute... and not "unsigned"
Edit: I may not be able to help with the equation, but I can help you with the coding if I know what you want to do. Been a while since I used that equation for anything, so I don't quite understand how it's used. But I can tell you how to get certain variables from that equation in code.
Hmm try this line instead of all the clutter you've got going on, i've put the formula all into one line:
Code: x = (-b + sqrt(pow(b, 2) - (4 * a * c))) / (2 * a);
pow from math.h takes a double as input as well, not int.
Can't test as I have no idea of what kind of valid numbers would go into the equation however. You're last bit of the equation seems to have been right, just not sure where that 1 came in when you had it in your code... If it's not working, just reply with what happens at runtime with this. Or provide me with some valid numbers that can be used for this equation that I can fool around with.
Posts: 119
Threads: 18
Joined: Jul 2011
Reputation:
1
(01-18-2012, 08:53 AM)AceInfinity Wrote: You don't know about the square root methods in C++ or you're just having issues with it in any updated code so far?
What are you talking about C++ doesn't support Minus sign with a variable though???
Make sure your variable definitions have the "signed" attribute... and not "unsigned"
Edit: I may not be able to help with the equation, but I can help you with the coding if I know what you want to do. Been a while since I used that equation for anything, so I don't quite understand how it's used. But I can tell you how to get certain variables from that equation in code.
Hmm try this line instead of all the clutter you've got going on, i've put the formula all into one line:
Code: x = (-b + sqrt(pow(b, 2) - (4 * a * c))) / (2 * a);
pow from math.h takes a double as input as well, not int.
Can't test as I have no idea of what kind of valid numbers would go into the equation however. You're last bit of the equation seems to have been right, just not sure where that 1 came in when you had it in your code... If it's not working, just reply with what happens at runtime with this. Or provide me with some valid numbers that can be used for this equation that I can fool around with.
Actually, I used the one line code that you have quoted but was the same error.
I now realize that the problem is not in code, it is in the combination of number I input to the program
Thanks for your brilliant time that you gave me
[url=http://myurl.pk/BLgzA][/url]
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
(01-21-2012, 08:30 AM)Clones Wrote: Actually, I used the one line code that you have quoted but was the same error.
I now realize that the problem is not in code, it is in the combination of number I input to the program
Thanks for your brilliant time that you gave me
Hmm, if I was a quadratic equation formula expert i'd be able to help you further haha, sorry about that friend
I recognize the equation, but it's been some time since I last used it.
Posts: 119
Threads: 18
Joined: Jul 2011
Reputation:
1
(01-21-2012, 08:39 AM)AceInfinity Wrote: Hmm, if I was a quadratic equation formula expert i'd be able to help you further haha, sorry about that friend
I recognize the equation, but it's been some time since I last used it.
you helped alot.. No problem
[url=http://myurl.pk/BLgzA][/url]
|