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
20,988 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.