C# C# Objects Loops and Final Touches Foreach Loops

help on foreach loops

i absolutely cannot figure this out. can someone please tell me what i need to fill into the for each loop?

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

            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

2 Answers

andren
andren
28,391 Points

The structure of a foreach loop is this:

foreach(Type variableName in List/Array)
{

}

Each time though the loop one item will be pulled from the list/array and will be assigned to the variable you declared which will be accessible within the loop. Here is a more practical example showing a foreach loop used for printing out the contents of a string array:

string[] names = {"Eric", "John", "Elizabeth", "William", "Tom", "Ellitot"};
foreach(string name in names)
    {
        Console.WriteLine(name);
    }

/* This will print:
Eric
John
Elizabeth
William
Tom
Ellitot
*/

In the above program the name variable is set equal to the first item in the names array during the first loop ("Eric") then the second item during the second run ("John") and so on until the loop has gone though all of the items. After going through all of the items the loop exits automatically.

In this challenge you have to use the foreach loop to loop though an array of Frog objects so the structure is pretty similar to the for loop above:

foreach(Frog frog in frogs)
{

}

That is how the for loop itself has to be for this task, I'll leave figuring out the rest of the code to you. By looking at the examples above and using your existing C# knowledge it should not be too difficult to solve this task. Though if you get completely stuck I can post the solution for you if you want.

ok i just want a pointer as to what to fill into the loop. everything i try i get a compiler error.

I am trying it in workspaces but I can't figure out how to total it all up and get the average. I just get compiler errors.

Steven Parker
Steven Parker
177,671 Points

:point_right: I'm guessing you don't want a total spoiler, so here's a few hints:

  • you probably want to use the loop to examine each frog in the list
  • so your loop might be something like: foreach (var frog in frogs)
  • then inside your loop, you might want to use total to accumulate the sum of the tongue lengths
  • finally, after the loop you might divide the total by the number of frogs to get the average
  • then you would return that average
Steven Parker
Steven Parker
177,671 Points

:expressionless: Okay, so maybe you do want a "spoiler":

So, just implementing the hints already given above:

            int total = 0;
            foreach (var frog in frogs)
            {
                total += frog.TongueLength;  // accumulate sum of tongues
            }
            return total / frogs.Length;     // divide by number of frogs and return average

But next time, show us the code that's causing the error so we can try to help you fix it.