quadratic formula via the non-void function

Apr 25, 2013 at 3:44am
Your main function should acquire the values for a, b, and c and report the results of the function call ....where have I went wrong please help! thanks
Last edited on Apr 25, 2013 at 6:05am
Apr 25, 2013 at 3:56am
You never initialize the variable x or do not have any method to assign it a value.
Apr 25, 2013 at 4:02am
the assignment says to only acquire values for a b c not x idk what to do
Apr 25, 2013 at 4:08am
Well, if for example you want to display a list of results from the calls f(x_o) to f(x_n), then you can use a for loop to pass varying values of x:

1
2
3
4
const int max = 10;   //Arbitrary maximum number
for(int x= 0; x< max; x++){
   std::cout << answer(a,b,c,x) << '\n';
}


However, this all depends on how you are supposed to display the return values.
Apr 25, 2013 at 6:06am
ok I was completly wrong from the start here is what I have now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;



bool quad(int, int, int, double, double);
	
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
int a,b,c;
double r1, r2;

cout << "enter a number";
cin >> a;
cout << "enter another number";
cin >> b;
cout << "enter the last number";
cin >> c;
if(quad(a,b,c,r1,r2))
{
	cout << "solution found:"<< r1 << "," << r2 << endl;
}
else
{
	cout << " no solution!\n";
}
return 0;
}
bool quad( int a, int b, int c, double r1, double r2)
{
	bool valid = true;
	if(b*b-4*a*c>0)
	{
		valid = false;
	}
	else
	{
		r1=(b + sqrt * b-4*a*c))/(2*a);
		r2=(b - sqrt *b-4*a*c))/(2*a);
	}
	return valid;
}
Last edited on Apr 25, 2013 at 6:07am
Apr 25, 2013 at 6:11am
bool quad( int a, int b, int c, double r1, double r2)

In your main function, you wanted to print out the new values of r1 and r2 after passing them to the quad function. However, you are passing by value, which means that copies of r1 and r2 are modified, not the original variables. Pass r1 and r2 by reference instead.

On a side note, your else statement in your main function is not entirely accurate. It should be "No real solution." :)

Edit:
1
2
r1=(b + sqrt * b-4*a*c))/(2*a);
r2=(b - sqr t *b-4*a*c))/(2*a);


You made a typo with the asterisks after "sqrt," the second b should be squared, and there is an extra space.
Last edited on Apr 25, 2013 at 6:15am
Apr 25, 2013 at 6:15am
what should I do to fix this?
Apr 25, 2013 at 6:19am
Have you learned how to use reference (&) variables? An example of a function that passes by reference is void funct(char &ref){/*...*/}

Also, I noticed this bit:
1
2
3
4
if(b*b-4*a*c>0)
	{
		valid = false;
	}


You may want to check your condition.
Apr 25, 2013 at 6:26am
these are the errors im getting
1>c:\users\cameron\documents\visual studio 2010\projects\cdfm21\cdfm21\cdfm21.cpp(34): error C2668: 'pow' : ambiguous call to overloaded function
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or       'double pow(double,int)'
1>          while trying to match the argument list '(int, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Apr 25, 2013 at 6:29am
May you post your updated code?
Apr 25, 2013 at 6:38am

I cannot use a void function in the assignmet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;



bool quad(int, int, int, double, double);
	
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
int a,b,c;
double r1, r2;

cout << "enter a number";
cin >> a;
cout << "enter another number";
cin >> b;
cout << "enter the last number";
cin >> c;

if(quad(a,b,c,r1,r2))
{
	cout << "solution found:"<< r1 << "," << r2 << endl;
}
else
{
	cout << " no real solution!\n";
}
return 0;
}
bool quad( int a, int b, int c, double r1, double r2)
{
	bool valid = true;
	if( b*b-4*a*c > 0)
	{
		valid = false;
	}
	else
	{
		r1=(-b + sqrt * b-4*a*c))/(2*a);
		r2=(-b - sqrt *b-4*a*c))/(2*a);
	}
	return valid;
}
Apr 25, 2013 at 6:53am
1
2
3
4
5
6
7
8
9
	if( b*b-4*a*c > 0)
	{
		valid = false;
	}
	else
	{
		r1=(-b + sqrt * b-4*a*c))/(2*a);
		r2=(-b - sqrt *b-4*a*c))/(2*a);
	}


I am not sure about your "ambiguous pow called" issue, but perhaps it stems from the assignment lines. "sqrt" is a function, so I believe you meant sqrt(b*b-4*a*c).

Also, please address your if condition. The function will return false even if a real solution exists.
Topic archived. No new replies allowed.