Challenge: Function Overloading Practice
Swipe to start coding
You are building a geometry calculation tool that can compute the area of different shapes. You will implement function overloading so that the same function name calculateArea
can handle rectangles, circles, and triangles.
-
Rectangle Area Function
- Declare a function
calculateArea
that takes twodouble
parameters:length
andwidth
. - Inside the function, calculate the area by multiplying
length
bywidth
. - Return the calculated area.
- Declare a function
-
Circle Area Function
- Overload the
calculateArea
function to take onedouble
parameter:radius
. - Calculate the area using the formula
PI
multiplied byradius
squared, using thepow
function to raiseradius
to the power of 2. - Return the calculated area.
- Overload the
-
Triangle Area Function
- Overload the
calculateArea
function to take threedouble
parameters:a
,b
, andc
. - Compute the semi-perimeter
s = (a + b + c) / 2
. - Use Heron's formula:
sqrt(s * (s - a) * (s - b) * (s - c))
to calculate the area. - Return the calculated area.
- Overload the
Do not modify the value of the PI
variable.
Example
calculateArea(4, 6)
→ 24
(rectangle)
calculateArea(3)
→ 28.27431
(circle)
calculateArea(5, 4, 6)
→ 9.92157
(triangle)
Рішення
solution.cpp
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Can you explain this in simpler terms?
What are some examples related to this topic?
How does this information apply to real-world situations?
Awesome!
Completion rate improved to 5
Challenge: Function Overloading Practice
Свайпніть щоб показати меню
Swipe to start coding
You are building a geometry calculation tool that can compute the area of different shapes. You will implement function overloading so that the same function name calculateArea
can handle rectangles, circles, and triangles.
-
Rectangle Area Function
- Declare a function
calculateArea
that takes twodouble
parameters:length
andwidth
. - Inside the function, calculate the area by multiplying
length
bywidth
. - Return the calculated area.
- Declare a function
-
Circle Area Function
- Overload the
calculateArea
function to take onedouble
parameter:radius
. - Calculate the area using the formula
PI
multiplied byradius
squared, using thepow
function to raiseradius
to the power of 2. - Return the calculated area.
- Overload the
-
Triangle Area Function
- Overload the
calculateArea
function to take threedouble
parameters:a
,b
, andc
. - Compute the semi-perimeter
s = (a + b + c) / 2
. - Use Heron's formula:
sqrt(s * (s - a) * (s - b) * (s - c))
to calculate the area. - Return the calculated area.
- Overload the
Do not modify the value of the PI
variable.
Example
calculateArea(4, 6)
→ 24
(rectangle)
calculateArea(3)
→ 28.27431
(circle)
calculateArea(5, 4, 6)
→ 9.92157
(triangle)
Рішення
solution.cpp
Дякуємо за ваш відгук!
single