0

i am trying to find the area of given no equilateral triangle. 1st there is a big equilateral triangle with length "l" and from its all sides new equilateral triangle is formed with "l/3" length again from all 3 triangle's free side(i.e only 2 side) new triangle is formed with length "l/9".

so i need to find the total area of all triangles if no of repetition and length is given using recursion

So here the code that i have tried. It gives the correct result for 2 up to repetitions and the wrong result for more:

Module Module1
    Dim noOfTriangles As Single = 3 / 2

    Function AreaOfTriangle(ByVal noOfRepetition As Integer, ByVal length As Double)
        If noOfRepetition = 0 Then
            Return Nothing
        Else
            noOfTriangles = noOfTriangles * 2
            Return (((3 ^ (1 / 2)) / 4) * (length ^ 2) + noOfTriangles * AreaOfTriangle(noOfRepetition - 1, length / 3))
        End If

    End Function

    Sub Main()
        Dim area As Double
        area = AreaOfTriangle(3, 9)
        Console.WriteLine(area)
        Console.ReadKey()
    End Sub 
End Module
1

2 Answers 2

3

I don’t understand the exact problem description but here are a few things in your code that need fixing.

  • First order of business, enable Option Strict in the project options. Always. Then your code will no longer compile because it contains bugs.

  • The return type of your function is missing. It’s probably (!) Single or Double.

  • Return Nothing makes no sense. An area is never “nothing”. It can, however, be 0.

  • Do not use a module-wide variable, use another parameter instead.

  • Initialise variables immediately when you declare them: Dim area As Double = AreaOfTriangle(…).

  • Make the logic clearer. I really have no idea what the code does. This includes using proper names. noOfTriangles is of type Single and has the initial value 3 / 2. That certainly makes no sense: a number of triangles will always be a integer number, never a fraction.

Sign up to request clarification or add additional context in comments.

1 Comment

i know the program is pretty messed up : but for noOfTriangles ,1st value 3 was required,and then 6 ,then 12 and so on so i gave initial value 3/2 which when multiplied by 2 gives 3 then and again 3 when multiplied by 2 gives 6 and so on i know this is really messed one but for the flow i did so .(#i am still amateur programmar)
0

You don't need to multiply by the noOfTriangles or else you will be double-counting. You are multiplying the number of triangles at one level of a recursion by an area. But your area itself is a recursion, so you are calculating the area of all of the triangle to the bottom of the recursion. That means, at the 3rd iteration, there aren't "6 triangles", there are just "2 triangles", in fact at every iteration, there are just "2 triangles", except for the 1st one. The fact that visually there are 6, 12, 24, etc. in handle by the multiplication outside of the call to AreaOfTriangle.

Replace this line

noOfTriangles = noOfTriangles * 2

With this:

If (noOfTriangles = 3 / 2) Then
    noOfTriangles = 3
Else
    noOfTriangles = 2
End If

And it should work.

4 Comments

at first there a triangle ...... at 2nd iteration noOfTriangles =3......at 3rd iteration noOfTriangles=6(2 from each 3 new triangles)........ at 4th iteration noOfTriangles=12(2 from each 6 new triangles)
I know. But your recursion is handling that for you. You don't need to keep multiplying the noOfTriangles by 2. Trust me. Did you try my code?
You can view the recursion two ways: find the area of all triangles at one level of the recursion or, find the area of one triangle (and all triangles attached to that triangle) all the way to the bottom of the recursion. Your code was doing some combination of both of those methods at the same time. The line "noOfTriangles * AreaOfTriangle" is taking care of doing the "noOfTriangles * 2" thing for you.
Your code was saying, add the area of the first triangle plus 3 times the area of the next triangle. The area of each of those triangles is the area of the equilaterial plus 6 * area of the next triangle. And that was the mistake. There are only two triangles, not 6. There are 6 all toghether, but your recursion is doing the 3 * 2 bit for you, I don't know another way to explain it, and I would not have structured my code the way you did. See Konrad's explanation regarding that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.