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

How do I convert an int to a double, and is that my problem with this challenge?

The problem is to get the average tongue length, but I am unsure why this will not compile.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double final = 0.0; 

            for(double i = 0.0; i <  frogs[i].TongueLength; i++)
            {
                final = final + frogs[i];
            } 
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public double TongueLength { get; }

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

2 Answers

Allan Clark
Allan Clark
10,810 Points

A couple issues with your syntax here. First, the index of a for loop must be an int. Secondly, you want to loop through the array (i < frogs.Length) and add the current index's TongueLength property.

The double only comes into play when you compute the average. So the idea is to add them all up and return the result divided by the number of frogs.

Should end up looking something like this:

public static double GetAverageTongueLength(Frog[] frogs)
        {
            int sum = 0; //the variable we use to keep track of the running sum of TongueLengths 

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

Thank you! This worked.