user1303781
user1303781

Reputation: 11

Where to declare variable?

I am trying to make an average function. Total adds the values, then Total is divided by n, the number of entries.

No matter where I put double Total;, I get an error message. In this example I get the exception:

    Use of unassigned local variable 'Total'

If I put it before the Average method, both references show up as error. I'm sure it's something simple ...

namespace frmAssignment3
{
    class StatisticalFunctions
    {
        public static class Statistics
        {
            public static double Average(List<double> argMachineDataList)
            {
                double Total;

                int n;
                for (n = 1; n <= argMachineDataList.Count; n++) {
                    Total = argMachineDataList[n];
                }

                return Total / n;
            }

            public static double StDevSample(
                List<MachineData.MachineRecord> argMachineDataList)
            {
                return -1;
            }
        }
    }
}

Upvotes: 1

Views: 205

Answers (9)

James Johnson
James Johnson

Reputation: 46077

This should fix the issue...

double Total = 0;

The thing is, what you are trying to do, is using a variable that has not been assigned a value yet. Before you use a variable, you must assign it a value.

What you originally had, double Total; is declaring the variable, not assigning it.

Upvotes: 0

Omar
Omar

Reputation: 16621

You need to initialize Total:

double Total = 0;

or as said @Kirk Woll use linq:

double Total = argMachineDataList.Average();

to avoid to write:

for (int n = 0; n < argMachineDataList.Count; n++)
//the first index of the list start from 0 and the last one is n-1 so      
//argMachineDataList.Count-1 so you need < not <=
{               
     Total += argMachineDataList[n]; 
     //Remember that if you want to 
     //increment total you need the += instead of =
}

return Total / argMachineDataList.Count;
//Then remeber that n int this case is a local variable so you can't use it out 
//of the for loop

Upvotes: 1

tletnes
tletnes

Reputation: 1998

Your loop may only run once, leaving Total unassinged. Init Total when you declare it.

double Total = 0.0;

Upvotes: 0

Tester101
Tester101

Reputation: 8182

public static double Average(List<double> argMachineDataList)
{
    //First lets handle the case where argMachineDataList is empty.
    // Otherwise we'll return Double.NaN when we divide by zero.
    if(argMachineDataList.Count == 0)
    {
        throw new ArgumentException("argMachineDataList cannot be an empty List.");
    }

    //Second we have to assign an initial value to Total, 
    // because there is no guarantee it will be set in the loop. 
    //And since it's a local variable, it will not automatically be set.
    double Total = 0.0;

    //Next since Lists are Zero Base indexed, 
    // we'll want to start with n = 0 so we include the first element.
    //We also want the loop to stop before n = argMachineDataList.Count, 
    // or we'll get an ArgumentOutOfRange Exception when trying to access 
    //  argMachineDataList[n] (when n = argMachineDataList.Count).
    for (int n = 0; n < argMachineDataList.Count; n++)
    {
        //Since we want to add the value of argMachineDataList[n] to Total, 
        // we have to change from = to += 
        //   (which is the same as saying Total = Total + argMachineDataList[n]).
        Total += argMachineDataList[n];
    }
    //Lastly, n will be out of scope outside of the for loop. 
    // So we'll use argMachineDataList.Count, to get the number of items.
    return Total / argMachineDataList.Count;
}

Upvotes: 0

Jetti
Jetti

Reputation: 2458

Just do this and you'll be fine:

    public static double Average(List<double> argMachineDataList)
    {
        double Total = 0.0;

        int n;
        for (n = 0; n < argMachineDataList.Count; n++)
        {
            Total += argMachineDataList[n]; // added += since you probably want to sum value
        }

        return Total / n;
    }

I also added a += between Total and your argMachineDataList[n] as since you want the total you need to actually sum up the values.

Upvotes: 1

Christopher Jones
Christopher Jones

Reputation: 257

The issue here is that there is no guarantee that the item being passed by parameter (argMachineList) will have a Count property that is greater than or equal to 1 (which is necessary for the loop to iterate AT LEAST once). If someone were to pass new List<double>() into this function, it would run and compile, but the for loop will never enter since the Count property would be 0. So it is completely possible that the for loop will never iterate once, the value of Total is never set, and thus you won't be able to return properly since your return statement would basically be "pointer to double / some integer n". Notice that if you were to pull Total = argMachineList[1] (for example) outside of the for loop, the compiler error disappears.

Everyone's simple suggestion of setting Total's default value to 0 eliminates the problem; Total will always be able to return a value since it's value will be set regardless of whether or not the for loop iterates once.

    public static double Average(List<double> argMachineList)
    {
        double Total = 0.0;
        int n;
        for (n = 0; n < argMachineList.Count; n++)
        {
            Total += argMachineList[n];
        }
        return Total / argMachineList.Count;
    }

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 161002

You have several bugs in your code, one that hasn't been mentioned yet is an "off by one" bug in your for loop - the first element you are adding has an index of 1, but the first item in the collection has an index of zero:

double total = 0;
for (int n = 0; n < argMachineDataList.Count; n++)
{
    total += argMachineDataList[n];
}

return total / argMachineDataList.Count; // will throw exception if n = 0

Upvotes: 0

Khan
Khan

Reputation: 18172

To get rid of your issue, give Total a default value:

double Total = 0;

Also, you are not adding to total. So change your = to +=:

Total += argMachineDataList[n];

Upvotes: 5

McAden
McAden

Reputation: 13970

you need to initialize total

double total = 0.0;

You're also starting with the 2nd element instead of the first by setting n = 1 in the for loop.

Upvotes: 0

Related Questions