Thread: Quad equation using fuctions problem

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    13

    Quad equation using fuctions problem

    Code:
    #include <iostream>
    using namespace std;
    void quadraticequation(float a, float b, float c);
    
    int main()
    {
    	int a,b,c;
    	
    	cout <<"a"<< endl;
    	cin>>a;
    	cout<<"b"<<endl;
    	cin>>b;
    	cout<<"c"<<endl;
    	cin>>c;
    	quadraticequation(a,b,c);
    	
    }
    
    
    	void quadraticequation(float a, float b, float c)
    {
    	if (((b*b)-4*a*c)<0)
    		{
    		cout<<"No real solution"<<endl;
    		}
    	
    	else if (((b*b)-4*a*c)=0 )
    		{
    		cout<<"Answer is"<<-b/2*a;
    		}
    	
    	 else if (((b*b)-4*a*c)>0)
    		 {
    		  cout<<"X1 ="<< -b+sqrt((b*b)-4*a*c)/2*a;
    		  
    		  cout<<"X2 ="<< -b-sqrt((b*b)-4*a*c)/2*a;
    		  }
    	
    	
    	
    }
    In the above code I'm to write out a function that solves a quadratic equation. I have one error that comes up but I dont know how to stop it because it seems okay to me(the beginner ). Does anyone know where I went wrong??

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Try to always tell us what the errors are when you get them. It seems to me the error comes from the fact that you have mistaken '==' for '='. It's in your first 'else if' statement.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Well, mathematically you should have
    Code:
    	  cout<<"X1 ="<< (-b+sqrt((b*b)-4*a*c))/(2*a);
    	  cout<<"X2 ="<< (-b-sqrt((b*b)-4*a*c))/(2*a);
    You could also save some typing by defining a variable for b*b-4*a*c.

    Edit: And also
    Code:
    		cout<<"Answer is"<<-b/(2*a);
    Last edited by robatino; 12-07-2006 at 11:09 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    58
    Code:
    #include <math.h>
    #include <iostream>
    using namespace std;
    void quadraticequation(float a, float b, float c);
    
    int main()
    {
    	int a,b,c;
    	
    	cout <<"a"<< endl;
    	cin>>a;
    	cout<<"b"<<endl;
    	cin>>b;
    	cout<<"c"<<endl;
    	cin>>c;
    	quadraticequation(a,b,c);
    	
    }
    
    
    	void quadraticequation(float a, float b, float c)
    {
    	if (((b*b)-4*a*c)<0)
    		{
    		cout<<"No real solution"<<endl;
    		}
    	
    	else if (((b*b)-4*a*c)==0 )
    		{
    		cout<<"Answer is"<<-b/(2*a);
    		}
    	
    	 else if (((b*b)-4*a*c)>0)
    		 {
    		  cout<<"X1 ="<< (-b+sqrt((b*b)-4*a*c))/2*a;
    		  
    		  cout<<"X2 ="<< (-b-sqrt((b*b)-4*a*c))/2*a;
    		  }
    	
    	
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    there are two more places with /2*a that should be fixed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    13
    Thanks guys, It works now. I really appreciate the help, I have an exam in this Wednesday and up till today I was clueless, still am but I think I know the basics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. OpenGL - look trough surface -nehe lesson
    By GanglyLamb in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 11:06 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. problem with an equation...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-16-2002, 12:47 PM