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 Foreach Loops

Need help Return the average length of the tongues of the frogs in the array. Use a foreach loop as part of your solutio

Lost. Have looked at the other solutions in the treehouse community for this task and could not figure the solution \ proper C# code. Seeking the actual code that works to study it.

Thanks N00Be

FrogStats.cs
using System;

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


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

            //Console.WriteLine(avg/i);
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

3 Answers

Hello

please review this code

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double sumTongueLength = 0.0D;
            int i = 0;
            foreach (Frog frog in frogs)
            {
                  sumTongueLength = sumTongueLength + frog.TongueLength;
                i++;
            }
            return sumTongueLength/i;
        }
    }
}

change as you see fit

Thank you for the support! I can see it now. u the best!

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

Try this inside the metho

 double total=0;
            for(int i=0;i<frogs.Length;i++)
            {
                total+=frogs[i].TongueLength;
            }
            return total/frogs.Length;
Chanuka Weerasinghe
Chanuka Weerasinghe
2,337 Points

Fyi - You don't need an increment (i++) in foreach.