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

Nick Torchio
Nick Torchio
912 Points

Unexpected Symbol

Hello community. I have worked on this code challenge for a good while and I have an error code for an unexpected symbol ")" on line 7. I'm not sure why I'm getting this error because I was under the impression that I needed that closing parentheses to close the parameters for the For loop.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for (i = 0; i < frogs.Length; i+=)

                double avg = i/frogs.Length;

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

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

2 Answers

Steven Parker
Steven Parker
231,269 Points

Your for statement is incomplete.

The immediate issue is that the last clause of your for statement is "i+=", which is incomplete. You probably intended to write "i+=1" instead.

But you might also need a different strategy for computing the average. You could declare a variable to accumulate a sum, and inside the loop add up the TongueLength values of each frog. Then after the loop ends you could divide that total by the number of frogs to get the average.

Nick Torchio
Nick Torchio
912 Points

I changed it to i++ so that it would add itself to the number and then re-start the loop (at least I think that's what that line will do). However, I still get the CS1023 error code. With that being said, would that eliminate the need to declare the TongueLength variable you had described? If I have i set to run the loop and sum its values, wouldn't that allow me to divide by frogs.Length (giving the length of the array, correct?) and then subsequently give the average? Here is my revised code also.

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

Thank you very much for your initial response Steven.

Steven Parker
Steven Parker
231,269 Points

But you don't want to divide inside the loop, you just want to add up the lengths there. Divide after the loop has finished. Here's some pseudo-code just to convey the idea:

create a total with 0 in it
loop through the frogs:
        in loop, add each frog's tongue length to the total
(loop ends)
return the total divided by the number of frogs

And "i++" is essentially the same thing as "i += 1" when used in a for loop.

Nick Torchio
Nick Torchio
912 Points

Is the i++ only adding by one or is it adding each integer/double (from the Frog array) each time the loop runs? So with the revisions I'm at:

i = 0 for( i < frogs.Length; i++) { i = i + i; } double avg = i / frogs.Length;

I still keep getting the same error (1525) but now its about an unexpected closing bracket on line 36 (my code ends at line 16). The body of my for loop I'm not sure is necessary depending on how the i++ works. If the i++ does add each integer whenever it runs the loop then I think I can cut that part out. If however, the i++ is only adding one each time through the loop, then the body of the loop is where ill do all the adding.

Edit: I keep trying to type my code out for each comment, but it keeps condensing it to one or two lines.

Steven Parker
Steven Parker
231,269 Points

I explained "i++" in my previous comment.

For code formatting, use the instructions in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: