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 trialJonathan Barnthouse
2,801 PointsNeed 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
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);
}
}
}
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
3 Answers
markmneimneh
14,132 PointsHello
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
HIDAYATULLAH ARGHANDABI
21,058 PointsTry 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
2,337 PointsFyi - You don't need an increment (i++) in foreach.
Jonathan Barnthouse
2,801 PointsJonathan Barnthouse
2,801 PointsThank you for the support! I can see it now. u the best!