Sale!

CS1026A Assignment 1 solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (2 votes)

Problem 1. This problem deals with inscribed circles. An inscribed circle is a circle which
fits maximally (with respect to area) inside another shape. In this problem we are interested
in circles inscribed within squares and triangles. Figure 1 shows a circle inscribed inside of a
square. Figure 2 shows a circle inscribed inside of a triangle. In this problem you must devise
an algorithm (try not to google it!) for determining the area of the square (resp. triangle)
excluding the area of the inscribed circle. That is, the coloured regions in each figure (the
blue region in Figure 1 and the green region in Figure 2). Let us call the algorithm which
computes areas with respect to the square the “square algorithm”. Let us call the algorithm
which computes areas with respect to the triangle the “triangle algorithm”.

Figure 1: A circle inscribed inside of a
square.
𝑂
𝐴
𝐵
𝐶
𝑐
𝑎
𝑏
Figure 2: A circle inscribed inside of a
triangle.
The written portion of this assignment is to define pseudo-code for each of those algorithms.
1. The square algorithm takes as input the side length ℓ of the square.
2. The triangle algorithm takes as input the 3 pairs of numbers (𝑥𝐴, 𝑦𝐴), (𝑥𝐵, 𝑦𝐵), (𝑥𝐶, 𝑦𝐶)
which define the Cartesian coordinates of the vertices of the triangle.
3. Your algorithm and subsequent program only needs to handle non-negative
integer inputs.
4. In your pseudo-code work generically with variables and not instances of the problem.
The programming portion of this assignment is to implement both algorithms within the
same program. To facilitate this consider the following functional specifications for your
program.
1. The program should begin by prompting the user with the question “Is your circle
inscribed in a square or a triangle?” and receiving some string as input.
2. If the user responds with “s”, “S”, “square”, “Square”, or any combination of uppercase
and lowercase letters which spell the word “square”, then your program computes the
“square algorithm”.
2 CS1026A
3. If the user responds with “t”, “T”, “triangle”, “Triangle”, or any combination of uppercase and lowercase letters which spell the word “triangle”, then your program computes
the “triangle algorithm”.
4. If the user’s input does not match any of the previous two cases then your program should print “Sorry, your input was: . This program accepts
one of “square” or “triangle”” where is replaced by the actual input of
the user. The program should then terminate without computing anything.
5. For the square algorithm:
(a) The program should then prompt the user with “The square’s side length: ” and
then receive as input a positive integer which is the square’s side length.
(b) Compute and print the area of the inscribed circle to a precision of 3 digits after
the decimal place. Include a meaningful message describing what the value is (e.g.
The area of the inscribed circle is: 23.345).
(c) Compute and print the area of the square excluding the area of the inscribed
circle (i.e. the blue shaded region) to a precision of 3 digits after the decimal
place. Include a meaningful message describing what the value is.
6. For the triangle algorithm:
(a) The problem should prompt the user with some meaningful text to enter the 3
coordinates (6 non-negative integer numbers in total) which are the coordinates
of the vertices of the triangle. How you prompt the user and how you get these 6
values is up to you.
(b) Compute and print the coordinates of the center of the inscribed circle to a precision of 2 digits after the decimal place. Include a meaningful message describing
what the value is.
(c) Compute and print the area of the inscribed circle to a precision of 4 digits after
the decimal place. Include a meaningful message describing what the value is.
(d) Compute and print the area of the triangle to a precision of 4 digits after the
decimal place. Include a meaningful message describing what the value is.
(e) Compute and print the area of the triangle excluding the area of the inscribed
circle (i.e. the green shaded region) to a precision of 3 digits after the decimal
place. Include a meaningful message describing what the value is.
7. If, at any time, the user inputs a non-number where a number is expected, the program
should output “Sorry, was not a number.” where is replaced by
the actual input of the user.
Some useful information.
• To test if a string is an integer you can use the isnumeric() method.
isANumber = “245”.isnumeric()
3
• The distance 𝑑 between two Cartesian points 𝑝1 = (𝑥1, 𝑦1) and 𝑝2 = (𝑥2, 𝑦2) is
𝑑 =
⌈︂
(𝑥1 − 𝑥2)
2 + (𝑦1 − 𝑦2)
2
• The area of a circle with radius 𝑟 is
𝐴𝑟𝑒𝑎 = 𝜋𝑟2
• The area of a triangle with side lengths 𝑎, 𝑏, 𝑐 is
𝐴𝑟𝑒𝑎 =
⌈︂
𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐), 𝑠 =
(𝑎 + 𝑏 + 𝑐)
2
• Given the Cartesian coordinates of a triangle’s vertices 𝐴 = (𝑥𝐴, 𝑦𝐴), 𝐵 = (𝑥𝐵, 𝑦𝐵), 𝐶 =
(𝑥𝐶, 𝑦𝐶), the center of the triangle’s inscribed circle is 𝑂 = (𝑥𝑂, 𝑦𝑂),
𝑥𝑂 =
(𝑎𝑥𝐴 + 𝑏𝑥𝐵 + 𝑐𝑥𝐵)
𝑝
, 𝑦𝑂 =
(𝑎𝑦𝐴 + 𝑏𝑦𝐵 + 𝑐𝑦𝐶)
𝑝
where 𝑝 is the triangle’s perimeter 𝑝 = 𝑎 + 𝑏 + 𝑐. Note that side 𝑎 is opposite vertex 𝐴;
similarly for 𝑏 and 𝑐 (see Figure 2).
• The radius 𝑟 of a circle inscribed in a triangle with side lengths 𝑎, 𝑏, 𝑐 is
𝑟 =
⌈︂
𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
𝑠
, 𝑠 =
(𝑎 + 𝑏 + 𝑐)
2
4