Home > Software design >  Reading numbers from CSV file and calculating the average
Reading numbers from CSV file and calculating the average

Time:03-10

I need help taking numbers from a CSV file and calculating the average. So far I can retrieve the correct numbers from the last column, but it seems like I am not converting them to right type of array. I think the number I am looking for should be Average = 6.4.

private void label21_Click(object sender, EventArgs e)
{
    var path = conf.path   "\\"   "CloudOpen.csv"; 

    using (TextFieldParser csvParser = new TextFieldParser(path))
    {
        csvParser.CommentTokens = new string[] { "#" };
        csvParser.SetDelimiters(new string[] { "," });
        csvParser.HasFieldsEnclosedInQuotes = false;
        csvParser.ReadLine();

        while (!csvParser.EndOfData)
        {
            // Read current line fields, pointer moves to the next line.
            string[] fields = csvParser.ReadFields();
            string five = fields[5]; 
            var intArray = five.Select(c => c - '0').ToArray(); 

            Average(intArray);
        }
    }
}

public void Average(int[] array)
{
    double avg = Queryable.Average(array.AsQueryable());

    Console.WriteLine("Average = "   avg);
}

Here is the CSV file I am reading:

Id,time,two,five,ten,twenty
0,03/07/2022 14:47:03,0,1,2,5
0,03/07/2022 14:47:33,0,1,2,6
0,03/07/2022 14:48:37,0,1,3,6
0,03/07/2022 14:48:37,0,1,3,6
0,03/07/2022 14:48:37,0,1,3,7
0,03/07/2022 14:48:37,0,1,3,8

CodePudding user response:

Creating a .NET 6.0 command line project you can run the following code without worrying about namespaces, classes, etc. I believe you may need to either round to one decimal or use the ceiling for your use case?

//Add six values to list of integers (simplifying the CSV to Array code)
List<int> listInts = new List<int>() { 5, 6, 6, 6, 7, 8 };

double avg = Queryable.Average(listInts.AsQueryable()); //This variable will be a double of 6.333 repeating
double avgOneDecimal = Math.Round(avg, 1); //This variable will round to one decimal place
double avgOneDecimalCeiling = Math.Ceiling(avg); //This variable will be 7 if you need to round up for whatever reason

Console.WriteLine($"Average = {avg}");
Console.WriteLine($"Average to one decimal = {avgOneDecimal}");
Console.WriteLine($"Average rounded up = {avgOneDecimalCeiling}");

CodePudding user response:

public void average()

        {

            List<double> listA = new List<double>();

            int five;

            var path = conf.path   "\\"   "CloudOpen.csv"; 

            using (TextFieldParser csvParser = new TextFieldParser(path))

            {

                csvParser.CommentTokens = new string[] { "#" };

                csvParser.SetDelimiters(new string[] { "," });

                csvParser.HasFieldsEnclosedInQuotes = false;

                csvParser.ReadLine();

                while (!csvParser.EndOfData)

                {
                    string[] fields = csvParser.ReadFields();

                    five = fields[5].Count();
   
                    var fi = Convert.ToInt32(fields[5]);

                    listA.Add(fi);

                }

                int total = listA.Sum(x => Convert.ToInt32(x));

                var count = listA.Count;

                Console.WriteLine("Numbers added:"   total);

                Console.WriteLine("Number of items:"   listA.Count);

                Console.WriteLine(total / count);

            }

        }
  • Related