C++ LENGTH OF LINE PROGRAM LAB REPORT 1) Enter your name, student ID, platform (
ID: 3598266 • Letter: C
Question
C++ LENGTH OF LINE PROGRAM LAB REPORT 1) Enter your name, student ID, platform (Mac or PC) and date Name and Student ID: Class: CIS054 C/C++ Programming Platform (Mac or PC): Date: DESCRIPTION: Create a program that uses a function to determine the length of a line by inputting the X,Y coordinates of the line endpoints. Show the result with a precision of four decimal places. The function prototype is to be specified as follows: double LengthOfLine (double X1, double Y1, double X2, double Y2); // returns length of line
Use pythagorean theorem to compute the length of the line
Hypotenuse = squr (x^2 + y^2)
This is my progress so far but a par of it is wrong.
/
// main.cpp
// Lentgh if a line
//
// Created by Erol Alvarez on 10/15/17.
// Copyright © 2017 Erol Alvarez. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
double LengthOfLine (double X1, double Y1, double X2, double Y2, double length);
int main(int argc, const char * argv[])
{
double X1, X2, Y1, Y2, length;
cout << "Enter X1 X2 Y1 Y2 separated by spaces: ";
cin >> X1 >> X2 >> Y1 >> Y2;
length = LengthOfLine (X1, Y1, X2, Y2);
cout << "The length of the line is " << length << endl;
return 0;
}
double LengthOfLine (double X1, double Y1, double X2, double Y2)
{
double a, b, c; // c is the computed length of the line
a = X2 - X1;
b = Y2 - Y1;
c = sqrt(a*a + b*b);
return c;
}
Explanation / Answer
No the code is correct only printing of answer is incorrect. Use printf for that :
CODE :
#include <iostream>
#include <cmath>
using namespace std;
double LengthOfLine(double X1, double Y1, double X2, double Y2, double length);
double LengthOfLine(double X1, double Y1, double X2, double Y2)
{
double a, b, c; // c is the computed length of the line
a = X2 - X1;
b = Y2 - Y1;
c = sqrt(a*a + b*b);
return c;
}
int main(int argc, const char * argv[])
{
double X1, X2, Y1, Y2, length;
cout << "Enter X1 X2 Y1 Y2 separated by spaces: ";
cin >> X1 >> X2 >> Y1 >> Y2;
length = LengthOfLine(X1, Y1, X2, Y2);
printf("The length of the line is %0.4f", length);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.