开发者

Equations with Array Bidimenensional in C#

开发者 https://www.devze.com 2023-04-13 05:42 出处:网络
I have an array with 310 lines and 120 columns. I get the data that will populate this array from serial port at intervals of 10 seconds.

I have an array with 310 lines and 120 columns.

I get the data that will populate this array from serial port at intervals of 10 seconds.

How to send this data to the first row of the array and on the next iteration to the line down and it continuously until the operation is complete?

Once completed, take the maximum, minimum and average of the array. And finally, the average, maximum and minimum of a selection of cells in the Array.

This is possible with array in C#?

([7,31]-xxxxx.MIN([$28$5:$45$95])/(xxxxx.MAX[$28$5:$46$95]-xxxxx.MIN[$2开发者_如何学JAVA8$5:$45$95])


The comments you made below your question clarified it a bit, but the screwy nature of comments jumbled this line:

...receive a string like this  0#,22008,21930,00000, n / a, n / a ! But only use the 0#,22008,21930,00000. The...

So I'll just assume for now that those weird characters are an endline. Adapt it otherwise.

A set arraylength of 120 is completely unnecessary, and a 2-dimensional array adds no new methods usable here. We'll just use a jagged array instead. The length of 310 probably has some reason, so we'll keep that.

So on each row, the first 24 "cells" are the input from the stream. I'm unfamiliar with ports, but I can safely assume you can route that RS485/USB to a Stream object. Cells 0, 4, 8, 12, 16 and 20 are filled with IDs, one of {01, 02, 03, 04, 05, 06} to indicate what LPC it came from.

But a bit later in the comments you specify wanting this:

([32,6]-MIN([28,5]:[41,96]))/(MAX([28,5]:[41,96])-MIN([28,5]:[41,96]))

Even in Excel, this would take the mini/max/average over those IDs as well. We're avoiding that in this code though, it ignores those columns.

class LaserArray
{
    private int minimum;
    private int maximum;
    private double average;
    private bool finishedReading;
    private StreamReader sr;

    public int Minimum
    { get { return finishedReading ? minimum : -1; } }
    public int Maximum
    { get { return finishedReading ? maximum : -1; } }
    public double Average
    { get { return finishedReading ? average : -1; } }
    public bool FinishedReading
    { get { return finishedReading; } }
    public bool StreamInitialized
    { get { return sr != null; } }

    private int[][] arr;

    public LaserArray()
    {
        arr = new int[310][];
        finishedReading = false;
    }

    public bool InitStream(Stream s)
    {
        try
        {
            sr = new StreamReader(s);
            /*alternatively, as I have no clue about your Stream:
             * sr = new StreamReader(s, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize)
             * */
        }
        catch(Exception)
        {
            Console.WriteLine("Invalid Stream object.");
            sr = null;
            return false;
        }
        return true;
    }

    public void ReadInData()
    {
        if (sr == null)
        {
            Console.WriteLine("Initialize a Stream with UseStream first.");
            return;
        }
        if (finishedReading)
        {
            Console.WriteLine("The stream is already read.");
            return;
        }

        minimum = int.MaxValue; maximum = 0;
        int currentTotal = 0;

        for (int rowCounter = 0; rowCounter < 310; rowCounter++)
        {
            arr[rowCounter] = new int[24];
            int indexCounter = 0;
            for (int i = 0; i < 6; i++)
            {                                   // 0#,22008,21930,00000, n / a, n / a !
                char[] buffer = new char[28];   //123456789012345678901234  5  67  8 makes 28 characters?
                try
                {
                    sr.ReadBlock(buffer, 0, 2 + 5 + 1); 
                }
                catch (IOException e)
                {
                    //some error occurred
                    Console.WriteLine("IOException: " + e.Message);
                }

                string input = new String(buffer);
                arr[rowCounter][indexCounter] = int.Parse(input.Substring(2, 2));
                indexCounter++;

                int currentNumber;

                currentNumber = int.Parse(input.Substring(6, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(12, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(18, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;
            }
        }

        average = currentTotal / (double) 310;
        //succesfully read in 310 lines of data
        finishedReading = true;
    }

    public int GetMax(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int max = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] > max)
                    max = arr[i][j];
            }

        return max;
    }
    public int GetMin(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int min = 99999;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] < min)
                    min = arr[i][j];
            }

        return min;
    }
    public double GetAverage(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int total = 0;
        int counter = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                counter++;
                total += arr[i][j];
            }

        return total / (double) 310;
    }

}

This should be self-explanatory. Create a LaserArray, init the stream, readIn the data, and then have fun with the results.

Also, I'm bored and on vacation, so that's why I'm answering a year-old question in such great detail.

Also reputation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号