0

I have an array initialized as such:

int[] myArray = new int[] {9, 8, 7, 3, 4, 5, 6, 2, 1};

I then have a for() loop searching the array for the highest value each time using:

int maxValue = myArray.Max();
int maxIndex = myArray.ToList().IndexOf(maxValue);

It obviously keeps finding 9 as the highest value.

I want it to first set the previously indexed value to a randomized value below the current maxValue but above -1 and continue searching the array for the next maxValue and print it to console.

(If all values reach a value == 0 then the simulation stops) <- this part I know how to do.

Is this possible? If so, how?

7
  • which is the final goal, second max? Commented Nov 11, 2016 at 2:24
  • I read it 4 times and still don't understand. How if your max index is at 0, what number is below zero but above -1? Commented Nov 11, 2016 at 2:24
  • @ Niyoko Yuliawan below the current maxValue, it's searching for a maxValue. If that maxValue is greater than 0 then it'll reduce that one. If it doesn't encounter a value greater than 0 then it stops the simulation. Commented Nov 11, 2016 at 2:25
  • @Zinov The final goal is to get everything to 0. Commented Nov 11, 2016 at 2:31
  • you mean on each iteration? putting the max value left to zero? Commented Nov 11, 2016 at 2:36

3 Answers 3

1

I guess this might be what you want. Let me know how it works for you.

using System;
using System.Linq;

public class Program
{
    private static Random random = new Random();

    public static void Main()
    {
        int[] myArray = new int[] {9, 8, 7, 3, 4, 5, 6, 2, 1};
        Simulate(myArray);

    }

    static void Simulate(int[] myArray)
    {
        int maxValue = myArray.Max();
        Console.WriteLine(string.Join(" ",myArray));
        var continueSimulation = true;
        do{

            int maxIndex = myArray.ToList().IndexOf(maxValue);
            var randomValue = random.Next(0, maxValue);
            myArray[maxIndex] = randomValue;

            maxValue = myArray.Max();
            if (maxValue == 0)
                continueSimulation = false;

            Console.WriteLine(string.Join(" ",myArray));

        }while(continueSimulation);
    }
}

You can check it out on this fiddle.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly it. Thank you so much
0

If you want to find the second max, you can mark the position of the first one and continue with your same approach. How can be done? 1- initialize an array of bool with the same length of the array where you want to find the max, then find the first max and mark that position in the second array with true, if you want the second max, make a loop through the array asking for the max and if that element is not marked in the second array of bool. Finally you will get the second max . Another idea is taking the values in a list and once you find the max, remove the max from the list to continue with the same algorithm but with an array of less values

static int Max(int [] num)
{
    int max = num[0];

    for(int i = 0; i < num.Length; i ++)
    {
        if(num[i] > max)
            max = num[i];
    }

    return max;
}

static int SecondMax(int[]a)
{
    if(a.Length < 2) throw new Exception("....");

    int count = 0; 
    int max = Max(a);
    int[]b = new int[a.Length];

    for(int i = 0; i < a.Length; i ++)
    {
        if(a[i] == max && count == 0)
        {
            b[i] = int.MinValue; 
            count ++;
        }
        else b[i] = a[i];
    }

    return Max(b);
 }

Comments

0

Honestly, the question feels a bit unusual, so if you share why you're trying to do this, maybe someone could suggest a better approach. However, to answer your original question, you can just use .NET's random number generator.

    int[] myArray = new int[] { 9, 8, 7, 3, 4, 5, 6, 2, 1 };
    Random random = new Random();

    for (int max = myArray.Max(); max > 0; max = myArray.Max())
    {
        int index = myArray.IndexOf(max);

        DoSomething(max);

        myArray[index] = random.Next(0, max);
    }

From the MSDN doco on Random, the upper bound is exclusive, which means that it will generate a random number between 0 and max-1, unless max==0, in which case it will return 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.