简体   繁体   中英

Finding the maximum value on a random array

Well im trying to find a maxValue in a array and its harder then i feel it should. Normally this code here works. If i declare an array and manually put in numbers for the array it find the max value fine. But when i put in a method to make an array with random numbers it breaks and returns the last value set as the maximum one.

static int MaxArray(int[] Array)
{
    int maxVal = Array[0];
    for(int i = 0; i < Array.Length; i++)
    {
        if(Array[i] > maxVal)
        {
            maxVal = Array[i];
        }
    }           
    return maxVal;
}
static void Main(string[] args)
{
    Random r = new Random();
    int[] myArray = new int[5];
    for(int i = 0; i < myArray.Length; i++)
    {
        int rNumb = r.Next(0, 100);
        for (int v = 0; v < myArray.Length; v++)
        {
            myArray[v] = rNumb;
        }
        Console.WriteLine(myArray[i]);
    }
    Console.WriteLine("Press entere to find the max value");
    Console.ReadKey();            
    Console.Write(MaxArray(myArray));
    Console.Read();
}

The inner for-loop in your Main method is useless. It fills the whole array with the current random number (so at the end the whole array will contain the last random number repeated).
The correct code is the following:

for(int i = 0; i < myArray.Length; i++)
{
    int rNumb = r.Next(0, 100);
    myArray[i] = rNumb;
    Console.WriteLine(myArray[i]);
}

This is easily achieved using Linq.

using System.Linq;

private static Random _random = new Random();
public static int[] GenerateRandomArray(int arrayLength)
{
    return Enumerable.Range(0, arrayLength).Select(i => _random.Next(0, 100)).ToArray();
}

public static int FindMaxValue(int[] array)
{
    return array.Max();
}

You were overwriting the values with your second for loop,

    for (int v = 0; v < myArray.Length; v++)
    {
        myArray[v] = rNumb;
    }

will write the current random number at each index of your array. The last random number will overwrite previous ones, so it will be declared as max since it's the only available number in your array.

Try this instead :

    static void Main(string[] args)
    {
        Random r = new Random();
        int[] myArray = new int[5];
        for (int i = 0; i < myArray.Length; i++)
        {
            myArray[i] = r.Next(0, 100);

            Console.WriteLine(myArray[i]);
        }
        Console.WriteLine("Press entere to find the max value");
        Console.Write(MaxArray(myArray));
        Console.Read();
    }

But honestly that MaxArray method is useless, don't reinvent the wheel, use Max from LINQ instead :

Console.Write(myArray.Max());

It's because your array initialization is completely useless. This is what you're looking for:

for(int i = 0; i < myArray.Length; i++)
{
    myArray[i] = r.Next(0, 100);
    Console.WriteLine(myArray[i]);
}

At the end of it, the array will look like this (with your code):

{n, n, n, n, n}

where n is the last random number.

You don't need this loop. You should change this

for (int v = 0; v < myArray.Length; v++)
    {
        myArray[v] = rNumb;
    }

with

myArray[i] = rNumb;

By writing this loop you are overwriting all the values in the array with the last value.

The reason you get the last value set as the maximum every time is because you set every element in the array to the last random number generated on the last iteration of the outer for loop.

Your Console.WriteLine(myArray[i]); is giving you a false impression of what the values of the array you pass into MaxValue() actually are!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM