|
|
|
|
4
|
![main2.png](images/main2.png)
|
4
|
![main2.png](images/main2.png)
|
5
|
![main4.png](images/main4.png)
|
5
|
![main4.png](images/main4.png)
|
6
|
|
6
|
|
|
|
7
|
+[verano2016 - Coralys]
|
7
|
|
8
|
|
8
|
-Arithmetic expressions are an essential part of almost any algorithm that solves a useful problem. Therefore, a basic skill in any computer programming language is to implement arithmetic expressions correctly. In this laboratory experience you will practice the implementation of arithmetic expressions in C++ by writing equations for the quadratic formula and complete the code for a game in which a frog jumps from leaf to leaf.
|
|
|
|
|
9
|
+Arithmetic expressions are an essential part of almost any algorithm that solves a useful problem. Therefore, a basic skill in any computer programming language is to implement arithmetic expressions correctly. In this laboratory experience you will practice the implementation of arithmetic expressions in C++ by writing equations for the quadratic formula and completing the code for a game in which a frog jumps from leaf to leaf.
|
9
|
|
10
|
|
10
|
-##Objectives:
|
|
|
|
|
11
|
+## Objectives:
|
11
|
|
12
|
|
12
|
-1. To implement arithmetic expressions in C++ to produce graphs.
|
|
|
13
|
-2. To use constants adequately.
|
|
|
|
|
13
|
+1. To implement arithmetic expressions in C++ to produce the graphs that will define the trajectory of the frog's jump in the game.
|
|
|
14
|
+2. To use constant variables adequately.
|
14
|
3. To define variables using adequate data types.
|
15
|
3. To define variables using adequate data types.
|
15
|
4. To cast a data value to another type when necessary.
|
16
|
4. To cast a data value to another type when necessary.
|
16
|
|
17
|
|
17
|
|
18
|
|
18
|
-##Pre-Lab:
|
|
|
|
|
19
|
+## Pre-Lab:
|
19
|
|
20
|
|
20
|
Before you get to the laboratory you should have:
|
21
|
Before you get to the laboratory you should have:
|
21
|
|
22
|
|
22
|
1. Reviewed the following concepts:
|
23
|
1. Reviewed the following concepts:
|
23
|
|
24
|
|
24
|
- a. implementing arithmetic expressions in C++.
|
|
|
|
|
25
|
+ a. Implementing arithmetic expressions in C++.
|
25
|
|
26
|
|
26
|
- b. native data types in C++ (int, float, double)
|
|
|
|
|
27
|
+ b. Basic data types in C++ (int, float, double)
|
27
|
|
28
|
|
28
|
- c. using "type casting" to cast the value of variables to other data types within expressions.
|
|
|
|
|
29
|
+ c. Using "type casting" to cast the value of variables to other data types within expressions.
|
29
|
|
30
|
|
30
|
- d. using arithmetic functions and constants from the `cmath` library.
|
|
|
|
|
31
|
+ d. Using arithmetic functions and constants from the `cmath` library.
|
31
|
|
32
|
|
32
|
- e. quadratic equations and their graphs.
|
|
|
|
|
33
|
+ e. Quadratic equations and their graphs.
|
33
|
|
34
|
|
34
|
2. Studied the concepts and instructions for the laboratory session.
|
35
|
2. Studied the concepts and instructions for the laboratory session.
|
35
|
|
36
|
|
36
|
-3. Taken the Pre-Lab quiz that can be found in Moodle.
|
|
|
|
|
37
|
+3. Taken the Pre-Lab quiz available in Moodle.
|
37
|
|
38
|
|
38
|
|
39
|
|
39
|
|
40
|
|
|
|
|
|
42
|
---
|
43
|
---
|
43
|
|
44
|
|
44
|
|
45
|
|
45
|
-##Quadratic Formula
|
|
|
|
|
46
|
+## Quadratic Formula
|
46
|
|
47
|
|
47
|
-A *quadratic equation* has a form
|
|
|
|
|
48
|
+A *quadratic equation* has the form
|
48
|
|
49
|
|
49
|
|
50
|
|
50
|
$$y = a x^2+ b x + c,$$
|
51
|
$$y = a x^2+ b x + c,$$
|
51
|
|
52
|
|
52
|
-where $$a, b, c$$ are real numbers and $$a\not=0$$. The graph of a quadratic equation is a *parabola* that opens up if $$a > 0$$ and opens dawn if $$a < 0$$.
|
|
|
|
|
53
|
+where $$a, b, c$$ are real numbers and $$a\not=0$$. The graph of a quadratic equation is a *parabola* that opens up if $$a > 0$$ and opens down if $$a < 0$$.
|
53
|
|
54
|
|
54
|
A graph intersects the $$x$$-axis when $$ y = 0 $$ . Therefore, if a parabola intersects the $$x$$-axis, the intersects are given by the real solutions to the equation
|
55
|
A graph intersects the $$x$$-axis when $$ y = 0 $$ . Therefore, if a parabola intersects the $$x$$-axis, the intersects are given by the real solutions to the equation
|
55
|
|
56
|
|
56
|
-
|
|
|
57
|
-
|
|
|
58
|
$$0 = a x^2 + b x + c.$$
|
57
|
$$0 = a x^2 + b x + c.$$
|
59
|
|
58
|
|
60
|
-The solutions to the previous equation can be obtained using the *quadratic formula*:
|
|
|
61
|
|
59
|
|
62
|
|
60
|
|
|
|
61
|
+The solutions to the previous equation can be obtained using the *quadratic formula*:
|
63
|
|
62
|
|
64
|
$$x=\frac{-b±\sqrt{b^2-4ac}}{2a}.$$
|
63
|
$$x=\frac{-b±\sqrt{b^2-4ac}}{2a}.$$
|
65
|
|
64
|
|
66
|
-Note that if the *discriminant* $$b^2-4ac$$ of the quadratic formula is negative, the values of $$x$$ are complex numbers and are not plotted in the Cartesian plane. Therefore, if the discriminant is negative, the parabola does not intersect the $$x$$-axis. If the discriminant is equal to $$0$$, then the parabola intersects the $$x$$-axis in only one point (only the vertex touches the $$x$$-axis).
|
|
|
|
|
65
|
+Note that if the *discriminant* $$b^2-4ac$$ of the quadratic formula is negative, the values of $$x$$ are complex numbers and are not plotted in the Cartesian plane. Therefore, if the discriminant is negative, the parabola does not intersect the $$x$$-axis. If the discriminant is equal to $$0$$, then the parabola intersects the $$x$$-axis in only one point (only the vertex touches the $$x$$-axis).
|
67
|
|
66
|
|
68
|
If the discriminant is positive, the quadratic formula gives two solutions to the equation $$0 = a x^2 + b x + c$$ and these solutions are the intersects in the $$x$$-axis. For example, suppose that the quadratic formula gives two values
|
67
|
If the discriminant is positive, the quadratic formula gives two solutions to the equation $$0 = a x^2 + b x + c$$ and these solutions are the intersects in the $$x$$-axis. For example, suppose that the quadratic formula gives two values
|
69
|
|
68
|
|
70
|
-
|
|
|
71
|
-
|
|
|
72
|
$$ x = x_1 $$
|
69
|
$$ x = x_1 $$
|
73
|
$$ x = x_2 $$
|
70
|
$$ x = x_2 $$
|
74
|
|
71
|
|
|
|
72
|
+
|
75
|
Then,
|
73
|
Then,
|
76
|
|
74
|
|
77
|
$$a x^2 + b x + c = a(x-x_1)(x-x_2),$$
|
75
|
$$a x^2 + b x + c = a(x-x_1)(x-x_2),$$
|
|
|
|
|
89
|
|
87
|
|
90
|
Note that the equation
|
88
|
Note that the equation
|
91
|
|
89
|
|
92
|
-
|
|
|
93
|
$$y=-(x-x_1)(x-x_2)$$
|
90
|
$$y=-(x-x_1)(x-x_2)$$
|
94
|
|
91
|
|
95
|
is a quadratic equation and its parabola opens down and intersects the $$x$$-axis in $$x_1$$ and $$x_2$$. For example, the equation
|
92
|
is a quadratic equation and its parabola opens down and intersects the $$x$$-axis in $$x_1$$ and $$x_2$$. For example, the equation
|
96
|
|
93
|
|
97
|
-
|
|
|
98
|
$$y=-(x+2)(x-3)=-x^2+x+6$$
|
94
|
$$y=-(x+2)(x-3)=-x^2+x+6$$
|
99
|
|
95
|
|
100
|
is a quadratic equation with a parabola that opens down and intersects the $$x$$-axis at $$x_1=-2$$ and $$x_2=3$$. Note that, in this equation, the values for $$a, b, c$$ are $$a=-1, \ b=1, \ c=6$$.
|
96
|
is a quadratic equation with a parabola that opens down and intersects the $$x$$-axis at $$x_1=-2$$ and $$x_2=3$$. Note that, in this equation, the values for $$a, b, c$$ are $$a=-1, \ b=1, \ c=6$$.
|
|
|
|
|
118
|
---
|
114
|
---
|
119
|
|
115
|
|
120
|
|
116
|
|
121
|
-##Laboratory session:
|
|
|
|
|
117
|
+## Laboratory session:
|
122
|
|
118
|
|
123
|
-###Exercise 1
|
|
|
|
|
119
|
+### Exercise 1
|
124
|
|
120
|
|
125
|
In this exercise you will implement the quadratic formula to complete a game in which a frog leaps from one leaf to another. You will assume that the leaves are in the $$x$$-axis and that the leap is determined by a parabola that opens down. If you want the frog to leap from leaf to leaf, you must find a quadratic equation with a parabola that opens down and intersects the $$x$$-axis in the places where the leaves are located. Your task is to write the equations for the quadratic formula.
|
121
|
In this exercise you will implement the quadratic formula to complete a game in which a frog leaps from one leaf to another. You will assume that the leaves are in the $$x$$-axis and that the leap is determined by a parabola that opens down. If you want the frog to leap from leaf to leaf, you must find a quadratic equation with a parabola that opens down and intersects the $$x$$-axis in the places where the leaves are located. Your task is to write the equations for the quadratic formula.
|
126
|
|
122
|
|
|
|
|
|
128
|
|
124
|
|
129
|
**Instructions**
|
125
|
**Instructions**
|
130
|
|
126
|
|
131
|
-1. Double click the file `QuadraticFrog.pro` that is contained in the `Documents/eip/Expressions-QuadraticFrog` folder to load the project `QuadraticFrog` into Qt. You can also download the folder `Expressions-QuadraticFrog` from `http://bitbucket.org/eip-uprrp/expressions-quadraticfrog`.
|
|
|
|
|
127
|
+1. Load the proyect `QuadraticFrog` into `QtCreator`. There are two ways to do this:
|
|
|
128
|
+ * Using the virtual machine: Double click the file `QuadraticFrog.pro` that can be found in the `home/Documents/eip/Expressions-QuadraticFrog` of your virtual machine.
|
|
|
129
|
+ * Downloading the proyect's folder from `Bitbucket`: Use the terminal found in your virtual machine and write the `git clone http://bitbucket.org/eip-uprrp/expressions-quadraticfrog`command to download the folder `QuadraticFrog` from `Bitbucket`. Double click the file `QuadraticFrog.pro`located in the folder that you downloaded to your computer.
|
132
|
|
130
|
|
133
|
2. Configure the project and run the program by clicking the green arrow in the menu on the left side of the Qt Creator window. The program will display a message saying that the quadratic formula is wrong. This happens because the program has testing instructions that verify that the code implementation is correct. Your program is missing the code for the quadratic formula and this is why the message is displayed.
|
131
|
2. Configure the project and run the program by clicking the green arrow in the menu on the left side of the Qt Creator window. The program will display a message saying that the quadratic formula is wrong. This happens because the program has testing instructions that verify that the code implementation is correct. Your program is missing the code for the quadratic formula and this is why the message is displayed.
|
134
|
|
132
|
|
135
|
-3. You will write the equations for the quadratic formula in the file In the file `QuadraticFormula.cpp` (in `Sources`). In the function `QuadraticPlus` add the equation
|
|
|
|
|
133
|
+3. You will write the equations for the quadratic formula in the file `QuadraticFormula.cpp` (in `Sources`). In the function `QuadraticPlus` add the equation
|
136
|
|
134
|
|
137
|
$$result=\frac{-b+\sqrt{b^2-4ac}}{2a},$$
|
135
|
$$result=\frac{-b+\sqrt{b^2-4ac}}{2a},$$
|
138
|
|
136
|
|
|
|
|
|
142
|
|
140
|
|
143
|
The other files of the project have code that will test your equations by evaluating several choices for $$a, b, c$$ and verifying that the equations produce the expected result. Code validation is an important part of software development.
|
141
|
The other files of the project have code that will test your equations by evaluating several choices for $$a, b, c$$ and verifying that the equations produce the expected result. Code validation is an important part of software development.
|
144
|
|
142
|
|
145
|
-4. Run the program by clicking the green arrow in the menu on the left side of the Qt Creator window. If your equations are implemented correclty, you should obtain a window similar to the one in Figure 2.
|
|
|
|
|
143
|
+4. Run the program by clicking the green arrow in the menu on the left side of the Qt Creator window. If your equations are implemented correctly, you should obtain a window similar to the one in Figure 2.
|
146
|
|
144
|
|
147
|
---
|
145
|
---
|
148
|
|
146
|
|
|
|
|
|
152
|
|
150
|
|
153
|
---
|
151
|
---
|
154
|
|
152
|
|
155
|
-5. Use "Deliver 1" in Moodle to submit the file `QuadraticFormula.cpp` containing the code with the functions `QuadraticPlus` and `QuadraticMinus`. Remember to use good programming practices, to include the names of the programmers and to document your program.
|
|
|
156
|
-
|
|
|
157
|
-6. To play, the frog should leap jump from one leaf to another. Note that the leaves have values for $$x_1$$ and $$x_2$$. These values represent the intersects of the parabola with the $$x$$-axis. You should input the values for the coefficients $$a, b, c$$ of the quadratic equation so that its graph that is a parabola that opens down and intersects the $$x$$-axis in the values $$x_1, x_2$$ shown in the leaves. You can obtain these values noting that
|
|
|
|
|
153
|
+5. Use "Deliver 1" in Moodle to submit the file `QuadraticFormula.cpp` containing the code with the functions `QuadraticPlus` and `QuadraticMinus`. Remember to use good programming practices, by including the names of the programmers and documenting your program.
|
158
|
|
154
|
|
|
|
155
|
+6. To play, the frog should leap from one leaf to another. Note that the leaves have values for $$x_1$$ and $$x_2$$. These values represent the intersects of the parabola with the $$x$$-axis. You should input the values for the coefficients $$a, b, c$$ of the quadratic equation so that its graph that is a parabola that opens down and intersects the $$x$$-axis in the values $$x_1, x_2$$ shown in the leaves. You can obtain these values noting that
|
159
|
|
156
|
|
160
|
$$a x^2 + b x + c = a(x-x_1)(x-x_2),$$
|
157
|
$$a x^2 + b x + c = a(x-x_1)(x-x_2),$$
|
161
|
|
158
|
|
|
|
|
|
164
|
|
161
|
|
165
|
|
162
|
|
166
|
|
163
|
|
167
|
-###Exercise 2
|
|
|
|
|
164
|
+### Exercise 2
|
168
|
|
165
|
|
169
|
In this exercise you will write a program to obtain a student's grade point average (GPA).
|
166
|
In this exercise you will write a program to obtain a student's grade point average (GPA).
|
170
|
|
167
|
|
171
|
-Suppose that all courses in Cheo's University are 3 credits each and have the following point values: $$A = 4$$ points per credit; $$B = 3$$ points per credit; $$C = 2$$ points per credit; $$D = 1$$ point per credit y $$F = 0$$ points per credit.
|
|
|
|
|
168
|
+Suppose that all courses in Yauco's University are 3 credits each and have the following point values: $$A = 4$$ points per credit; $$B = 3$$ points per credit; $$C = 2$$ points per credit; $$D = 1$$ point per credit y $$F = 0$$ points per credit.
|
172
|
|
169
|
|
173
|
**Instructions**
|
170
|
**Instructions**
|
174
|
|
171
|
|