|
|
|
|
1
|
-
|
|
|
2
|
# Using functions in C++ - Pretty Plots
|
1
|
# Using functions in C++ - Pretty Plots
|
3
|
|
2
|
|
4
|
|
3
|
|
|
|
|
|
8
|
|
7
|
|
9
|
[version 2016.01.29]
|
8
|
[version 2016.01.29]
|
10
|
|
9
|
|
11
|
-A good way to organize and structure computer programs is dividing them into smaller parts using functions. Each function carries out a specific task of the problem that we are solving.
|
|
|
|
|
10
|
+A good way to organize and structure computer programs is dividing them into smaller parts using functions. Each function carries out a specific task of the problem that we are solving.
|
12
|
|
11
|
|
13
|
-You've seen that all programs written in C++ must contain the `main` function where the program begins. You've probably already used functions such as `pow`, `sin`, `cos`, or `sqrt` from the `cmath` library. Since in almost all of the upcoming lab activities you will continue using pre-defined functions, you need to understand how to work with them. In future exercises you will learn how to design and validate functions. In this laboratory experience you will invoke and define functions that compute the coordinates of the points of the graphs of some curves. You will also practice the implementation of arithmetic expressions in C++.
|
|
|
|
|
12
|
+You've seen that all programs written in C++ must contain the `main` function where the program begins. You've probably already used functions such as `pow`, `sin`, `cos`, or `sqrt` from the `cmath` library. Since in almost all of the upcoming lab activities you will continue using pre-defined functions, you need to understand how to work with them. In future exercises you will learn how to design and validate functions. In this laboratory experience you will call and define functions that compute the coordinates of the points of the graphs of some curves. You will also practice the implementation of arithmetic expressions in C++.
|
14
|
|
13
|
|
15
|
|
14
|
|
16
|
## Objectives:
|
15
|
## Objectives:
|
17
|
|
16
|
|
18
|
1. Identify the parts of a function: return type, name, list of parameters, and body.
|
17
|
1. Identify the parts of a function: return type, name, list of parameters, and body.
|
19
|
-2. Invoke pre-defined functions by passing arguments by value ("pass by value"), and by reference ("pass by reference").
|
|
|
|
|
18
|
+2. Call pre-defined functions by passing arguments by value ("pass by value"), and by reference ("pass by reference").
|
20
|
3. Implement a simple overloaded function.
|
19
|
3. Implement a simple overloaded function.
|
21
|
4. Implement a simple function that utilizes parameters by reference.
|
20
|
4. Implement a simple function that utilizes parameters by reference.
|
22
|
5. Implement arithmetic expressions in C++.
|
21
|
5. Implement arithmetic expressions in C++.
|
|
|
|
|
29
|
|
28
|
|
30
|
a. the basic elements of a function definition in C++.
|
29
|
a. the basic elements of a function definition in C++.
|
31
|
|
30
|
|
32
|
- b. how to invoke functions in C++.
|
|
|
|
|
31
|
+ b. how to call functions in C++.
|
33
|
|
32
|
|
34
|
c. the difference between parameters that are passed by value and by reference.
|
33
|
c. the difference between parameters that are passed by value and by reference.
|
35
|
|
34
|
|
|
|
|
|
43
|
|
42
|
|
44
|
2. Studied the concepts and instructions for the laboratory session.
|
43
|
2. Studied the concepts and instructions for the laboratory session.
|
45
|
|
44
|
|
46
|
-3. Taken the Pre-Lab quiz that can be found in Moodle.
|
|
|
47
|
|
45
|
|
48
|
---
|
46
|
---
|
49
|
|
47
|
|
|
|
|
|
51
|
|
49
|
|
52
|
## Functions
|
50
|
## Functions
|
53
|
|
51
|
|
54
|
-In mathematics, a function $f$ is a rule that is used to assign to each element $x$ from a set called *domain*, one (and only one) element $y$ from a set called *range*. This rule is commonly represented with an equation, $y=f(x)$. The variable $x$ is the parameter of the function and the variable $y$ will contain the result of the function. A function can have more than one parameter, but only one result. For example, a function can have the form $y=f(x_1,x_2)$ where there are two parameters, and for each pair $(a,b)$ that is used as an argument in the function, the function has only one value of $y=f(a,b)$. The domain of the function tells us the type of value that the parameter should have and the range tells us the value that the returned result will have.
|
|
|
|
|
52
|
+In mathematics, a function $$f$$ is a rule that is used to assign to each element $$x$$ from a set called *domain*, one (and only one) element $$y$$ from a set called *range*. This rule is commonly represented with an equation, $$y=f(x)$$. The variable $$x$$ is the parameter of the function and the variable $$y$$ will contain the result of the function. A function can have more than one parameter, but only one result. For example, a function can have the form $$y=f(x_1,x_2)$$ where there are two parameters, and for each pair $$(a,b)$$ that is used as an argument in the function, the function has only one value of $$y=f(a,b)$$. The domain of the function tells us the type of value that the parameter should have and the range tells us the value that the returned result will have.
|
55
|
|
53
|
|
56
|
Functions in programming languages are similar. A function has a series of instructions that take the assigned values as parameters and performs a certain task. In C++ and other programming languages, functions return only one result, as it happens in mathematics. The only difference is that a *programming* function could possibly not return any value (in this case the function is declared as `void`). If the function will return a value, we use the instruction `return`. As in math, you need to specify the types of values that the function's parameters and result will have; this is done when declaring the function.
|
54
|
Functions in programming languages are similar. A function has a series of instructions that take the assigned values as parameters and performs a certain task. In C++ and other programming languages, functions return only one result, as it happens in mathematics. The only difference is that a *programming* function could possibly not return any value (in this case the function is declared as `void`). If the function will return a value, we use the instruction `return`. As in math, you need to specify the types of values that the function's parameters and result will have; this is done when declaring the function.
|
57
|
|
55
|
|
|
|
|
|
68
|
would be the header of the function called `example`, which returns an integer value. The function receives as arguments an integer value (and will store a copy in `var1`), a value of type `float` (and will store a copy in `var2`) and the reference to a variable of type `char` that will be stored in the reference variable `var3`. Note that `var3` has a & symbol before the name of the variable. This indicates that `var3` will contain the reference to a character.
|
66
|
would be the header of the function called `example`, which returns an integer value. The function receives as arguments an integer value (and will store a copy in `var1`), a value of type `float` (and will store a copy in `var2`) and the reference to a variable of type `char` that will be stored in the reference variable `var3`. Note that `var3` has a & symbol before the name of the variable. This indicates that `var3` will contain the reference to a character.
|
69
|
|
67
|
|
70
|
|
68
|
|
71
|
-### Invoking
|
|
|
|
|
69
|
+### Calling
|
72
|
|
70
|
|
73
|
|
71
|
|
74
|
-If we want to store the value of the `example` function's result in a variable `result` (that would be of type integer), we invoke the function by passing arguments as follows:
|
|
|
|
|
72
|
+If we want to store the value of the `example` function's result in a variable `result` (that would be of type integer), we call the function by passing arguments as follows:
|
75
|
|
73
|
|
76
|
`result=example(2, 3.5, unCar);`
|
74
|
`result=example(2, 3.5, unCar);`
|
77
|
|
75
|
|
78
|
-Note that as the function is invoked, you don't include the type of the variables in the arguments. As in the definition for the function `example`, the third parameter `&var3` is a reference variable; what is being sent to the third argument when invoking the function is a *reference* to the variable `unCar`. Any changes that are made on the variable `var3` will change the contents of the variable `unCar`.
|
|
|
|
|
76
|
+Note that as the function is called, you don't include the type of the variables in the arguments. As in the definition for the function `example`, the third parameter `&var3` is a reference variable; what is being sent to the third argument when invoking the function is a *reference* to the variable `unCar`. Any changes that are made on the variable `var3` will change the contents of the variable `unCar`.
|
79
|
|
77
|
|
80
|
You can also use the function's result without having to store it in a variable. For example you could print it:
|
78
|
You can also use the function's result without having to store it in a variable. For example you could print it:
|
81
|
|
79
|
|
|
|
|
|
83
|
|
81
|
|
84
|
or use it in an arithmetic expression:
|
82
|
or use it in an arithmetic expression:
|
85
|
|
83
|
|
86
|
-`y=3 + example(2, 3.5, unCar);`
|
|
|
|
|
84
|
+`y = 3 + example(2, 3.5, unCar);`
|
87
|
|
85
|
|
88
|
|
86
|
|
89
|
|
87
|
|
|
|
|
|
129
|
|
127
|
|
130
|
|
128
|
|
131
|
|
129
|
|
132
|
-### Values by default
|
|
|
|
|
130
|
+### Default values
|
133
|
|
131
|
|
134
|
Values by default can be assigned to the parameters of the functions starting from the first parameter to the right. It is not necessary to initialize all of the parameters, but the ones that are initialized should be consecutive: parameters in between two parameters cannot be left uninitialized. This allows calling the function without having to send values in the positions that correspond to the initialized parameters.
|
132
|
Values by default can be assigned to the parameters of the functions starting from the first parameter to the right. It is not necessary to initialize all of the parameters, but the ones that are initialized should be consecutive: parameters in between two parameters cannot be left uninitialized. This allows calling the function without having to send values in the positions that correspond to the initialized parameters.
|
135
|
|
133
|
|
136
|
-**Examples of function headers and valid invocations:**
|
|
|
|
|
134
|
+**Examples of function headers and valid function calls:**
|
137
|
|
135
|
|
138
|
1. **Headers:** `int example(int var1, float var2, int var3 = 10)`
|
136
|
1. **Headers:** `int example(int var1, float var2, int var3 = 10)`
|
139
|
|
137
|
|
140
|
- **Invocations:**
|
|
|
|
|
138
|
+ **Calls:**
|
141
|
|
139
|
|
142
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value of 12 to `var3`.
|
140
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value of 12 to `var3`.
|
143
|
|
141
|
|
|
|
|
|
145
|
|
143
|
|
146
|
2. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
144
|
2. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
147
|
|
145
|
|
148
|
- **Invocations:**
|
|
|
|
|
146
|
+ **Calls:**
|
149
|
|
147
|
|
150
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value 12 to `var3`.
|
148
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value 12 to `var3`.
|
151
|
|
149
|
|
|
|
|
|
154
|
c. `example(5)` In this function call only the first parameter is given a value, and the last two parameters will be assigned values by default. That is, `var1` will be 5, `var2` will be 5.0, and `var3` will be 10.
|
152
|
c. `example(5)` In this function call only the first parameter is given a value, and the last two parameters will be assigned values by default. That is, `var1` will be 5, `var2` will be 5.0, and `var3` will be 10.
|
155
|
|
153
|
|
156
|
|
154
|
|
157
|
-**Example of a valid function header with invalid invocations:**
|
|
|
|
|
155
|
+**Example of a valid function header with invalid function calls:**
|
158
|
|
156
|
|
159
|
1. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
157
|
1. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
160
|
|
158
|
|
161
|
- **Invocation:**
|
|
|
|
|
159
|
+ **Calls:**
|
162
|
|
160
|
|
163
|
a. `example(5, , 10)` This function call is **invalid** because it leaves an empty space in the middle argument.
|
161
|
a. `example(5, , 10)` This function call is **invalid** because it leaves an empty space in the middle argument.
|
164
|
|
162
|
|
165
|
- b. `example()` This function call is **invalid** because `var1` was not assigned a default value. A valid invocation to the function `example` needs at least one argument (the first).
|
|
|
|
|
163
|
+ b. `example()` This function call is **invalid** because `var1` was not assigned a default value. A valid call to function `example` needs at least one argument (the first).
|
166
|
|
164
|
|
167
|
|
165
|
|
168
|
**Examples of invalid function headers:**
|
166
|
**Examples of invalid function headers:**
|
|
|
|
|
178
|
|
176
|
|
179
|
## Parametric equations
|
177
|
## Parametric equations
|
180
|
|
178
|
|
181
|
-*Parametric equations* allow us to represent a quantity as a function of one or more independent variables called *parameters*. In many occasions it is useful to represent curves using a set of parametric equations that express the coordinates of the points of the curve as functions of the parameters. For example, in your trigonometry course you should have studied that the equation of the circle of radius $r$ and centered at the origin has the following form:
|
|
|
|
|
179
|
+*Parametric equations* allow us to represent a quantity as a function of one or more independent variables called *parameters*. In many occasions it is useful to represent curves using a set of parametric equations that express the coordinates of the points of the curve as functions of the parameters. For example, in your trigonometry course you should have studied that the equation of the circle of radius $$r$$ and centered at the origin has the following form:
|
182
|
|
180
|
|
183
|
|
181
|
|
184
|
$$x^2+y^2=r^2.$$
|
182
|
$$x^2+y^2=r^2.$$
|
185
|
|
183
|
|
186
|
-The points $(x,y)$ that satisfy this equation are the points that form the circle of radius $r$ and center at the origin. For example, the circle with $r=2$ and center at the origin has equation
|
|
|
|
|
184
|
+The points $$(x,y)$$ that satisfy this equation are the points that form the circle of radius $$r$$ and center at the origin. For example, the circle with $$r=2$$ and center at the origin has equation
|
187
|
|
185
|
|
188
|
|
186
|
|
189
|
$$x^2+y^2=4,$$
|
187
|
$$x^2+y^2=4,$$
|
190
|
|
188
|
|
191
|
-and its points are the ordered pairs $(x,y)$ that satisfy this equation. A parametric representation of the coordinates of the points in the circle of radius $r$ and center at the origin is:
|
|
|
|
|
189
|
+and its points are the ordered pairs $$(x,y)$$ that satisfy this equation. A parametric representation of the coordinates of the points in the circle of radius $$r$$ and center at the origin is:
|
192
|
|
190
|
|
193
|
|
191
|
|
194
|
$$x=r \cos(t)$$
|
192
|
$$x=r \cos(t)$$
|
195
|
|
193
|
|
196
|
$$y=r \sin(t),$$
|
194
|
$$y=r \sin(t),$$
|
197
|
|
195
|
|
198
|
-where $t$ is a parameter that corresponds to the measure (in radians) of the positive angle with initial side that coincides with the positive part of the $x$-axis and terminal side that contains the point $(x,y)$, as it is illustrated in Figure 1.
|
|
|
|
|
196
|
+where $$t$$ is a parameter that corresponds to the measure (in radians) of the positive angle with initial side that coincides with the positive part of the $$x$$-axis and terminal side that contains the point $$(x,y)$$, as it is illustrated in Figure 1.
|
199
|
|
197
|
|
200
|
|
198
|
|
201
|
---
|
199
|
---
|
202
|
|
200
|
|
203
|
![circulo.jpg](images/circuloAngulo01.png)
|
201
|
![circulo.jpg](images/circuloAngulo01.png)
|
204
|
|
202
|
|
205
|
-**Figure 1.** Circle with center in the origin and radius $r$.
|
|
|
|
|
203
|
+**Figure 1.** Circle with center in the origin and radius $$r$$.
|
206
|
|
204
|
|
207
|
|
205
|
|
208
|
---
|
206
|
---
|
209
|
|
207
|
|
210
|
-To plot a curve that is described by parametric equations, we compute the $x$ and $y$ values for a set of values of the parameter. For example, for $r=2$, some of the values are
|
|
|
|
|
208
|
+To plot a curve that is described by parametric equations, we compute the $$x$$ and $$y$$ values for a set of values of the parameter. For example, Figure 2 shows the values for $$t$$, $$(x,y)$$ for the circle with $$r=2$$.
|
211
|
|
209
|
|
212
|
|
210
|
|
213
|
---
|
211
|
---
|
214
|
|
212
|
|
215
|
![circulo.jpg](images/circuloPuntos01.png)
|
213
|
![circulo.jpg](images/circuloPuntos01.png)
|
216
|
|
214
|
|
217
|
-**Figure 2.** Some coordinates for the points $(x,y)$ for the circle with radius $r=2$ and center in the origin.
|
|
|
|
|
215
|
+**Figure 2.** Some coordinates for the points $$(x,y)$$ for the circle with radius $$r=2$$ and center in the origin.
|
218
|
|
216
|
|
219
|
---
|
217
|
---
|
220
|
|
218
|
|
221
|
---
|
219
|
---
|
222
|
|
220
|
|
|
|
221
|
+
|
|
|
222
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-01.html"
|
|
|
223
|
+<br>
|
|
|
224
|
+
|
|
|
225
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-03.html"
|
|
|
226
|
+<br>
|
|
|
227
|
+
|
|
|
228
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-11.html"
|
|
|
229
|
+<br>
|
|
|
230
|
+
|
|
|
231
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-12.html"
|
|
|
232
|
+<br>
|
|
|
233
|
+
|
|
|
234
|
+
|
|
|
235
|
+---
|
|
|
236
|
+
|
|
|
237
|
+---
|
|
|
238
|
+
|
|
|
239
|
+
|
223
|
## Laboratory session:
|
240
|
## Laboratory session:
|
224
|
|
241
|
|
225
|
In the introduction to the topic of functions you saw that in mathematics and in some programming languages, a function cannot return more than one result. In this laboratory experience's exercises you will practice how to use reference variables to obtain various results from a function.
|
242
|
In the introduction to the topic of functions you saw that in mathematics and in some programming languages, a function cannot return more than one result. In this laboratory experience's exercises you will practice how to use reference variables to obtain various results from a function.
|
|
|
|
|
230
|
|
247
|
|
231
|
**Instructions**
|
248
|
**Instructions**
|
232
|
|
249
|
|
233
|
-1. Load the project `prettyPlot` in Qt by double clicking on the file `prettyPlot.pro` in the folder `Documents/eip/Functions-PrettyPlots` of your computer. You may also go to `http://bitbucket.org/eip-uprrp/functions-prettyplots` to download the folder `Functions-PrettyPlots` to your computer.
|
|
|
|
|
250
|
+1. Download the folder`Functions-PrettyPlots` from `Bitbucket` using the terminal, moving to the `Documents/eip` directory, and writing the command `git clone http://bitbucket.org/eip-uprrp/functions-prettyplots`.
|
234
|
|
251
|
|
235
|
-2. Configure the project and execute the program by clicking on the green arrow in the menu on the left side of the Qt Creator screen. The program should show a window similar to the one in Figure 3.
|
|
|
|
|
252
|
+2. Load the project `prettyPlot` in Qt Creator by double clicking on the file `prettyPlot.pro` in the folder `Documents/eip/functions-prettyplots` of your computer.
|
|
|
253
|
+
|
|
|
254
|
+3. Configure the project and execute the program by clicking on the green arrow in the menu on the left side of the Qt Creator screen. The program should show a window similar to the one in Figure 3.
|
236
|
|
255
|
|
237
|
---
|
256
|
---
|
238
|
|
257
|
|
|
|
|
|
242
|
|
261
|
|
243
|
---
|
262
|
---
|
244
|
|
263
|
|
245
|
-3. Open the file `main.cpp` (in Sources). Study the `illustration` function and how to call it from the `main` function. Note that the variables `argValue` and `argRef`are initialized to 0 and that the function call for `illustration` makes a pass by value of `argValue` and a pass by reference of `argRef`. Also note that the corresponding parameters in `illustration` are assigned a value of 1.
|
|
|
|
|
264
|
+4. Open the file `main.cpp` (in Sources). Study the `illustration` function and how to call it from the `main` function. Note that the variables `argValue` and `argRef`are initialized to 0 and that the function call for `illustration` makes a pass by value of `argValue` and a pass by reference of `argRef`. Also note that the corresponding parameters in `illustration` are assigned a value of 1.
|
246
|
|
265
|
|
247
|
void illustration(int paramValue, int ¶mRef) {
|
266
|
void illustration(int paramValue, int ¶mRef) {
|
248
|
paramValue = 1;
|
267
|
paramValue = 1;
|
|
|
|
|
251
|
<< "The content of paramRef is: " << paramRef << endl;
|
270
|
<< "The content of paramRef is: " << paramRef << endl;
|
252
|
}
|
271
|
}
|
253
|
|
272
|
|
254
|
-4. Execute the program and observe what is displayed in the window `Application Output`. Notice the difference between the content of the variables `argValue` and `argRef` despite the fact that both had the same initial value, and that `paramValue` and `paramRef` were assigned the same value. Explain why the content of `argValor` does not change, while the content of `argRef` changes from 0 to 1.
|
|
|
|
|
273
|
+5. Execute the program and observe what is displayed in the window `Application Output`. Notice the difference between the content of the variables `argValue` and `argRef` despite the fact that both had the same initial value, and that `paramValue` and `paramRef` were assigned the same value. Explain why the content of `argValor` does not change, while the content of `argRef` changes from 0 to 1.
|
255
|
|
274
|
|
256
|
### Exercise 2
|
275
|
### Exercise 2
|
257
|
|
276
|
|
|
|
|
|
259
|
|
278
|
|
260
|
**Instructions**
|
279
|
**Instructions**
|
261
|
|
280
|
|
262
|
-1. Study the code in the `main()` function in the file `main.cpp`.
|
|
|
|
|
281
|
+1. Study the code in the `main()` function in the file `main.cpp`. The line `XYPlotWindow wCircleR5;` creates a `wCircleR5` object that will be the window where the graph will be drawn, in this case the graph of a circle of radius 5. In a similar way, the objects `wCircle` and `wButterfly` are created. Observe the `for` cycle. In this cycle a series of values for the angle $$t$$ are generated and the function `circle` is called, passing the value for $$t$$ and the references to $$x$$ and $$y$$. The `circle` function does not return a value, but using parameters by reference, it calculates the values for the coordinates $$xCoord$$ and $$yCoord$$ for the circle with center in the origin and radius 5, and allows the `main` function to have these values in the `x` , `y` variables.
|
263
|
|
282
|
|
264
|
XYPlotWindow wCircleR5;
|
283
|
XYPlotWindow wCircleR5;
|
265
|
XYPlotWindow wCircle;
|
284
|
XYPlotWindow wCircle;
|
|
|
|
|
271
|
double increment = 0.01;
|
290
|
double increment = 0.01;
|
272
|
int argValue=0, argRef=0;
|
291
|
int argValue=0, argRef=0;
|
273
|
|
292
|
|
274
|
- // invoke the function illustration to view the contents of variables
|
|
|
|
|
293
|
+ // call the function illustration to view the contents of variables
|
275
|
// by value and by reference
|
294
|
// by value and by reference
|
276
|
|
295
|
|
277
|
illustration(argValue,argRef);
|
296
|
illustration(argValue,argRef);
|
|
|
|
|
281
|
// repeat for several values of the angle t
|
300
|
// repeat for several values of the angle t
|
282
|
for (double t = 0; t < 16*M_PI; t = t + increment) {
|
301
|
for (double t = 0; t < 16*M_PI; t = t + increment) {
|
283
|
|
302
|
|
284
|
- // invoke circle with the angle t and reference variables x, y as arguments
|
|
|
|
|
303
|
+ // call circle with the angle t and reference variables x, y as arguments
|
285
|
circle(t,x,y);
|
304
|
circle(t,x,y);
|
286
|
|
305
|
|
287
|
// add the point (x,y) to the graph of the circle
|
306
|
// add the point (x,y) to the graph of the circle
|
288
|
wCircleR5.AddPointToGraph(x,y);
|
307
|
wCircleR5.AddPointToGraph(x,y);
|
289
|
|
308
|
|
290
|
- The line `XYPlotWindow wCircleR5;` creates a `wCircleR5` object that will be the window where the graph will be drawn, in this case the graph of a circle of radius 5. In a similar way, the objects `wCircle` and `wButterfly` are created. Observe the `for` cycle. In this cycle a series of values for the angle $t$ is generated and the function `circle` is invoked, passing the value for $t$ and the references to $x$ and $y$. The `circle` function does not return a value, but using parameters by reference, it calculates the values for the coordinates $xCoord$ and $yCoord$ for the circle with center in the origin and radius 5, and allows the `main` function to have these values in the `x` , `y` variables.
|
|
|
291
|
-
|
|
|
292
|
- After the function call, each ordered pair $(x,y)$ is added to the circle’s graph by the member function `AddPointToGraph(x,y)`. After the cycle, the member function `Plot()` is invoked, which draws the points, and the member function `show()`, which displays the graph. The *members functions* are functions that allow use to work with and object’s data. Notice that each one of the member functions is written after `wCircleR5`, followed by a period. In an upcoming laboratory experience you will learn more about objects, and practice how to create them and invoke their method functions.
|
|
|
|
|
309
|
+ After the function call, each ordered pair $$(x,y)$$ is added to the circle’s graph by the member function `AddPointToGraph(x,y)`. After the cycle, the member function `Plot()` is called, which draws the points, and the member function `show()`, which displays the graph. The *members functions* are functions that allow use to work with and object’s data. Notice that each one of the member functions is written after `wCircleR5`, followed by a period. In an upcoming laboratory experience you will learn more about objects, and practice how to create them and invoke their method functions.
|
293
|
|
310
|
|
294
|
- The `circle` function implemented in the program is very restrictive since it always calculates the values for the coordinates $x$ and $y$ of the same circle: the circle with center in the origin and radius 5.
|
|
|
|
|
311
|
+ The `circle` function implemented in the program is very restrictive since it always calculates the values for the coordinates $$x$$ and $$y$$ of the same circle: the circle with center in the origin and radius 5.
|
295
|
|
312
|
|
296
|
-2. Now you will create an overloaded function `circle` that receives as arguments the value of the angle $t$, the reference to the variables $x$ and $y$, and the value for the radius of the circle. Invoke the overloaded function `circle` that you just implemented from `main()` to calculate the values of the coordinates $x$ and $y$ for the circle with radius 15 and draw its graph. Graph the circle within the `wCircle` object. To do this, you must invoke the method functions `AddPointToGraph(x,y)`, `Plot` and `show` from `main()`. Remember that these should be preceded by `wCircle`, for example, `wCircle.show()`.
|
|
|
|
|
313
|
+2. Now you will create an overloaded function `circle` that receives as arguments the value of the angle $$t$$, the reference to the variables $$x$$ and $$y$$, and the value for the radius of the circle. Call the overloaded function `circle` that you just implemented from `main()` to calculate the values of the coordinates $$x$$ and $$y$$ for the circle with radius 15 and draw its graph. Graph the circle within the `wCircle` object. To do this, you must call the method functions `AddPointToGraph(x,y)`, `Plot` and `show` from `main()`. Remember that these should be preceded by `wCircle`, for example, `wCircle.show()`.
|
297
|
|
314
|
|
298
|
### Exercise 3
|
315
|
### Exercise 3
|
299
|
|
316
|
|
|
|
|
|
307
|
|
324
|
|
308
|
$$y= 10\sin(t) \left[ \sin^2(1.2t) + \cos^3(6t) \right].$$
|
325
|
$$y= 10\sin(t) \left[ \sin^2(1.2t) + \cos^3(6t) \right].$$
|
309
|
|
326
|
|
310
|
- Observe that both expressions are almost the same, except that one starts with $5\cos(t)$ and the other with $10\sin(t)$. Instead of doing the calculation for $ \sin^2(1.2t) + \cos^3(6t)$ twice, you can assign its value to another variable $q$ and calculate it as such:
|
|
|
|
|
327
|
+ Observe that both expressions are almost the same, except that one starts with $$5\cos(t)$$ and the other with $$10\sin(t)$$. Instead of doing the calculation for $$ \sin^2(1.2t) + \cos^3(6t)$$ twice, you can assign its value to another variable $$q$$ and calculate it as such:
|
311
|
|
328
|
|
312
|
$$q = \sin^2(1.2t) + \cos^3(6t)$$
|
329
|
$$q = \sin^2(1.2t) + \cos^3(6t)$$
|
313
|
|
330
|
|
|
|
|
|
315
|
|
332
|
|
316
|
$$y = 10 \sin(t)(q).$$
|
333
|
$$y = 10 \sin(t)(q).$$
|
317
|
|
334
|
|
318
|
-2. Implement the function `butterfly` using the expressions above, invoke the function in `main()` and observe the resulting graph. It is supposed to look like a butterfly. This graph should have been obtained with a `XYPlotWindow` object called `wButterfly`, invoking method functions similar to how you did in Exercise 2 for the circle.
|
|
|
|
|
335
|
+2. Implement the function `butterfly` using the expressions above, call the function in `main()` and observe the resulting graph. It is supposed to look like a butterfly. This graph should have been obtained with a `XYPlotWindow` object called `wButterfly`, invoking method functions similar to how you did in Exercise 2 for the circle.
|
319
|
|
336
|
|
320
|
In [2] and [3] you can find other parametric equations from other interesting curves.
|
337
|
In [2] and [3] you can find other parametric equations from other interesting curves.
|
321
|
|
338
|
|
322
|
---
|
339
|
---
|
|
|
340
|
+
|
323
|
---
|
341
|
---
|
324
|
|
342
|
|
325
|
##Deliverables
|
343
|
##Deliverables
|
326
|
|
344
|
|
327
|
-Use "Deliverables" in Moodle to hand in the file `main()` that contains the functions you implemented, the function calls and changes you made in exercises 2 and 3. Remember to use good programming techniques, include the name of the programmers involved and document your program.
|
|
|
|
|
345
|
+Use "Deliverables" in Moodle to hand in the file `main.cpp` that contains the functions you implemented, the function calls and changes you made in exercises 2 and 3. Remember to use good programming techniques, include the name of the programmers involved and document your program.
|
328
|
|
346
|
|
329
|
|
347
|
|
330
|
---
|
348
|
---
|
|
|
|
|
337
|
|
355
|
|
338
|
[2] http://paulbourke.net/geometry/butterfly/
|
356
|
[2] http://paulbourke.net/geometry/butterfly/
|
339
|
|
357
|
|
340
|
-[3] http://en.wikipedia.org/wiki/Parametric_equation
|
|
|
|
|
358
|
+[3] http://en.wikipedia.org/wiki/Parametric_equation
|