【问题标题】:Why does my linear search method not return the index number of the avg value为什么我的线性搜索方法不返回平均值的索引号
【发布时间】:2018-05-23 22:58:18
【问题描述】:

这是我对数组进行排序并找到最大值、最小值和平均值的方法。

public static void selectionSort(double[] _arr, ref double max, ref double min, ref double sum, ref double avg)
    {
        int min1;
        double temp;

        Console.WriteLine("\n--Original Array--");
        printArray(_arr);

        Console.WriteLine("--Selection Sort Process--");
        for (int i = 0; i < _arr.Length - 1; i++)//Outter loops goes through all of the objects in the array.
        {
            min1 = i;//Minimum value is set to the current index that the outer loop is at.
            for (int j = i + 1; j < _arr.Length; j++)//Inner loop goes thorough and does the swaps.
            {
                if (_arr[j] < _arr[min1])//Condition checking of the current state of the array
                {
                    min1 = j;//If the current value is less than arr[min] then make j the new min.
                }
            }
            if (min1 != 1)
            {
                temp = _arr[i];
                _arr[i] = _arr[min1];
                _arr[min1] = temp;
            }
        }
        printArray(_arr);//Display final sorted array
        for (int i = 0; i < _arr.Length; i++)
        {
            sum += _arr[i];//adds all the values in the array together and into the sum variable
            if (max < _arr[i])//if the i value is greater than the max value
            {
                max = _arr[i];
            }
            if (min > _arr[i])//if the Min value is greater than the i value
            {
                min = _arr[i];//the Min value will become the i value
            }
        }
        avg = sum / _arr.Length;//the variable avg = sum divide by the total number of the array
        Console.Write("Maximum value: {0}, Minimum value: {1}, Average value: {2}", max, min, Math.Round(avg, 2));
        Console.WriteLine();
    }

这是假设使用 selectionsort 方法中的值来查找这些值的索引号的方法。

public static void linearSearch(double[] _arr, double max, double min, double avg)
    {
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;

        for (int i = 0; i < _arr.Length; i++)
        {
            if (_arr[i] == max)
            {
                index1 = i;
            }
            if (_arr[i] == min)
            {
                index2 = i;
            }
            if (_arr[i] == avg)
            {
                index3 = i;
            }
        }
        Console.WriteLine("Max index number: {0}, Min index number: {1}, Avg index number: {2}", index1, index2, index3);
    }

我正在使用 ref 函数,它允许线性搜索方法使用那些包含值的变量,以便它可以找到它们的索引所在的位置。

static void Main(string[] args)
    {
        int size = 100;
        double[] arr1 = new double[size];
        double[] arr2 = new double[size];
        double[] arr3 = new double[size];

        arr1 = importData();
        arr2 = importData();
        arr3 = importData();

        findMaximum(arr1);

        double max = 0d;
        double min = arr2[0];
        double sum = 0d;
        double avg = 0d;

        selectionSort(arr2, ref max, ref min, ref sum, ref avg);

        linearSearch(arr3, max, min, avg);

        Console.ReadLine();

    }

我使用的数组来自此处的 txt 文件。

        public static double[] importData()
    {
        string[] txt = File.ReadLines(@"c: \Users\9993959\Moisture_Data.txt").ToArray();
        double[] arr = txt.Select(Convert.ToDouble).ToArray();
        return arr;
    }

OUTPUT

从链接中您可以看到平均数字 48.04 与数组中的数字相同。我需要的是该数字在数组中的索引号。

【问题讨论】:

  • 您需要使用容差来比较非整数值stackoverflow.com/questions/3874627/…
  • “平均数已四舍五入以等于数组中的数字”显然不是。如果您想进一步了解这个,请告诉我们minimal reproducible example。
  • 如何找到平均值的索引?毕竟它是一个平均值,而不是内容的一部分。即它甚至可能不存在
  • 简单地在数组[1, 2, 3, 4]中,avg是2.5,不在数组中,你可能会得到index3 = 0!
  • 这种方法非常错误。显然,avg 和 max/min 都可能不存在(因为它们都是方法的参数)。另外,index1/2/3 不应该是参数。

标签: c# arrays indexing double


【解决方案1】:

这个问题有些地方不对。这可能会帮助您更好地考虑问题。加入胡椒和盐调味

public static void LinearSearch(double[] arr, out int maxIndex, out int minIndex)
{
    maxIndex = 0;
    minIndex = 0;
    var max = double.MinValue;
    var min = double.MaxValue;
    double sum = 0;
    double avg = 0;
    for (int i = 0; i < arr.Length; i++)
    {
        sum += arr[i];
        if (arr[i] >= max)
        {
            max = arr[i];
            maxIndex = i;
        }

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

    avg = sum / (double)arr.Length;
    Console.WriteLine(string.Format("Max {0}, Min {1}, Avg {2}", max, min, avg));
}

public static void Main()
{
    var ary = new double[]{34, 23, 345, 546, 4562321, 3, 45, 42, 4, 4, 6, 5, -34534, 345, 546, 456};
    int maxIndex;
    int minIndex;
    LinearSearch(ary, out  maxIndex, out minIndex);
    Console.WriteLine(string.Format("Max index : {0}, Min index : {1}, Avg index : who knows", maxIndex, minIndex));
}

}

输出

Max 4562321, Min -34534, Avg 283136.9375
Max index : 4, Min index : 12, Avg index : who knows

Full Demo Here

【讨论】:

  • 我已经添加了我的代码,希望你能看到我的问题所在。
【解决方案2】:

我找到了解决方案。我所要做的就是 Math.Round 在 index 方法中的 avg 值。

public static void linearSearch(double[] _arr, double max, double min, double avg)
    {
        int mxindx = 0;
        int mnindx = 0;
        int avgindx = 0;

        for (int i = 0; i < _arr.Length; i++)
        {
            if (_arr[i] == max)
            {
                mxindx = i;
            }
            if (_arr[i] == min)
            {
                mnindx = i;
            }
            if (_arr[i] == Math.Round(avg, 2))
            {
                avgindx = i;
            }
        }
        Console.WriteLine("Maximum index number: {0}, Minimum index number: {1}, Average index number: {2}", mxindx, mnindx, avgindx);
        Console.WriteLine();
    }
}

Output

链接显示程序的输出以及平均值的索引号。

【讨论】:

    猜你喜欢
    • 2019-03-02
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多