12 Replies - 10753 Views - Last Post: 17 March 2009 - 02:20 PM Rate Topic: -----

#1 Weasel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 07

Quadratic formula

Post icon  Posted 22 September 2007 - 07:23 AM

I'm writing a program that will allow the user to input three variables and will then perform the quadratic equation on them. I found plenty of snippets that were very helpful, but I'm about as new to C++ as they come (and terrible at math to boot.)

	
	root = b * b - 4.0f * a * c;
									  



So, what I'm really looking for is some explanation as well as some help. In the above line of code, what is the purpose of using "4.0f" instead of just "4.0" or "4"?

Also, I need to write a function into the program that will check to make sure the value of "a" is not equal to zero. Specifically, if "a" is zero, the program should give the user one more try to enter the variables, and then terminate if "a = 0" a second time.

Here's what I've got for that portion:
#include <iostream>
#include <math.h>

using namespace std;

int main ()
{
	int a, b, c;
	
	cout << "Enter the values for a, b and c.";
	cin >> a >> b >> c;
	cout << endl;
	
	if (a == 0)
	   {
	   cout << "The value of 'a' cannot equal zero. Please reenter a, b and c." << endl;
	   cin >> a >> b >> c;
	   }
	   if (a == 0)
	   {  
		  cout << "The value of 'a' is still not valid. Please restart the program and try again." << endl; //If statement a<=0
	   }
		 system ("cls");
		 cout << a << char(253)<< " + " << b << "x + " << c << " = 0" << endl;
		 
	
	system ("pause");
	
	return 0;


A bit sloppy maybe? What do I need to do to get the program to terminate after the second if statement if "a" is still equal to zero?

Thanks in advance for taking the time to read all this. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Quadratic formula

#2 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Quadratic formula

Posted 22 September 2007 - 07:38 AM

Try
exit(0)

You'll have to include stdlib.h
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Quadratic formula

Posted 22 September 2007 - 07:42 AM

in your second if statement, you can add a return statement, which will end the program. I would suggest using return 1; just for good practice, return/exit codes can be useful in diagnosing problems :)
This also avoids the need of including more libraries.

As for the 4.0f, this is to designate a float value.
4 is assumed to be an integer
4.0 would (on most compilers) default to a double
4.0f the f tells the compiler to treat this as a float, and thus carry more precision and decimal places.


*your code may not be the best example, but things you defiantly did right:
-indenting is consistent
-you commented on an if statement, to remind yourself what is happening there
-you used int main instead of void main for your declaration (this is very good practice please continue to do so :))


system("pause") is not the best thing to use, but while your still new, i won't bother you about it too much.
there's also a missing } which i assume is just a copy and paste mistake.
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Quadratic formula

Posted 22 September 2007 - 07:55 AM

What exactly is the reason for int main being better?
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Quadratic formula

Posted 22 September 2007 - 08:11 AM

very glad you asked! (a lot of coders are afraid to ask this)

Basically because your code is not standalone... there is an OS which is using your code, *nix and Windows alike (i don't think i've ever grouped them like this before) call the code's entry point (etc) and wait for a return or exit code, to know that the operation is completed.

Basically the memory for this program can be leaked, corrupted, etc on successive runnings of the code, assuming it uses the same memory address, this can cause improper data readings, operations, and crashes of both the code and depending on what the code does/accesses, the system.

Also don't be fooled: ISO C++ and C99, both do not force an assumed int, if you use main() the int must be there.

*also strict C code will not allow void main, this is new to C++, but not standard: ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1

If you have any more questions you can consult the C/C++ FAQ from the man Bjarne Stroustrup himself.


wow i'm glad i did that paper on ISO and ANSI standards, it's nice to know what is legit and what others are just making up and claiming as legit.
Was This Post Helpful? 1
  • +
  • -

#6 Weasel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 07

Re: Quadratic formula

Posted 22 September 2007 - 04:30 PM

Awesome, thank you for all the help so far. I think I've almost got it up and running.


I look forward to spending more time around here getting to know everyone. B)

-Weasel
Was This Post Helpful? 0
  • +
  • -

#7 Weasel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 07

Re: Quadratic formula

Posted 23 September 2007 - 06:45 AM

Ok, the program is running just fine, I just have one last finesse issue to work out. What would be the most efficient way of changing the operators in the formula...For example:

