#Quiz 3

maxresdefaultHello again. As you can see we had a third quiz. God I really hated them but that does not matter, I have to do it. So even I hated them, I did it and here is my post.

Problem 1:

  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.

So that was the problem, now I am going to explain what I did in order to solved it. Well the problem was very simple, you just need to calculate the distance between to points given by the user. So first you need to have those points. You declare four variables, in this case I used x1 and y1 for the first point and x2 and y2 for the second point. Then you proceed to ask to the user the value of each variable and you stored the value in the variables you just declare. You ask for the value using a cout and then you store the value using a cin. Then what I did was to create a function for doing all the math stuff. As you can see, I write the function at the bottom of my code, just after the int main, and then I declare the function at the beginning. That is my style but you could also just write the function on top of the code (before the int main) and it would work. So in my function what I did was declaring two more variables d and c witch would be the result of x2-x1 and

Captura de pantalla 2016-02-18 a las 6.37.53 p.m.
Captura de pantalla 2016-02-18 a las 6.38.06 p.m.
Captura de pantalla 2016-02-18 a las 7.21.59 p.m.

. Then I declare one more variable called z.  First z would be the result of c raised to the second power  plus d raised to the second power. And at the end z would be the square rout of z, in other words the new value of z will be the square rout of the old value of z. And after all this shit I return the value of z, so now the value of my function equals to z. Then you just need to print the value of the function in order to know the result.

Here is the link for you to download my code and play with it:

https://www.dropbox.com/s/n5uzvbbu2bm3qq0/distance.cpp?dl=0

Captura de pantalla 2016-02-18 a las 6.37.53 p.m.Captura de pantalla 2016-02-18 a las 6.38.06 p.m.

Problem 2:

  1. Write a function called fibonacci which receives a single parameter “n” (anon-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 in other words the user will give you an integer number (witch we are going to called n) and the program should print the value of the number in position nth of the fibonacci series. So if i give n=8, should return 21 because in the 8th position, the value is 21.

This program was a little bit harder, but I did it anyways.

Well as always what I did fist was to declare my variables. In this case I used one, and I called it num, witch will be the position of the number they want to know (n). Then as usual I ask for it using cout and then I stored it using cin. Then I proceed to do a function. My function contains an if  loop witch validates if the number is equals to 0 or 1. If this is true it returns the value of n and if it is not it does the operation inside the else. The else do this, it solves the function with n-2 plus the function with n-1. And this should give my the number depending of it´s position, in other word depending on the value of n. As you can see, in this program I wrote my function at the begging of my code and it works. In the first one I did it different and also works, so you decide witch one used.

Here is the link for you to download my code and play with it:

https://www.dropbox.com/s/t93bp4aosxyyjdv/fibonacci.cpp?dl=0

Captura de pantalla 2016-02-18 a las 7.21.59 p.m.

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