0

I'm working on a project that involves importing data from a CSV file into a C# program using an array and returning values such as the minimum and maximum. I've been having a bit of trouble implementing a way to return the max and min values.

Samples class:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Thomas_Nicholas_E2
{
    class Samples
    {
        public double Fenner { get; set; }
        public double Abom { get; set; }

        public Samples()
        {

        }

        public void ReadCsv(StreamReader reader)
        {
            string buffer;
            string[] parts;

            // Get record from file
            buffer = reader.ReadLine();

            // Break record into components
            parts = buffer.Split(',');

           /* Array.Sort<string>(parts);
            foreach (var x in parts)
            {
                Console.WriteLine(x);
            }
            */
            // Convert field values to variable
            Fenner = Convert.ToDouble(parts[0]);
            Abom = Convert.ToDouble(parts[1]);


            // Finding the range of the datasets 
            //var sortedFenner = parts[0].OrderBy(i => i);
            //var sortedAbom = parts[1].OrderBy(i => i);
            //var sortedTotal = parts.OrderBy(i => i);
            //Console.WriteLine(sortedAbom);
            //Console.WriteLine(parts.Max());
            //double minimum1 = Math.Min(parts, parts);
            //Console.WriteLine(parts[1].Min());

            //string min = parts[0].Min();
            // Console.WriteLine(min);
            //double min = parts[1].Min();
            //Console.WriteLine(min);
        }

    }
}

You can see what I've tried so far in the commented out section at the bottom of the Samples class.

Programs class just in case:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Thomas_Nicholas_E2
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader("examdata.csv");

            string buffer;

            double totalFenner = 0.0;
            double totalAbom = 0.0;
            double total = 0.0;
            double minumum = 0.0;
            double maximum = 0.0;
            double range = 0.0;
            double meanFenner = 0.0;
            double meanAbom = 0.0;
            double meanTotal = 0.0;

            // Read header to get it out of the way
            buffer = reader.ReadLine();

            while(!reader.EndOfStream)
            {
                Samples MySamples = new Samples();
                MySamples.ReadCsv(reader);

                // Processing
                //Calculating totals
                totalFenner += MySamples.Fenner;
                totalAbom += MySamples.Abom;
                total = (totalFenner + totalAbom);

                //Calculating means
                meanFenner = (totalFenner / 20);
                meanAbom = (totalAbom / 20);
                meanTotal = (total / 40);


                // += MySamples;

                //Console.WriteLine("TransId: {0}  DeptNo: {1}  Amt: {2}", transId, deptNo, amt);
                //MyTransaction.Print();
            }
            Console.WriteLine(meanFenner);
            Console.WriteLine(meanAbom);
            Console.WriteLine(meanTotal);
            //Console.WriteLine(Samples.sortedAbom)




            reader.Close();

            Pause();
        }
        private static void Pause()
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

I appreciate any advice or guidance, thank you.

5
  • 5
    Read all items to a collection and use System.Linq namespace and Min() Max() methods. Commented Dec 9, 2018 at 23:01
  • 1
    It seems you are only reading one line from the csv file. you need to read all lines. Commented Dec 9, 2018 at 23:05
  • 2
    string.Split() is an awful way to handle csv values. Commented Dec 9, 2018 at 23:23
  • You have an array of two values part[0] is the first value of an array, not an array, this is why you cannot call part[0].Min() Commented Dec 9, 2018 at 23:26
  • It really would help if you posted a few lines from examdata.csv. Commented Dec 10, 2018 at 0:09

3 Answers 3

1

Build the ReadCsv() method in the Samples class with overloaded Factory pattern methods, like this:

public static Samples ReadCsv(StreamReader reader)
{
    return ReadCSv(reader.ReadLine());
}

public static Samples ReadCsv(string line)
{
    parts = line.Split(',');
    return new Samples() {
        Fenner = Convert.ToDouble(parts[0]),
        Abom = Convert.ToDouble(parts[1])
    };
}

And then you can use it from your Main() method like this:

static void Main(string[] args)
{
    var samples = File.ReadLines("examdata.csv")
                 .Select(l => Samples.ReadCsv(l))
                 .ToList();

    double totalFenner = samples.Select(s => s.Fenner).Sum();
    double totalAbom = samples.Select(s => s.Abom).Sum();
    double total = totalFenner + totalAbom;

    double meanFenner = sampes.Select(s => s.Fenner).Avg();
    double meanAbom = samples.Select(s => s.Abom).Avg();
    double meanTotal = samples.Select(s => s.Fenner + s.Abom).Avg();

    Console.WriteLine(meanFenner);
    Console.WriteLine(meanAbom);
    Console.WriteLine(meanTotal);

     Pause();
}

or like this (more code, but runs faster and easier for some to understand):

static void Main(string[] args)
{
    double totalFenner = 0.0;
    double totalAbom = 0.0;
    double total = 0.0;

    double meanFenner = 0.0;
    double meanAbom = 0.0;
    double meanTotal = 0.0;

    var samples = File.ReadLines("examdata.csv")
                 .Select(l => Samples.ReadCsv(l));

    int count = 0;
    foreach (var sample in samples)
    {
        totalFenner += sample.Fenner;
        totalAbom += sample.Abom;
        count++;
    }
    total = totalFenner + totalAbom;
    meanTotal = total / count;
    meanFenner = totalFenner / count;
    meanAbom = totalAbom / count;

    Console.WriteLine(meanFenner);
    Console.WriteLine(meanAbom);
    Console.WriteLine(meanTotal);

     Pause();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, this really helped me out.
0

Finding the min/max is trivially easy. Assuming an array or list you can simply call .Min() or .Max() on it. For example:

var foo = new List<int> {4,3,5,9,1};
var min = foo.Min();
var max = foo.Max();

Comments

0

I thought it might be useful to provide some code for some of the answers that I've seen in the comments.

To read all the lines you could do something like:

    using (StreamReader reader = new StreamReader("examdata.csv")) {

      while ((buffer = reader.ReadLine()) != null)
      {
         // Something
      }

    }

And then if you're using an array, you can do something like what @gilliduck said and find the min/max values with .Min() or .Max().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.