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.

I am a bit lost on this question. I am pretty sure I have not used the correct method to calculate the average length. I have just copied what we have just learned if someone could please help with the correct method

Thank you

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for(double i = 0; i < frogs.Length; i++)
            {
                Frog frog = frog[i];
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,732 Points

Heres a few hints:

  • before the loop, declare a value to store the total of the lengths
  • use an int for an index instead of a double
  • the array name is "frogs" (with an "s")
  • after selecting the individual frog from the array you can access its TongueLength
  • in the loop, add each length to the total
  • after the loop is finished, divide the total by the count to get the average
  • finally, return the average

I really appreciate the hints and not just giving us the answer! I didn't think about storing the value of total lengths before the loop then returning the average outside the loop. Thanks!

Steven Parker
Steven Parker
229,732 Points

Thanks for the feedback! I'm strongly resistant to giving "spoilers" because I also find that working things out from hints creates a better learning experience.