Function & String help

How would I go about defining a function to compute the roots of a QE when supplied with 3 coefficients?
Last edited on
Use the quadratic formula.
well, this is my code
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
48
49
50
51
52
53
54
55
#include <cmath>
#include <iostream>

    double QuadRoot (double a, double b, double c);
    double a, b, c, D, D2, D3;

int main () { 
 
    cout << "Randall Hall \t CSCE 1020 Lab Assignment #10 \t randallhall2@my.unt.edu" << endl;

    cout << "\n\n";

    do {
       cout << "Please enter a number between -100.0 and 100.0 for x^2: "; 
       cin >> a;
       } while (a < -100.0 || a > 100.0);

    cout << "a has the value: " << a << ".";
    cout << endl;
    
    do {
       cout << "Please enter a number between -100.0 and 100.0 for x: "; 
       cin >> b;
       } while (b < -100.0 || b > 100.0);
    cout << "b has the value: " <<  b << ".";
    cout << endl;
    
    do {
       cout << "Please enter a number between -100.0 and 100.0 for the constant: "; 
       cin >> c;
       } while (c < -100.0 || c > 100.0);
    cout << "c has the value: " << c << ".";

    cout << "\n\n";
    
    D = (b*b)-(4*a*c);
   
    D2 = (-b+sqrt(D))/(2*a);

    D3 = (-b-sqrt(D))/(2*a);
    
    if (D < 0) { 
      cout << "The determinant is less than 0 so the roots are imaginary/not real" << endl;
               };      
    if (D == 0) {
      cout << "The determinant is 0 so there will only be one root, the root is " << D2 << endl << endl;
                }; 
    if (D > 0) {
      cout << "The determinant is greater than 0 so there will be two roots, the roots are " << D2 << " and " << D3 << endl << endl;
               };
    
      
             
   return 0;
}
, and it doesn't feel like I have a function defined
And I have no clue how to "correctly" define a function.
rsh0117 wrote:
And I have no clue how to "correctly" define a function.


That's not quite true, as you just posted the code for a correctly defined function int main() {/*...*/} :)

Other functions are defined the same way:
1
2
3
4
5
6
7
8
returnType function_name 
{
     //declare whatever variables

    //do stuff

    return variable  //variable type must match returnType
}
so
1
2
3
4
   return Type double QuadRoot (double a, double b, double c)
   {
 QuadRoot = -b+sqrt((b*b)-(4*a*c))/(2*a)
   }

would be correct?
That may have been my fault. When I wrote returnType, I meant the type of value returned, not the literal word.
1
2
3
4
int main()  //returns an int value
double QuadRoot //returns a double value
string StringFunction  //returns a string value
etc...


and the actual return statement must be inside the function body
1
2
3
4
5
6
7
double QuadRoot ( double a, double b, double c)
{
     double D;
     D= -b + sqrt((b*b) - (4*a*c))/(2*a);
     return D;  //the variable D is of type double and
                //it matches the return type of the function
}
Use a void function and use the address of operator '&' to change D, D2, and D3 then continue with your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void quad(int &, int &, int &);

int main()
{
  //instructions
  
  quad(D, D2, D3);

  if
  else if  //I'm not going to bother wasting time writing this again for you
  else
}

void quad(int &x, int &y, int &z)  //actual variable names are unimportant
{
}


Topic archived. No new replies allowed.