Hi, i'm new here so bear with me on this. I have a task, due in on Friday, to read data from a file, do some calculations with it such as averages and standard deviation and then read it out to a file. I'm about half way though but I just can't seem to a) fully grasp what i'm doing and b) finish off the code in a decent manner. Also, when trying to read in the month values in the same way as the year values, the executable puts in a space between each line for no apparent reason and after hitting "enter" many times (the .csv files are large) it restarts Visual C#.

What I have up to now is:

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

namespace Assignment1
{
    class Assignment1
    {
        public static void Main()
        {
            // initial variables
            string fileName = "sheffielddata.csv";

            /* set up stream to file
            StreamReader read = new StreamReader(fileName);

            // while the file still has unread lines ...
            while (read.EndOfStream == false)
            {
                // read the next line and print it to the screen
                string line = read.ReadLine();
                Console.WriteLine(line);
            }
            read.Close();*/

            // Finds out how big the file is
            int nLines = 0;
            StreamReader reader = new StreamReader(fileName);
            while (reader.EndOfStream == false)
            {
                string line = reader.ReadLine();
                nLines = nLines + 1;
            }
            Console.WriteLine("Weather records for " + fileName);

            // create the array to store the number of lines
            //int[] noLines = new int[count];

            // now close the file
            reader.Close();

            // initialise array with number of elements
            string[] weatherData = new string[nLines];

            StreamReader read = new StreamReader(fileName);
            int count = 0;
            while (read.EndOfStream == false)
            {
                // read a line into our program
                weatherData[count] = read.ReadLine();
                count = count + 1;
            }
            read.Close();
            //Console.WriteLine(weatherData[0]);


            // split each string into the separate component elements 


            // set deliminater
            char[] delimiter = new char[] { ',' };

            // calculate the number of elements in a record (we assume that
            // every record has the same number of elements)
            int nElements = (weatherData[0].Split(delimiter)).Length;
            //Console.WriteLine(nElements);
            Console.ReadLine();

            // initialise the array
            string[,] weatherRecords = new string[nLines, nElements];

            // read record elements into array
            for (int i = 0; i < nLines; i++)
            {

                string[] tmp = weatherData[i].Split(delimiter);
                for (int j = 0; j < nElements; j++)
                {
                    weatherRecords[i, j] = tmp[j].Trim(null);

                }

            }
            // In theory we now have the .csv file as a 2D array
            for (int i = 0; i < nLines; i++)
            {
                for (int j = 0; j < nElements; j++)
                {
                    // Console.WriteLine(weatherRecords[i,j]);  
                }

            }

            {
                // search for a record by year

                Console.WriteLine();
                Console.WriteLine("To search for a set of data, enter the year:");
                string nyear = Console.ReadLine();
                int nYear = int.Parse(nyear);
                Console.Clear();
                Console.WriteLine("Shows high and low temps, average frost and rainfall in mm for the year {0}", nyear);
                Console.WriteLine();
                Console.WriteLine("{0,-15:0}{1,-15:0}{2,-15:0}{3,-15:0}", "Max temp", "Low temp", "Av frost", "rainfall(mm)");
                // We could put in error checking to see that it is a valid year.

                float maxTemp = 0;
                float minTemp = 999;
                double max = 0;
                double min = 999;
                double average = 0, totalMax = 0, totalMin = 0;

                for (int i = 0; i < nLines; i++)
                {
                    int yearArrval = int.Parse(weatherRecords[i, 0]);
                    // match the year and show it's details
                    if (yearArrval == nYear)
                    {
                        if (float.Parse(weatherRecords[i, 2]) > maxTemp)
                        {
                            maxTemp = float.Parse(weatherRecords[i, 2]);
                        }

                        if (float.Parse(weatherRecords[i, 3]) < minTemp)
                        {
                            minTemp = float.Parse(weatherRecords[i, 3]);
                        }

                        Console.WriteLine("{0,-15:0}{1,-15:0}{2,-15:0}{3,-15:0}", weatherRecords[i, 2], weatherRecords[i, 3], weatherRecords[i, 4], weatherRecords[i, 5]);

                        //find max temp by year
                        double yearTempmax = double.Parse(weatherRecords[i, 2]);
                        if (yearTempmax > max)
                        {
                            max = yearTempmax;
                        }

                        //find min temp by year
                        double yearTempmin = double.Parse(weatherRecords[i, 3]);
                        if (yearTempmin < min)
                        {
                            min = yearTempmin;
                        }

                        // find average year temp
                        if (nYear == yearArrval)
                        {
                            totalMax = totalMax + maxTemp;
                            totalMin = totalMin + minTemp;
                            average = (totalMin + totalMax) / 24;
                        }

                    }
                }
                Console.WriteLine("Max temp in year " + nyear + " is " + maxTemp);
                Console.WriteLine("Min temp in year " + nyear + " is " + minTemp);
                Console.WriteLine("The average temperature in " + nyear + " is " + Math.Round(average, 2) + (char)176 + "c");

                {
                    // search for a record by month

                    Console.WriteLine();
                    Console.WriteLine("To search for a set of data, enter the month (1 for Jan, 2 for Feb etc:)");
                    string nmonth = Console.ReadLine();
                    int nMonth = int.Parse(nmonth);
                    Console.Clear();
                    Console.WriteLine("Shows high and low temps, average frost and rainfall in mm for the month {0}", nMonth);
                    Console.WriteLine();
                    Console.WriteLine("{0,-15:0}{1,-15:0}{2,-15:0}{3,-15:0}", "Max temp", "Low temp", "Av frost", "rainfall(mm)");
                    //We could put in error checking to see that it is a valid month.

                    float maxTemp2 = 0;
                    float minTemp2 = 999;
                    double max2 = 0;
                    double min2 = 999;
                    double average2 = 0, totalMax2 = 0, totalMin2 = 0;

                    for (int i = 0; i < nLines; i++)
                    {
                        int monthArrval = int.Parse(weatherRecords[i, 1]);
                        // match the month and show it's details
                        if (monthArrval == nMonth)
                        {
                            if (float.Parse(weatherRecords[i, 2]) > maxTemp2)
                            {
                                maxTemp2 = float.Parse(weatherRecords[i, 2]);
                            }

                            if (float.Parse(weatherRecords[i, 3]) < minTemp2)
                            {
                                minTemp2 = float.Parse(weatherRecords[i, 3]);
                            }

                            Console.WriteLine("{0,-15:0}{1,-15:0}{2,-15:0}{3,-15:0}", weatherRecords[i, 2], weatherRecords[i, 3], weatherRecords[i, 4], weatherRecords[i, 5]);

                            //find max temp by month
                            double monthTempmax = double.Parse(weatherRecords[i, 2]);
                            if (monthTempmax > max)
                            {
                                max = monthTempmax;
                            }

                            //find min temp by year
                            double monthTempmin = double.Parse(weatherRecords[i, 3]);
                            if (monthTempmin < min)
                            {
                                min = monthTempmin;
                            }

                            // find average year temp
                            if (nYear == monthArrval)
                            {
                                totalMax = totalMax + maxTemp;
                                totalMin = totalMin + minTemp;
                                average = (totalMin + totalMax) / 24;
                            }

                            Console.ReadLine();
                        }

                        // write our statistics to a file
                        StreamWriter write = new StreamWriter("Statistics.txt");
                        write.WriteLine("The statistics calculated for our data set are:");
                        write.WriteLine("Max temp in year " + nyear + " is " + maxTemp);
                        write.WriteLine("Min temp in year " + nyear + " is " + minTemp);
                        write.WriteLine("The average temperature in " + nyear + " is " + Math.Round(average, 2) + (char)176 + "c");
                        write.Close();
                    }
                }

            }
        }
    }
}

How large are these files (and how many)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.