Quiz #3

#TC1017 #Quiz3

Well this quiz was really not so hard for me because I had already solved these exact problems a while ago, so they were quite simple to do.

For the first problem, calculating the distance between two coordinates all I had to do was search for the formula and implement it using a single function and asking the user for the variables, really not that different from WSQ08. This is the first time I use the math header to solve a problem since I needed to use both square roots and elevate to the power of two.

dist.proof.PNG

Source Code:

#include <iostream>
#include <math.h>
using namespace std;

int distance (int x1,int y1,int x2,int y2)
{
double result;

result = sqrt (pow((x2-x1),2) + pow((y2-y1),2));

cout << “The distance between the two coordinates is: ” << result << endl;
}

int main(){
int a, b, c, d;
cout << “Please enter the coordinates for x1:” << endl;
cin >> a;
cout << “Please enter the coordinates for y1:” << endl;
cin >> b;
cout <<“First coordinates: ” <<“(” << a << “,” << b << “)” << endl;
cout << endl;
cout << “Please enter the coordinates for x2:” << endl;
cin >> c;
cout << “Please enter the coordinates for y2:” << endl;
cin >> d;
cout <<“Second coordinates: ” <<“(” << c << “,” << d << “)” << endl;

distance (a, b, c, d);
}

 

For the second problem, calculating the last number of a user given Fibonacci series, the only real problem was getting the logic right, coding it was quite easy. I used a while loop to do so, but added a for loop as a comment which also runs the programm correctly.

Basically what this programm does is, since the Fibonacci series is

fibo.proof

you get when starting with 0 and 1, you get the next number as an addition of the previous two, is ask the user the Nth term (counter) to be displayed of said series and then passing the integer numbers back and forth until the counter is reached.

fibo.proof

 

Source Code:

#include <iostream>
using namespace std;

int a, b, counter, sum;

int main(){
cout << “How many numbers of the Fiboncacci series do you want: “;
cin >> counter;

a = 0;
b = 1;

cout << endl << “The last element of this Fibonacci series is: ” << endl;

// for (int i=counter; i>0; i–){
// sum = a + b;
// b = a;
// a = sum;
// }
// cout <<sum<< endl;

int i=0;
while (i<counter){
sum = a + b;

b = a;
a = sum;
i++;
}

cout <<sum<< endl;
}

——————–

Photo Credit: <a href=”https://www.flickr.com/photos/97206038@N03/14473918323/”>NicosFotos</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by/2.0/”>cc</a&gt;

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