0% found this document useful (0 votes)
3 views

New Text Document

The document contains instructions for writing a program that calculates the equation of the perpendicular bisector of a line segment defined by two points. It details the steps for computing the slope, midpoint, and y-intercept, while also addressing special cases for vertical and horizontal lines. Sample runs illustrate the expected input and output formats.

Uploaded by

faresimfree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

New Text Document

The document contains instructions for writing a program that calculates the equation of the perpendicular bisector of a line segment defined by two points. It details the steps for computing the slope, midpoint, and y-intercept, while also addressing special cases for vertical and horizontal lines. Sample runs illustrate the expected input and output formats.

Uploaded by

faresimfree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a program that outputs the equation of the perpendicular bisector of the line

segment between two points. Your program should


prompt for and input the coordinates of two points [for example (2.0, −4.0) and
(7.0, −2.0)];
compute the slope of the line between those two points;
compute the coordinates of the midpoint of the line segment between the two points
by averaging the two x coordinates and the two y coordinates;
compute the slope of the perpendicular bisector by taking the negative reciprocal
of the slope of the line segment
compute the y intercept of the perpendicular bisector (you now have the slope m of
the bisector and a point ( xmid , ymid ) on the bisector, so the y intercept is
ymid − m*xmid ); and
output the midpoint coordinates and the equation of the bisector as shown below
Note: You need to consider the special cases where the perpendicular bisector is
vertical or horizontal

write the code using loops, if, elif, float only

# Prompt the user for input


x1 = float(input("Enter the x-coordinate of the first point: "))
y1 = float(input("Enter the y-coordinate of the first point: "))
x2 = float(input("Enter the x-coordinate of the second point: "))
y2 = float(input("Enter the y-coordinate of the second point: "))

# Compute the slope of the line between the two points


if x2 - x1 != 0:
slope = (y2 - y1) / (x2 - x1)
else:
slope = float("inf")

# Compute the coordinates of the midpoint


x_mid = (x1 + x2) / 2
y_mid = (y1 + y2) / 2

# Compute the slope of the perpendicular bisector


if slope == 0:
perp_slope = float("inf")
else:
perp_slope = -1 / slope

# Compute the y-intercept of the perpendicular bisector


if perp_slope == float("inf"):
print("Perpendicular bisector: x =", x_mid)
elif perp_slope == 0:
print("Perpendicular bisector: y =", y_mid)
else:
message = "Perpendicular bisector: y = " + str(perp_slope) + "x + " + str(y_mid
- perp_slope * x_mid)
print(message)
# Output the midpoint coordinates
message = "Midpoint: (" + str(x_mid) + ", " + str(y_mid) + ")"
print(message)

The days of the year are often numbered from 1 through 365 (or 366). This number
can be computed in three steps using int arithmetic:
(a) dayNumber = 31(month - 1) +day
(b) if the month is after February subtract (4(month) + 23)//10
(c) if it's a leap year and month after February, add 1
Write a program that reads the day, month, and the year. Then, it calculates the
corresponding day number.
A leap year is a year with 29 days.
A leap year is a year (divisible by 4 but not by 100) or divisible by 400.
The number of days in each month are as follows:
months 1,3,5,7,8,10,12 have 31 days
months 4,6,9,11 have 30 days
month 2 has 28 days for non leap year and 29 for leap year.
Assume that the user will enter integer values as input.
A valid year is any year >= 1.
A valid month is between 1 and 12
For days, valid input is according to the month number as defined above

write the code as simple as possible

The days of the year are often numbered from 1 through 365 (or 366). This number
can be computed in three steps using int arithmetic:
(a) dayNumber = 31(month - 1) +day
(b) if the month is after February subtract (4(month) + 23)//10
(c) if it's a leap year and month after February, add 1
Write a program that reads the day, month, and the year. Then, it calculates the
corresponding day number.
A leap year is a year with 29 days.
A leap year is a year (divisible by 4 but not by 100) or divisible by 400.
The number of days in each month are as follows:
months 1,3,5,7,8,10,12 have 31 days
months 4,6,9,11 have 30 days
month 2 has 28 days for non leap year and 29 for leap year.
Assume that the user will enter integer values as input.
A valid year is any year >= 1.
A valid month is between 1 and 12
For days, valid input is according to the month number as defined above.

do not use format function

Sample run 1
Enter year number: -2
Wrong input: year must be >= 1

Sample run 2
Enter year number: 1950
Enter month number: 13
Wrong input: month must be from 1 to 12

Sample run 3
Enter year number: 1990
Enter month number: 11
Enter day number: 31
Wrong input: your day must be in [1,30] range

Sample run 4
Enter year number: 1990
Enter month number: 12
Enter day number: 32
Wrong input: your day must be in [1,31] range

Sample run 5 This case for leap year


Enter year number: 2000
Enter month number: 3
Enter day number: 1
The day number is: 61

Sample run 5 This case for non leap year


Enter year number: 1999
Enter month number: 11
Enter day number: 25
The day number is: 329

A conical part (reversed cone) with base radius=R and height=R


A cylinder with height=2R and base radius=R.
If the liquid level is quite low, in the conical part, the volume is simply the
conical volume of liquid. In this case the radius of the filled part of the cone is
simply equal to d.
If the liquid level is somewhere in the cylindrical part, the total volume of
liquid includes the filled conical part and the partially filled cylindrical part.

