-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_task_2_completing_the_square_.c
122 lines (87 loc) · 3.23 KB
/
sub_task_2_completing_the_square_.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Group Members
// David Henry - 1007604
// Rockell London - 1037481
#include <stdio.h>
#include <math.h>
// ax^2 + bx + c = 0
int a, b, c;
double newa, newb, newc, d, e, f, right_side, root1, root2;
int main()
{
printf("COMPLETING THE SQUARE\n");
//get the values
printf("\nEnter A !=0 :");
scanf("%d", &a);
printf("\nEnter B:");
scanf("%d", &b);
printf("\nEnter C:");
scanf("%d", &c);
if (a == 1)
{
// when c is moved to rhs its polarity changes, we do this by *-1 since +*+=+, +*-=- ,-*-=+
d = c *-1;
// half of b squared
e = pow(b / 2, 2);
right_side = d + e;
//half b
f = b / 2;
// + and - roots
root1 = sqrt(right_side) - f;
root2 = -sqrt(right_side) - f;
// JUST PRINTING DOWN BELOW
printf("\n(%d)x^2 + (%d)x + (%d) = 0\n\n", a, b, c);
printf("Move constant to the RHS -->");
printf("\n(%d)x^2 + (%d)x + __ = (%d)\n\n", a, b, c *-1);
printf("Take half of b, square it, then add to both sides\n");
printf("(%d)x^2 + (%d)x + __ = (%0.2f)\n", a, b, d);
printf("(%d)x^2 + (%d)x + (%0.2f) = (%0.2f) + (%0.2f)\n\n", a, b, e, d, e);
printf("Rewrite the LHS as a perfect square\n");
printf("((%dx + (%0.2f))^2 = (%0.2f)\n\n", a, f, (right_side));
printf("Take the square root of both sides\n");
printf("sqrt (((%dx + (%0.2f))^2) = sqrt (%0.2f)\n\n", a, f, (right_side));
printf("Isolate and solve for X\n");
printf("(%d)x + (%0.2f) = +- (%0.2f)\n\n", a, f, sqrt(right_side));
printf("(%d)x = + (%0.2f) - (%.2f)\n\n", a, sqrt(right_side), f);
printf("(%d)x = - (%0.2f) - (%.2f)\n\n", a, sqrt(right_side), f);
printf("First root is: %0.2f\n\n", root1);
printf("Second root is: %0.2f\n\n", root2);
}
else if (a > 1)
{
// since a is more than 1 we divide everything by a
newa = (double) a / a;
newb = (double) b / a;
newc = (double) c / a;
// when c is moved to rhs its polarity changes, we do this by *-1 since +*+=+, +*-=- ,-*-=+
d = newc *-1;
// half of b squared
e = pow(newb / 2, 2);
// calc rhs value
right_side = d + e;
//half b
f = newb / 2;
// + and - roots
root1 = sqrt(right_side) - f;
root2 = -sqrt(right_side) - f;
// JUST PRINTING DOWN BELOW
printf("\n(%d)x^2 + (%d)x + (%d) = 0\n\n", a, b, c);
printf("Move constant to the RHS -->");
printf("\n(%d)x^2 + (%d)x + __ = (%d)\n\n", a, b, c *-1);
printf("\nA != 1, so divide by A, %d", a);
printf("\n(%d/a)x^2 + (%d/a)x + __ = (%d/a)\n\n", a, b, c *-1);
printf("Take half of b, square it, then add to both sides\n");
printf("(%0.0f)x^2 + (%0.2f)x + __ = (%0.2f)\n", newa, newb, d);
printf("(%0.0f)x^2 + (%0.2f)x + (%0.2f) = (%0.2f) + (%0.2f)\n\n", newa, newb, e, d, e);
printf("Rewrite the LHS as a perfect square\n");
printf("(((%0.0f)x + (%0.2f))^2 = (%0.2f)\n\n", newa, f, (right_side));
printf("Take the square root of both sides\n");
printf("sqrt (((%0.0fx + (%0.2f))^2) = sqrt(%0.2f)\n\n", newa, f, (right_side));
printf("Isolate and solve for X\n");
printf("(%0.0f)x + (%0.2f) = +- (%0.2f)\n\n", newa, f, sqrt(right_side));
printf("(%0.0f)x = + (%0.2f) - (%.2f)\n\n", newa, sqrt(right_side), f);
printf("(%0.0f)x = - (%0.2f) - (%.2f)\n\n", newa, sqrt(right_side), f);
printf("First root is: %0.2f\n\n", root1);
printf("Second root is: %0.2f\n\n", root2);
}
return 0;
}