Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Loops and Final Touches For Loops

how to finish loop

I feel like I am close to finishing this but simply cannot figure out how to complete this loop. A little bit of help would be appreaciated.

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

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

1 Answer

Tim Danner
Tim Danner
3,281 Points

First, you're accessing the class directly instead of its instances, and second, you're assigning the values of type int to the variable sum of type double.

The solutions would be to iterate through the Frog array that's provided in the parameter, and to cast TongueLength to double:

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double sum = 0;
            double i = 0;
            for (; i < frogs.Length; i++)
            {
                // explicit cast by appending (double) 
                sum += (double)frogs[i].TongueLength;
            }
            i++;
            return sum / i;
        }
    }
}

While the solution above is legit, the codes could look a little bit confusing to other coders.

I'd suggest keeping i inside the for loop because it doesn't have any specific meaning outside the scope, and casting sum to double at the end of the calculation for better performance:

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            // double sum = 0;
            // double i = 0;

            int sum = 0;

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

            return (double)sum / frogs.Length;
        }
    }
}

In C#, when it comes to division, as long as either side is of type double, the return value will always be double.

You can verify this by calling the System method GetType():

Console.WriteLine((5.0/2).GetType());