Write a program that prompts for and reads:


the values of d (liquid level) and R (cylinder radius).
It then computes and displays the volume of the liquid in the tank.
Note that the value of d is between 0 and 3R. R has to be positive.
If d exceeds 3R, your program displays “Tank overfilled” message and stops.
If d <=0 or R <=0, it displays Wrong input message,as shown below, and stops.
If d is between 0 and R, then the filled part is a reversed cone with base radius =

𝑣𝑜𝑙𝑢𝑚𝑒 𝑜𝑓 𝑐𝑦𝑙𝑖𝑛𝑑𝑒𝑟=π𝑟2ℎ
d.

𝑣𝑜𝑙𝑢𝑚𝑒 𝑜𝑓 𝑐𝑜𝑛𝑒=13π𝑟2ℎ
where h=filled height of cylinder

Sample run 1 Enter radius of the cylinder: -4.5


Radius must be positive

Sample run 2 Enter radius of the cylinder: 2.5


Enter liquid level : -6.5
Liquid level must be positive

Sample run 3 Enter radius of the cylinder: 3.5


Enter liquid level : 16.5
Tank is overfilled

Sample run 4 This for liquid level in cone only


Enter radius of the cylinder: 12.5
Enter liquid level : 8.5
Total volume of liquid = 643.11

Sample run 5 This for liquid level somewhere in cylinder


Enter radius of the cylinder: 12.5
Enter liquid level : 28.6
Total volume of liquid = 9948.38

Write a program that outputs the equation of the perpendicular bisector of the line
segment between two points. Your program should

prompt for and input the coordinates of two points [for example (2.0, −4.0) and
(7.0, −2.0)];
compute the slope of the line between those two points;
compute the coordinates of the midpoint of the line segment between the two points
by averaging the two x coordinates and the two y coordinates;
compute the slope of the perpendicular bisector by taking the negative reciprocal
of the slope of the line segment;

compute the y intercept of the perpendicular bisector (you now have the slope m of
the bisector and a point ( xmid , ymid ) on the bisector, so the y intercept is
ymid − m*xmid ); and
output the midpoint coordinates and the equation of the bisector as shown below.
Note: You need to consider the special cases where the perpendicular bisector is
vertical or horizontal as shown in the sample runs 2 and 3.

Sample run 1
Enter x coordinate of point1: 2
Enter y coordinate of point1: -4
Enter x coordinate of point2: 7
Enter y coordinate of point2: -2
midpoint coordinates (4.50,-3.00)
Bisector equation: y = -2.5 x +8.25

Sample run 2
Enter x coordinate of point1: 2.5
Enter y coordinate of point1: 10.5
Enter x coordinate of point2: 14.8
Enter y coordinate of point2: 10.5
midpoint coordinates (8.65,10.50)
Bisector line is vertical with equation: x = 8.65

Sample run 3
Enter x coordinate of point1: 7.5
Enter y coordinate of point1: 3.8
Enter x coordinate of point2: 7.5
Enter y coordinate of point2: 20.5
midpoint coordinates (7.50,12.15)
Bisector line is horizontal with equation: y = 12.15

# Prompt for input


x1 = float(input("Enter x coordinate of point1: "))
y1 = float(input("Enter y coordinate of point1: "))
x2 = float(input("Enter x coordinate of point2: "))
y2 = float(input("Enter y coordinate of point2: "))

# Calculate midpoint
x_mid = (x1 + x2) / 2
y_mid = (y1 + y2) / 2

# Check if the line is vertical


if x1 == x2:
print("midpoint coordinates (%.2f,%.2f)" % (x_mid, y_mid))
print("Bisector line is vertical with equation: x = %.2f" % x_mid)

# Check if the line is horizontal


elif y1 == y2:
print("midpoint coordinates (%.2f,%.2f)" % (x_mid, y_mid))
print("Bisector line is horizontal with equation: y = %.2f" % y_mid)

# Otherwise, calculate the perpendicular bisector


else:
# Calculate slope of line segment
m_seg = (y2 - y1) / (x2 - x1)

# Calculate slope of perpendicular bisector


m_bis = -1 / m_seg

# Calculate y-intercept of perpendicular bisector


b_bis = y_mid - m_bis * x_mid

# Output results
print("midpoint coordinates (%.2f,%.2f)" % (x_mid, y_mid))
print("Bisector equation: y = %.2fx + %.2f" % (m_bis, b_bis))

Sample run 1
Enter x coordinate of point1: 2
Enter y coordinate of point1: -4
Enter x coordinate of point2: 7
Enter y coordinate of point2: -2
midpoint coordinates (4.50,-3.00)
Bisector equation: y = -2.5 x +8.25

Sample run 2
Enter x coordinate of point1: 2.5
Enter y coordinate of point1: 10.5
Enter x coordinate of point2: 14.8
Enter y coordinate of point2: 10.5
midpoint coordinates (8.65,10.50)
Bisector line is vertical with equation: x = 8.65
Sample run 3
Enter x coordinate of point1: 7.5
Enter y coordinate of point1: 3.8
Enter x coordinate of point2: 7.5
Enter y coordinate of point2: 20.5
midpoint coordinates (7.50,12.15)
Bisector line is horizontal with equation: y = 12.15

You might also like