-3

Here's the method where i sort the array and find the maximum, minimum and average values.

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();
    }

Heres the method that suppose to use the values from the selectionsort method to find the index numbers of those values.

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);
    }

I'm using the ref function which allows the linearsearch method to use those variables which contain the values so it can find where their index are located.

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();

    }

The array im using is from a txt file here.

        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

from the link you can see that the avg number 48.04 is the same number located in the array. What i need is the index number of where that number is in the array.

nat1
  • 23
  • 4
  • You need to use a tolerance to compare non-integral values https://stackoverflow.com/questions/3874627/floating-point-comparison-functions-for-c-sharp – aybe May 23 '18 at 23:00
  • 2
    "The avg number has been rounded to equal a number in the array" Plainly not. Show us a [mcve] if you want to get any further with this. – spender May 23 '18 at 23:10
  • How can you find an index to an average? after all its an average, and not exactly part of the contents. i.e it may not even exist – TheGeneral May 23 '18 at 23:29
  • Simply in array [1, 2, 3, 4], avg is 2.5 which is not in array and you may get index3 = 0! – Hossein Golshani May 23 '18 at 23:38
  • This method is very wrong. Obviously, neither avg, nor max/min may not exist (since all of them are parameters of the method). Also, index1/2/3 should not be parameters. –  May 23 '18 at 23:43
  • @spender I've provided my code for you. – nat1 May 24 '18 at 06:00
  • @HusseinGolshani In the link ive provided the avg number is the same as a number in the array 48.04. – nat1 May 24 '18 at 06:03
  • @Tom Your right about the index parameters thanks. – nat1 May 24 '18 at 06:05
  • @TheGeneral The link i've provided in the op shows that the result i have is the same is the number in the array 48.04 once rounded. – nat1 May 24 '18 at 06:07

2 Answers2

0

There are a few things not right about this question. This might help you think about the problem a little better. Add pepper and salt to taste

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));
}

}

Output

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

Full Demo Here

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

I've found the solution. All i had to do is Math.Round the avg value in the index method.

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

The link shows the output of the program with the index number of the avg value.

nat1
  • 23
  • 4