QUIZ09

In this week’s class, we needed to make a program that given coordinates would give back the distance between them.

#include <iostream>
#include <cmath>

using namespace std;

float distance(float x1, float y1, float x2, float y2){
float a = x2 - x1;
float b = y2 - y1;

return sqrt(pow(a, 2)+ pow(b, 2));
}

int main(){

float x1, y1, x2, y2;

cout << "This program will calculate the distance between coordinates" << endl;
cout << "X1: "; cin >> x1;
cout << "Y1: "; cin >> y1;
cout << "X2: "; cin >> x2;
cout << "Y2: "; cin >> y2;

cout << "The distance between coordinates (" << x1 << "," << y1 << ") & (" << x2 << "," << y2 << ") = " << distance(x1,y1,x2,y2) << endl;

return 0;
}

I made a function that would give back “c”.

Image.

pythagoras-theorem

Here is the function:

float distance(float x1, float y1, float x2, float y2){
float a = x2 - x1;
float b = y2 - y1;

return sqrt(pow(a, 2)+ pow(b, 2));
}

Featured image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s