Question

C++ code for CIS054 Create a program that uses a function to determine the length of...

C++ code for CIS054

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

by using this code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(int argc, char* argv[])
{
   double X1, Y1, X2, Y2, length;
   cout << "Enter X1 Y1 X2 Y2 seperated by spaces: ";
   cin >> X1 >> Y1 >> X2 >> Y2;
   length = LengthofLine(X1, Y1, X2, Y2);
   cout << setiosflags(ios::fixed | ios::showpoint);
   cout << "The length of the line is : " << setprecision(2) << length << endl;
   return 0;
}

double LengthofLine(double X1, double X2, double Y1, double Y2)
{
   double a, b, c;
   a = X2 - X1;
   b = Y2 - Y1;
   c = sqrt((a * a) + (b * b));
   return c;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans

code:-

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

//function prototype

double LengthofLine(double, double, double, double);

int main(int argc, char* argv[])

{

double X1, Y1, X2, Y2, length;

cout << "Enter X1 Y1 X2 Y2 seperated by spaces: ";

cin >> X1 >> Y1 >> X2 >> Y2;

length = LengthofLine(X1, Y1, X2, Y2);

cout << setiosflags(ios::fixed | ios::showpoint);

//precision of 4

cout << "The length of the line is : " << setprecision(4) << length << endl;

return 0;

}

//Take X1,Y1,X2,Y2

double LengthofLine(double X1, double Y1, double X2, double Y2)

{

double a, b, c;

a = X2 - X1;

b = Y2 - Y1;

c = sqrt((a * a) + (b * b));

return c;

}

1 #include <iostream> 2 #include <iomanip> 3 #include <cmath> 4 using namespace std; 5 //function prototype|| 6 double Length

If any doubt ask in the comments.

Add a comment
Know the answer?
Add Answer to:
C++ code for CIS054 Create a program that uses a function to determine the length of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify this code to display the message "That's hot!" if the temperature entered is greater than...

    Modify this code to display the message "That's hot!" if the temperature entered is greater than 90 degrees Farenheit or equivalent Celsius, or the message "That's cold!" if the temperature entered is less than -10 degrees Farenheit or equivalent Celsius. And to show one in between where it doesn't show any message. #include <iostream> #include <iomanip> using namespace std; // a temperature conversion program int main() { char tempType; double temp, fahren, celsius; cout << "Enter the temperature to be...

  • PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program...

    PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program in which the statements are in the incorrect order. Rearrange the statements in the following order so that the program prompts the user to input: The height of the base of a cylinder The radius of the base of a cylinder The program then outputs (in order): The volume of the cylinder. The surface area of the cylinder Format the output to two decimal...

  • Consider the following program in which the statements are in the incorrect order. Rearrange the statements...

    Consider the following program in which the statements are in the incorrect order. Rearrange the statements so that the program prompts the user to input the height and the radius of the base of a cylinder and outputs the volume and surface area of the cylinder. Formant the output to two decimal places. #include <iomanip> #include <cmath> int main () {} double height; cout << ”Volume of the cylinder = “ <<PI * pow(radius, 2.0) * height << endl; cout...

  • Write, compile, and test a C++ program that uses a loop to calculate and display each...

    Write, compile, and test a C++ program that uses a loop to calculate and display each employee’s weekly pay and the accumulated total pay for the company. The program will first ask the user for the number of employees. It will then ask for the number of hours and rate of pay for each employee and display the calculated pay for each employee. EXAMPLE RUN (user input appears in blue and bold): Enter number of employees: 3 Hours: 5 Rate...

  • In this exercise, you will modify the hypotenuse program shown earlier in Figure 9-6. Follow the...

    In this exercise, you will modify the hypotenuse program shown earlier in Figure 9-6. Follow the instructions for starting C++ and viewing the ModifyThis13.cpp file, which is contained in either the Cpp8/Chap09/ModifyThis13 Project folder or the Cpp8/Chap09 folder. Remove both calculation tasks from the main function, and assign both to a program-defined value-returning function named getHypotenuse. Test the program appropriately. //Hypotenuse.cpp - displays the length of a right //triangle's hypotenuse #include <iostream> #include <iomanip> #include <cmath> using namespace std; int...

  • Explain step by step how to solve this problem. I need it ASAP. Problem 4. ngsYou...

    Explain step by step how to solve this problem. I need it ASAP. Problem 4. ngsYou Chapter 2pds tps://csīw.csicuryedu/supernova 7/Rles/robbins/CSC270/LECTUREREVENN/Chapter%202 pdf ADVANCED HAND IN HW #3 READ STUDY 2.4.2.5.2.7,28.2.9 (skip 2.3,2.6) DO THE FOLLOWING EXAM PRACTICE part 1. PROBLEMS AT THE END OF CHAPTER 2. #14 TO #20 Both editions of the text, 10 PTS part2 #2, write the c++ code for this formula 27 points T- 2T(Lcos()/g)12 3 PTS 3. Write the algebraic formula for the following c++ code...

  • The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT =====================                        

    The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT =====================                                                                                                               0001: Enter~the~x~coordinate~for~point~1:~0                                                                                                                          0002: Enter~the~y~coordinate~for~point~1:~0                                                                                                                          0003: Enter~the~x~coordinate~for~point~2:~0                                                                                                                          0004: Enter~the~y~coordinate~for~point~2:~0                                                                                                                          0005: The~distance~is~-nan    =================== MISMATCH FOUND ON LINE 0005: ===================                                                                                                 ACTUAL  : The~distance~is~-nan                                                                                                                                       EXPECTED: The~distance~is~0                                                                                                                                          ======================================================   #include <iostream> #include <iomanip> #include <string> using namespace std; float squareRoot(float s) { float xn; xn = s / 2.0; int counter = 1; while (counter <= 10) { xn = (xn + (s/xn))/2.0; counter = counter + 1; } return xn; } int...

  • Program already solved, but I need to put function documentation headers for each and every function...

    Program already solved, but I need to put function documentation headers for each and every function in your program (for the ones you write, and keep the function header I give you for the main() function). Also make sure you keep the file block header at the top of the file, and fill in the information correctly. Secondly this week you must get your indentation correct. All indentation must use 2 spaces, and you should not have embedded tabs in...

  • Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency...

    Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency in sorting the employee’s net pay. //I need to use an EXSEL sort in order to combine the selection and Exchange sorts for my program. I currently have a selection sort in there. Please help me with replacing this with the EXSEL sort. //My input files: //My current code with the sel sort. Please help me replace this with an EXSEL sort. #include <fstream>...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT