2nd program

Write a program that creates the output shown in the Output Layout section below. The program should create 2 points with x and y coordinates as integers, prompt the user to input the x and y values for one of the points and randomly set the other (-99 to 99 range) and output the length of the radius line segment and the area of the circle that radius defines. The program should then end. Include an SDM for the program and any other appropriate documentation.

Special Calculations:
Distance between 2 points equation:
√((p0x – p1x)2 + (p0y – p1y)2) (This requires use of the math library)


Output Layout: (bold text represents user input)

Please enter the location of your first point.
Enter a value from -99 to 99 for your x coordinate: -2
Enter a value from -99 to 99 for your y coordinate: 17

The location of your second randomly set point.
Your x coordinate: 45
Your y coordinate: -89

The length of the radius line segment from point one to point two is 115.
The area of the circle with a radius of 115 is 41546.33.

Other:

int pAx;
int pAy;
int pBx;
int pBy;
And where do you have problems?
I did it,just want to make sure if my response is correct.

//Patrick Prevost
//Date : 02/29/2013
//Course : CPI


// pov2.cpp : Defines the entry point for the console application.
//

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

int main()
{
int x1;
int x2;
int y1;
int y2;
int deltaX;
int deltaY;
double hypot;
double lenght;
float radius;
float area;
const float PI = 3.14159;

srand (time(NULL));

x2 = rand()% 199-99;
y2 = rand()% 199-99;

cout << "Enter a value from -99 to 99 for your x coordinate:";
cin >> x1;

cout << "Enter a value from -99 to 99 for your y oordiante:";
cin >> y1;

deltaX = x2-x1;
deltaY = y2-y1;
hypot = (deltaX * deltaX) + (deltaX * deltaY);
lenght = sqrt(hypot);

radius = lenght / 2;
area = PI * radius * radius;

cout << "\n The coordinates of the second point are\n ";
cout << "X: " << x2 << "\n";
cout << "y: " << y2 << "\n\n";
cout << " The lenght of the radius line segment is: " << lenght << "\n";
cout << " The area of the circle is: " << area << "\n\n";

return EXIT_SUCCESS;

}





1) There is std::hypot function in cmath, so you cah just do:
1
2
3
deltaX = x2-x1;
deltaY = y2-y1;
lenght = std::hypot(deltaX, deltaY);

2) You shouldn't divide lenght by 2, it is already radius you want. Now it isn't the same as in example:
Enter a value from -99 to 99 for your x coordinate:-2
Enter a value from -99 to 99 for your y oordiante:17

 The coordinates of the second point are
 X: 45
y: -89

 The lenght of the radius line segment is: 115.953
 The area of the circle is: 10559.7


And don't forget to use code tags (they looks like that: <>)
Topic archived. No new replies allowed.