begginger function help

Hello guys. So I'mm having a little trouble. I was given code (which I will post below) that was given to me. My assignment was to turn the code in the main body into about two or three functions. I will post the changes that I made as well. here is the orginal code with the directions I was given

Directions
This program allows the user to repeatedly solve quadratic equations.
For each, it asks the user for the coefficients (a, b, and c) and then solves
for and prints the real number solutions (if any). After solving a problem
the program then asks the user whether to do another.

This version uses smart numerical analysis techniques to find more accurate
solutions in cases where a straightforward use of the quadratic formula
would not work or would not give very accurate answers (because of
"catastrophic cancellation"). There are two techniques used: the coefficients
are first normalized. Then, in cases where there are 2 solutions, only one
solution, call it x1, is calculated using the quadratic formula, namely the
solution in which cancellation cannot occur. Then the second solution, call it
x2, is calculated using x2 = c / (a * x1).



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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>     // Needed for the sqrt function.
using namespace std;


int main(void)
   {
   char Reply;
   int NumSolutions;
   float a, b, c, ANorm, BNorm, CNorm, x1, x2, Discriminant, Root, Largest;
   
   do
   {
      cout << "Quadratic Equation Solver" << endl << endl;
      cout << "Enter the value of coefficient a: ";
      cin >> a;
      cout << "Enter the value of coefficient b: ";
      cin >> b;
      cout << "Enter the value of coefficient c: ";
      cin >> c;

      if (fabs(a) > fabs(b))
         Largest = fabs(a);
      else
         Largest = fabs(b);
      
      if (fabs(c) > Largest)
         Largest = fabs(c);

      ANorm = a / Largest;
      BNorm = b / Largest;
      CNorm = c / Largest;
      Discriminant = BNorm * BNorm - 4.0f * ANorm * CNorm;
   
      if (Discriminant < 0)
         NumSolutions = 0;
      else if (Discriminant == 0)
         {
         NumSolutions= 1;
         x1 = -BNorm / (2.0f * ANorm);
         }
      else   // Discriminant must be positive
         {
         NumSolutions = 2;
         Root = sqrt(Discriminant);
         if (-BNorm > 0)
            x1 = (-BNorm + Root) / (2.0f * ANorm);
         else
            x1 = (-BNorm - Root) / (2.0f * ANorm);
            
         x2 = CNorm / (ANorm * x1);
         }

      if (NumSolutions == 0)
         cout << "No real solutions" << endl;
      else if (NumSolutions == 1)
         cout << "One real solution: " << x1 << endl;
      else
         cout << "Two real solutions: " << x1 << " and " << x2 << endl;  

      cout << "Solve another (y/n)? ";
      cin >> Reply;
   } while (Reply == 'y');

   return 0;
   }


NOW here is my code that I changed. but Im having alot of problems with it. could anyone help me out?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cmath>     // Needed for the sqrt function.
using namespace std;

float x1 (float & a, float & b, float & c);
float x2 (float x2);