It should normally print in the form ax˛ + bx + c = 0, but if b or c is negative then it should change to minus without printing a 1 as a coeffecient. That means if b = -2 then the equation should print in the
form ax˛ - bx + c = 0, not ax˛ + -bx + c = 0...

I'm sure it's something ridiculously easy that I'm just over looking.
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Quadratic formula

Posted 23 September 2007 - 06:52 AM

well you can check if b<0 or/and c<0 then make the print the way you want to: just printing the values of b and c and the ouput string there would be the minus sign(s). hope that helps ....give it a try

This post has been edited by PennyBoki: 23 September 2007 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 Weasel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 07

Re: Quadratic formula

Posted 23 September 2007 - 07:26 AM

Got it! Again, not sure if this is the cleanest way to do everything, but it works. :^: :D

Ta da!

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <complex>

using namespace std;

int main ()
{
	float a, b, c, x1, x2, root;
	complex<float> z;
	
	cout << "Enter the values for a, b and c.";
	cin >> a >> b >> c;
	cout << endl;
	
	if (a == 0)
	   {
	   cout << "The value of 'a' cannot equal zero. Please reenter a, b and c." << endl;
	   cin >> a >> b >> c;
	   }
	   if (a == 0)
	   {  
		  cout << "The value of 'a' is still not valid. Please restart the program and try again." << endl; //If statement a<=0
		 
		 system ("pause");
		 
		 exit (0); 
	   }
		 system ("cls");
		 
	if (b < 0) //change operator for b value
	   {
		 cout << a << char(253) << b << "x"; // -b
	   }
	   
	else 
	   {
		 cout <<a << char(253)<< "+" << b << "x"; // +b
	   }
	   
	if (c < 0) //change operator value for c
	   {
	   cout << c << "=0" << endl; // +c
	   }
	   
	else
	   {
	   cout << "+" << c << "=0" << endl; // -c
	   }
	   
		 
		 root = b * b - 4.0f * a * c;
										// b*b - 4ac
	if (root >= 0.0f)												   
		{
		
		root = sqrt(root);										 // real roots
		x1 = (-b + root) / (2.0f * a);
		x2 = (-b - root) / (2.0f * a);
		cout << "Real roots " << x1 << " and " << x2 << endl;
		cout << "Test " << a * x1 * x1 + b * x1 + c			  // test
			 << " and " << a * x2 * x2 + b * x2 + c << endl;
		}
		
	else
		{
															   // complex roots
		root = sqrt(-root) / (2.0f * a);
		x1 = -b / (2.0f * a);
		cout << "Complex roots " << x1 << " " << char(241) << " i" << root << endl;
		z = complex<float>(x1, root);
		cout << "Test " << a * z * z + b * z + c;				// test
		z = complex<float>(x1, -root);
		cout << " and " << a * z * z + b * z + c << endl;
		} 
		
	system ("pause");
	
	return 0;
} 


Thanks to horace in the snippets, giving me an example to work off of, and thank you all for the friendly input.
Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Quadratic formula

Posted 25 September 2007 - 10:14 AM

You may want to reconsider using system("pause") as William said above.
Was This Post Helpful? 0
  • +
  • -

#11 carolinaswamp   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-July 08

Re: Quadratic formula

Posted 17 March 2009 - 12:57 PM

View PostWilliam_Wilson, on 22 Sep, 2007 - 06:42 AM, said:

As for the 4.0f, this is to designate a float value.
4 is assumed to be an integer
4.0 would (on most compilers) default to a double
4.0f the f tells the compiler to treat this as a float, and thus carry more precision and decimal places.


Isn't it the other way around? Aren't Floats less precise than doubles?
Was This Post Helpful? 0
  • +
  • -

#12 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Quadratic formula

Posted 17 March 2009 - 01:52 PM

22 Sep, 2007 - 09:23 AM

Two year old thread, awesome post!
Was This Post Helpful? 0
  • +
  • -

#13 carolinaswamp   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-July 08

Re: Quadratic formula

Posted 17 March 2009 - 02:20 PM

View PostHyper, on 17 Mar, 2009 - 02:52 PM, said:

22 Sep, 2007 - 09:23 AM

Two year old thread, awesome post!


Haha, yes it is. I got to it through the sticky C++ FAQ at the top of this forum. Figured if its under FAQ it should at least be correct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1