Hi, I am new in learning c#, and have recently encountered a problem:
It comes with a exception says "sum" is "use of unassigned local variable". Since I have already declared "sum" outside the "for" loop, why it says "sum" is unassigned? Thanks!
Code:
static float ComputeAvg(float[] a)
{
float sum;
int i;
for (i = 0; i <= a.Length; i++)
{
sum = sum + a[i];
}
float avg = sum / (i-1);
return avg;
}
Comment