14 Replies - 2721 Views - Last Post: 02 July 2012 - 04:08 PM Rate Topic: -----

#1 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

quadratic equation using function

Posted 02 July 2012 - 05:56 AM

i m supposed to write a program to solve a quadratic equation. output should look like as follows:
coefficients a1=1,b=2,c=5,yield s1=-1+2i and s2=-1-2i
this is what i have done
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float a, float b, float c, float r1, float r2,float i1,float i2)
{
	float d=(b*b-4*a*c);
	if ( d<0)
	{
		return false;
		r1=r2=-b/(2*a);
		i1= sqrt(-d);
		i2= -sqrt(-d);
	}
	else 
	{
		i1=i2=0;
		r1=(-b+sqrt(d))/(2*a);
		r1=(-b-sqrt(d))/(2*a);
		return true;
	}
}
int main()
{
	float a,b,c,r1,r2,i1,i2;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(a,b,c,r1,r2,i1,i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
}

help me out what am i doing wrong here!! no output. just ask me to abort the operation

Is This A Good Question/Topic? 0
  • +

Replies To: quadratic equation using function

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: quadratic equation using function

Posted 02 July 2012 - 06:13 AM

First you should increase your compile warning levels. When I compile your code these are the warning I receive.

Quote

main.cpp||In function ‘bool QuadraticFormula(float, float, float, float, float, float, float)’:|
main.cpp|4|warning: no previous declaration for ‘bool QuadraticFormula(float, float, float, float, float, float, float)’ [-Wmissing-declarations]|
main.cpp|4|warning: parameter ‘r1’ set but not used [-Wunused-but-set-parameter]|
main.cpp|4|warning: parameter ‘i1’ set but not used [-Wunused-but-set-parameter]|
main.cpp||In function ‘int main()’:|
main.cpp|35|warning: ‘r1’ may be used uninitialized in this function [-Wuninitialized]|
main.cpp|35|warning: ‘r2’ may be used uninitialized in this function [-Wuninitialized]|
main.cpp|35|warning: ‘i1’ may be used uninitialized in this function [-Wuninitialized]|
main.cpp|35|warning: ‘i2’ may be used uninitialized in this function [-Wuninitialized]|


Next look at the placement of your return in the following snippet:
	if ( d<0)
	{
		return false;
		r1=r2=-b/(2*a);

When your program encounters the return it immediately returns to the calling function, it doesn't process any remaining lines.

Next since you are passing all of your variables by value the changes you make to these variables in your function will be lost when the function returns. So since you don't initialize your variables, when you try to output these variables in main they don't contain valid data.

You may want to review how to pass variables into functions by reference. The two links in my signature will probably help explain how to properly use functions.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: quadratic equation using function

Posted 02 July 2012 - 06:15 AM

How are you compiling it? It seems to run fine for me when compiled with G++. As Jim mentions though, there is many potential logic errors in the program.

This post has been edited by Aphex19: 02 July 2012 - 06:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 02:30 PM

okay i did whatever you said. changed position for
return false
gave pointers a good study but i still dont figure out how to implement this program. any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: quadratic equation using function

Posted 02 July 2012 - 02:41 PM

Post your current code.

Did you study the function tutorials contained in my signature?

Did you try passing the last four parameters by reference?

Jim
Was This Post Helpful? 0
  • +
  • -

#6 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 02:49 PM

thanks jim for such prompt reply. yes i did study tutorials in your signature. i had developed a bit of understanding for reference and pointers.the thing i dont understand how to implement them in my current code.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: quadratic equation using function

Posted 02 July 2012 - 02:53 PM

Okay, let's start by posting your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 03:04 PM

okay here is my current code
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float a, float b, float c, float r1, float r2,float i1,float i2)
{
	float d=(b*b-4*a*c);
	if ( d<0)
	{
		r1=r2=-b/(2*a);
		i1= sqrt(-d);
		i2= -sqrt(-d);
		return false;
	}
	else 
	{
		i1=i2=0;
		r1=(-b+sqrt(d))/(2*a);
		r1=(-b-sqrt(d))/(2*a);
		return true;
	}
}
int main()
{
	float a=0.0f,b=0.0f,c=0.0f,r1=0.0f,r2=0.0f,i1=0.0f,i2=0.0f;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(a,b,c,r1,r2,i1,i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	cin.get();
	return 0;
}

modifications i did was changing position for return false and initializing a,b,c,i1,i2,r1,r2.
only help i need from you is a little hint how to implement using pointers.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3457
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: quadratic equation using function

Posted 02 July 2012 - 03:12 PM

Thanks for posting your updated code. Now reread the very beginning Part II of Jim's tutorial. Notice how squareIt() works to pass an in-out parameter? In your case all you need is an out parameter and it should work the same.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: quadratic equation using function

Posted 02 July 2012 - 03:39 PM

Here is another tutorial that also explains passing by value and by reference. Functions II

Jim
Was This Post Helpful? 1
  • +
  • -

#11 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 03:40 PM

okay i read it again. i understood how squareIt() works. thing is i dont understand why my output is always 0+0i. i did tried to implement using pointers as:
bool QuadraticFormula(float *a, float *b, float *c, float r1, float r2,float i1,float i2)

and then called this function by:
if(QuadraticFormula(&a,&b,&c,r1,r2,i1,i2))

in the int main(). what i still dont understand here how pointers are useful here they dont seem to do anything.and one thing i forgot to mention my function:
bool QuadraticFormula(float *a, float *b, float *c, float r1, float r2,float i1,float i2)

should return false when it contains imaginary part otherwise return true
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3457
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: quadratic equation using function

Posted 02 July 2012 - 03:48 PM

It is your r and i variables that you would want to be passed by reference. You'll want to retain a, b, and c to be pass by value.
Was This Post Helpful? 1
  • +
  • -

#13 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 03:48 PM

here is my modified code.
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float *a, float *b, float *c, float r1, float r2,float i1,float i2)
{
	float d=((*B)/>*(*B)/>-4*(*a)*(*c));
	if ( d<0)
	{
		r1=r2=-*b/(2*(*a));
		i1= sqrt(-d);
		i2= -sqrt(-d);
		return false;
	}
	else 
	{
		i1=i2=0;
		r1=(-*b+sqrt(d))/(2*(*a));
		r1=(-*b-sqrt(d))/(2*(*a));
		return true;
	}
}
int main()
{
	float a=0.0f,b=0.0f,c=0.0f,r1=0.0f,r2=0.0f,i1=0.0f,i2=0.0f;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(&a,&b,&c,r1,r2,i1,i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	cin.get();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: quadratic equation using function

Posted 02 July 2012 - 03:53 PM

You are passing the wrong variables by pointer. You are trying to compute the value of r1,r2,i1, and i2 inside your function. These are the variables you should either pass by pointer or reference. Since this is a C++ I recommend passing by reference instead of by pointer. In the following snippet:

#include <iostream>

using namespace std;
void someFunctionByValue(int a);
void someFunctionByReferenc(int &a);

int main()
{

   int value = 1;

   cout << "Value before functions: " << value << endl;

   someFunctionByValue(value);
   cout << "Value after ByValue function: " << value << endl;

   someFunctionByReference(value)
   cout << "Value after ByReference function: " << value << endl;

   return 0;
}

void someFunctionByValue(int value)
{
   value = 10;
   cout << "Value in Function By Value: " << val << endl;
}

void someFunctionByReference(int &val)
{
   value = 10;
   cout << "Value in Function By Reference: " << value << endl;
}



This program demonstrates the differences of pass by value and pass by reference. Notice that other than the function prototypes these two functions are identical yet in main() the variable value changed only after the call by reference function.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: quadratic equation using function

Posted 02 July 2012 - 04:08 PM

thanks it worked like charm. just wanted to kno what bool QuadraticFormula(float a, float b, float c, float* r1, float* r2,float* i1,float* i2) actually did? here is my final code.
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float a, float b, float c, float* r1, float* r2,float* i1,float* i2)
{
	float d=(b*b-4*a*c);
	if ( d<0)
	{
		*r1=*r2=-b/(2*(a));
		*i1= sqrt(-d)/(2*a);
		*i2= -sqrt(-d)/(2*a);
		return false;
	}
	else 
	{
		*i1=*i2=0;
		*r1=(-b+sqrt(d))/(2*(a));
		*r2=(-b-sqrt(d))/(2*(a));
		return true;
	}
}
int main()
{
	float a=0.0f,b=0.0f,c=0.0f,r1=0.0f,r2=0.0f,i1=0.0f,i2=0.0f;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(a,b,c,&r1,&r2,&i1,&i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+"<<i1<<"i"<<endl;
		cout<<"s2="<<r2<<"+("<<i2<<")i"<<endl;	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+"<<i1<<"i"<<endl;
		cout<<"s2="<<r2<<"+("<<i2<<")i"<<endl;
	}
	cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1