Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches For Loops

Return the average length of the tongues of the frogs in the array. Use a for loop as part of your solution.

Hello, Guys I need your help... I can't figure it out... I tried every thing... Can some one please send me the answer and explanation so I can learn from it?

2 Answers

Are you returning the average length of the tongues of the frogs in the array?

public static double GetAverageTongueLength(Frog[] frogs) {
int tongues = 0;

        for(int i = 0; i < frogs.Length; i++)
        {                         
            tongues = frogs[i].TongueLength;                                            
        }

        return tongues / frogs.Length;
    }
Steven Parker
Steven Parker
231,268 Points

You might find folks more eager to help if you post the code you have attempted to get hints and suggestions on how to correct it. In the meantime, I'll offer a few generic strategy hints:

  • create a "for" loop that steps through the list of frogs
  • for each frog, add the length of the tongue to a running total
  • after the loop completes, divide the total by the number of frogs to get the average
  • remember to return the average value
Steven Parker
Steven Parker
231,268 Points

The TongueLength is a property each frog has, so you'll access it in the loop and add it to the running total.