Problems:
1. Open the flow_control_0.ino file and run the program. Observe the serial monitor output.
a. Modify the value of x so the if( ) statement from line 45 is run and prints y <= 4 to the serial monitor.
b. Change line 50 (if(y == 5.0)) to if(y = 5.0) and observe the serial monitor output. Explain why the output acts this way.
c. Change line 50 back to if(y == 5.0). Add the line y = round(y); on line 35.Now modify the value of x so the if( ) statement on line 50 is run and prints y = 5 to the serial monitor.
d. Re-write and/or rearrange the if( ) statements using else if( ) statement(s) so ONLY y = 5 is printed to the serial monitor.
2. Open the flow_control_1.ino file and run the program. Observe the serial monitor output.
a. What is the difference between do( ) and while( )?
b. Change line 56 (while(y > 10 && y < 20)) to while(y > 10 || y < 20) and observe the serial monitor output. At what value of y does this while() loop end? Explain why.
c. Why does the while(1){} repeat forever?
3. Open the arithmetic_0.ino file and run the program. Observe the serial monitor output.
a. Modify the code arithmetic to print out the first 10 odd values of x, first 10 even values of y, and the first 10 squared value of z. Note: The square values of z should be the squares of the numbers 1-10, for example: 1, 4, 9, 16, etc (Start at 1 for x and z, and 0 for y). Show your code as your answer.
b. Add 5,000 to x variable every time though the loop. What happens? Why?
c. Modify the x variable data type so the above issue doesnt happen. What data type did you need?
4. Keep using arithmetic_0.ino. Comment out lines 36 66. You can comment multiple lines of code using /* and */. Everything between these will be commented out. Un-comment lines 74 82.
This section of the code calculates a factorial for a given number. A factorial is the product of all the positive integers from 1 to a given number. (e.g., 5 factorial, or 5!, is 5 x 4 x 3 x 2 x 1 = 120) Run the program and observe the Serial Monitor output.
a. Complete the following table, filling in the missing values of i and x in each iteration of the for() loop.
Value of i | Value of x |
1 | 1 |
2 | |
6 | |
6 |
b. What is the largest factorial that can be calculated using a int data type for x? (Use the Help > Reference section to look up the range of allowable values for x, use trial-and-error within Arduino (Change the data type for x to int) or a factorial calculator to check your solution.)
5. Write your own code using a for( ) loop to calculate 10 solutions for y given the following equation. Show your code as your answer.
Use this equation in your Arduino code:
Equation in your Arduino code - Modify the Code Arithmetic