简体   繁体   中英

How to use local list as an input parameter for function

I am working on a project for school, and having quite a time with getting it to work.

The program flow is as follows:

Here is the function doing all of the statistics with its struct:

        public struct DescriptiveStat
    {
        public double max, min, sum, avg, var, stddev;
        public string range;
    }

    public static DescriptiveStat GetDescriptiveStat(List<double> data)
    {
        DescriptiveStat result = new DescriptiveStat();
        double sum = 0, sqSum = 0, max = data[0], min = data[0];
        foreach (double x in data)
        {
            sum += x;
            sqSum += Math.Pow(x, 2);
            if (x > max)
                max = x;
            if (x < min)
                min = x;
        }
        int n = data.Count;
        result.sum = sum;
        result.max = max;
        result.min = min;
        result.avg = sum / n;
        result.var = (sqSum - n * Math.Pow(result.avg, 2)) / n;
        result.stddev = Math.Sqrt(result.var);
        return result;
    }

The function is set by my teacher, and cannot be changed. It must take the List<double> data and return result.

Currently I have it so that when I press the button "Generate" in the form "DescriptiveStat" with the single variable, it calls my function to load the data into the list to prepare it to send it to DescriptiveStat function above. Here is my code:

        public void GetDescriptiveStat()
    {
        List<double> data = new List<double>();
        double temp;
        data.ToArray();
        for(int i = 0; i < 94; i++)
        {
            temp = dblValues[i][0];
            data.Add(temp);
        }

        data.ToList();
        //data.ForEach(Console.WriteLine);

    }

I know it is reading the data into the list correctly, but I am having a problem with using this local list as a parameter for function DescriptiveStat GetDescriptiveStat(List<double>data) - when I try inserting something like double result = Mathtool.GetDescriptiveStat(data); it gives me the error:

  • Cannot implicitly convert type 'MultivariateStatistics.Mathtool.DescriptiveStat' to 'double'

Can anyone please assist with what I am doing wrong, or how I can use the list as a parameter for the function?

Thank you.

First you don't have to convert your list to an array ( data.ToArray() ) and then back to a list ( data.ToList() ). Second instead of void let your function return List<double> like shown below:

public List<double> GetDescriptiveStat()
{
    List<double> data = new List<double>();
    for (int i = 0; i < 94; i++)
    {
        data.Add(dblValues[i][0]);
    }
    return data;
}

Now because your function GetDescriptiveStat() returns a list you can send it as an input parameter into the teachers function GetDescriptiveStat(List<double> data) :

DescriptiveStat myData = GetDescriptiveStat(GetDescriptiveStat());

There are maybe better ways to do this but you explicitly said that the teachers function cannot be changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM