For this exercise, you are asked to write a method called InitializeArrayWithNoDuplicates(), using the provided method header below, which will return a one dimensional array in which every element is a randomly generated integer between 1 and 45 inclusive with no integer value occurring more than once in the array.

The length of the array is passed as a parameter to the method and you can assume that the parameter value will be less than 45 and greater than zero.

Use the Random class method int Next(int minValue, int maxValue) which returns a random integer which is greater than or equal to minValue and less than maxValue.

To enable you to test your code, you should create a Main() method that calls the InitializeArrayWithNoDuplicates() method several times with different parameters each time to show that the method is correct; similar to the Main() method of the linear search exercise in Week 7.

Include a simple DisplayArray() method similar to that in the linear search exercise to output the resultant array, called from Main().

Exactly what you do to test the method and display the array is not important for your AMS marks as only your InitializeArrayWithNoDuplicates() method will be run. This means it must be public.

If you still have difficulties, here are some tips when writing programs for AMS:

Do not write your program directly into the text box. You should copy and paste the sample code into Visual Studio, write your program there, run it and test it to make sure it works, then paste it into AMS. You only get a limited number of attempts at each AMS exercise, so make sure they count. If your code uses Console.ReadKey(); it should be replaced with Console.ReadLine();. ReadKey() will work in Visual Studio but not in AMS.

Make sure your code displays exactly what the problem asks you for. If you can, copy and paste the text you need to display into your function to avoid possible transcription errors. If there are any mistakes at all your code will not pass.

Sample code:

using System;

namespace RandomArray
{
public class RandomArrayNoDuplicates
{
static Random rng = new Random();
/// < summary>
/// Creates an array with each element a unique integer
/// between 1 and 45 inclusively.
/// < /summary>
/// < param name="size"> length of the returned array < 45
/// < /param>
/// < returns>an array of length "size" and each element is
/// a unique integer between 1 and 45 inclusive < /returns>
public static int[] InitializeArrayWithNoDuplicates(int size)
{

}
}
}

using System;

namespace RandomArray
{
public class RandomArrayNoDuplicates
{
static Random rng = new Random();
/// < summary>
/// Creates an array with each element a unique integer
/// between 1 and 45 inclusively.
/// < /summary>
/// < param name="size"> length of the returned array < 45
/// < /param>
/// < returns>an array of length "size" and each element is
/// a unique integer between 1 and 45 inclusive < /returns>
public static int[] InitializeArrayWithNoDuplicates(int size)
{

}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.