Quiz 3

3QFOErL.gifSimplicity is the ultimate sophistication.DaVinci

Hello people. I made my third and the last one of the first partial. We have to create two programs: the first program, may calculate the distance between two points (x1,y1) & (x2,y2). In the second one, we have to create the Fibonacci sequence on C++ and ask to the user for the value that he/she wants. To make that, we use our previous knowledge about it, like conditionals, loops, functions, etc. so it wasn’t difficult.

The instructions were:

1. Write a function called distance(x1, y1, x2, y2) which receives four numbers which represent two points in the cartesian plane. The function should return the distance between the two points (x1,y1) and (x2,y2). Remember that the square of the hypotenuse of a right angle triangle is equal to the sum of the squares of the two other sides. Also, don’t forget to declare the variables with FLOAT to use decimal numbers.

**The new thing here, was the use of the math library, to can execute the square (SQRT in C++). The math library is using like this:

#include <cmath>

And now you can use the formula of the Pitagoras Theorem :

Distance= sqrt((x2-x1)^2+(y2-y1)^2)

Here my code with comments to make it understandable.

dis

And the program running:

disc

2. Write a function called fibonacci which receives a single parameter “n” (a non-negative integer) and returns the nth number in the fibonacci series which is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89………… So, fibonacci(0) would return 0. fibonacci(5) would return 5 fibonacci(8) would return 21. Note that the first two fibonacci numbers are 0 and 1. All others are the sum of the previous two fibonacci numbers. int fibonacci(int n){ }

Here I got a headache with the Fibonacci Formula, but once

fibc.png
dancing-banana
3QFOErL.gif

found it and understand it, I make it fastly. I made it like three times but Ken told me that I have to make it without declaring values, Just with the one that we are asking for. (n). In addition, I put a do-while to ask to the user if he wants to run the program again 😀.

So then I found the correct formula and you can see it in my code:

Haga click para ver el pase de diapositivas.

And how it works:

fibc.png

Check my codes on GitHub:

https://github.com/eduardomrlsg/TC101/blob/master/Distance

https://github.com/eduardomrlsg/TC101/blob/master/Fibonacci

IT WORKS BABY.

dancing-banana

Thanks and hope you like it!

Regards

EdM.

3QFOErL.gif

CC BY-SA 4.0 Quiz 3 by eduardomoralesg is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.