int main(void)
   {
   char Reply;
   int NumSolutions;
   
   
double x1 (float a, float b,float c);
double x2 (double x2);
  
      cout << "Solve another (y/n)? ";
      cin >> Reply;
    while (Reply == 'y');
	{

   return 0;
   }

double x1 (float & a, float & b, float & c)
{
 float a,b,c;
  
      cout << "Quadratic Equation Solver" << endl << endl; // make functions with & symbol 
      cout << "Enter the value of coefficient a: ";
      cin >> a;
      cout << "Enter the value of coefficient b: ";
      cin >> b;
      cout << "Enter the value of coefficient c: ";
      cin >> c;
	  
}


double x2 (int x2)
{
	float ANorm, BNorm, CNorm, x1, x2, Discriminant, Root, Largest;
 if (fabs(a) > fabs(b)) // make second function
         Largest = fabs(a);
      else
         Largest = fabs(b);
      
      if (fabs(c) > Largest)
         Largest = fabs(c);

      ANorm = a / Largest;
      BNorm = b / Largest;
      CNorm = c / Largest;
      Discriminant = BNorm * BNorm - 4.0f * ANorm * CNorm; //possibly make a function or inline function
    
      if (Discriminant < 0)
         NumSolutions = 0;
      else if (Discriminant == 0)
         {
         NumSolutions= 1;
         x1 = -BNorm / (2.0f * ANorm);
         }
      else   // Discriminant must be positive
         {
         NumSolutions = 2;
         Root = sqrt(Discriminant);
         if (-BNorm > 0)
            x1 = (-BNorm + Root) / (2.0f * ANorm);
         else
            x1 = (-BNorm - Root) / (2.0f * ANorm);
            
         x2 = CNorm / (ANorm * x1);
         }

      if (NumSolutions == 0)
         cout << "No real solutions" << endl;
      else if (NumSolutions == 1)
         cout << "One real solution: " << x1 << endl;
      else
         cout << "Two real solutions: " << x1 << " and " << x2 << endl; // end second function 


I don't know what code I have to change to make this work. Any help would be appreciated. Thank you.
Last edited on
first of all i liked the documentation about that normalizing :d but,
DO NOT EVER USE x1 AND x2 AS FUNCTIONS AGAIN!
ok i put all variables to global ones and used some conditional operators (condition?ifTrue:ifFalse) and i USED DESCRIPTIVE FUNCTIONNAMES.
i put the output straight after the calculation so NumSolution was removed. and it became this: (tested it and it works nicely)
and also the call you make to your functions is illegal.
don't put your function inside your main function
if the user answer y then you return 0 (=end program)
your mistake there is that you just copied
1
2
3
4
while (Reply == 'y');

   return 0;
   }
but actually the main structure of that was:
1
2
3
do {
...
} while(Reply == 'y');

you not even closing the x1 and main function
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
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include <iostream>
#include <cmath>     // Needed for the sqrt function.
using namespace std;

void input() ;
void normalize(float a,float b,float c) ;
void solution(float save_a,float save_c) ;
float a, b, c, x1, x2, Discriminant, Root, Largest;
int NumSolutions;

int main(void) {
	char Reply;
   do {
   input();
   float save_a=a,save_c=c;
   normalize(a,b,c);
   solution(save_a,save_c);
   cout << "Solve another (y/n)? ";
   cin >> Reply;
	} while (Reply=='y');
}

void input() {
	cout << "Quadratic Equation Solver" << endl << endl;
      cout << "Enter the value of coefficient a: ";
      cin >> a;
      cout << "Enter the value of coefficient b: ";
      cin >> b;
      cout << "Enter the value of coefficient c: ";
      cin >> c;
	  return;
}
void normalize(float a,float b,float c) {
		fabs(a) > fabs(b)?Largest = fabs(a):Largest = fabs(b);
		fabs(c) > Largest?Largest = fabs(c):Largest=Largest;

			a = a / Largest;
			b = b / Largest;
			c = c / Largest;
}

void solution(float save_a,float save_c) {
	Discriminant = pow(b,2) - 4.0f *a *c;
	if (Discriminant < 0)
        cout << "No real solutions" << endl;
      else if (Discriminant == 0)
         {
         x1 = -b / (2.0f * a);
		 cout << "One real solution: " << x1 << endl;
         }
      else   // Discriminant must be positive
         {
         Root = sqrt(Discriminant);
         if (-b> 0)
            x1 = (-b + Root) / (2.0f * a);
         else
            x1 = (-b- Root) / (2.0f * a);
            
         x2 = save_c / (save_a * x1);
         cout << "Two real solutions: " << x1 << " and " << x2 << endl;  
         }
}
Last edited on
making global variables for this is okay? because I was always taught to stay away from them. If possible.
Topic archived. No new replies allowed.