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
81
82
83
84
85
86
87
88
89
90
91
| // rootCalculator.cpp: computes roots of linear and quadratic equations
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
// prototypes of functions
float linRoot (float, float);
void quadRoot (float, float, float);
int main()
{
// local variables
int eqType;
float a, b, c;
// get equation type
cout << " Press 1 to choose linear equation or 2 to choose quadratic equation: ";
cin >> eqType;
// the actions for when linear equation is chosen
if (eqType == 1)
{
cout << " You have chosen linear equation. For bx+c=0," << endl;
// get the values of b and c
cout << " type in the the value of b: ";
cin >> b;
cout << " type in the the value of c: ";
cin >> c;
// call the function linRoot and display the result
cout << " x= " << linRoot (b, c);
};
// the actions when quadratic equation is chosen
if (eqType == 2)
{
cout << " You have chosen quadratic equation. For a(x^2)+bx+c=0," << endl;
//get the values for a, b and c
cout << " Type in the the value of a: ";
cin >> a;
cout << " Type in the the value of b: ";
cin >> b;
cout << " Type in the the value of c: ";
cin >> c;
// call the function quadRoot and it will display the results
quadRoot (a, b, c);
};
return 0;
}
// linRoot function description
float linRoot (float k, float l)
{
return ( (-l)/k );
}
// quadRoot function description
void quadRoot ( float m, float n, float o)
{
float d; // local variable (discriminant)
d = (n * n) - 4 * m * o; // compute the value if discriminant
if (d >= 0) // when discriminant is bigger than or equal to zero, the instructions within the following brackets are performed
{
float x1, x2; // local variables
x1 = (-n - sqrt(d)) / (2 * m); // first root computation
x2 = (-n + sqrt(d)) / (2 * m); // second root computation
//Display the results
cout << "x1= " << x1 << endl;
cout << "x2= " << x2 << endl;
}
if (d <0 ) // when discriminant is smaller than zero, the instructions within the following brackets are performed
{
cout << " No real roots!" ;
}
} |