Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# SAMPLE CODE: or this exercise, you are asked to write a method called Initial

ID: 3717680 • Letter: C

Question

C#

SAMPLE CODE:

or this exercise, you are asked to write a method called InitializeArrayWithNoDuplicatesO. using the provided method header below, which will return a one dimensional y 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 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 axValue To enable you to test your code, you should create a Main() method that calls the InitializeArraywithNoDuplicates() method several times with different parameters each e to show that the method is correct similar to the Main() method of the linear search exercise in Week 4. clude a simple DisplayArray() method similar to that in the linear search exercise to output the resultant array, called from Main) ly 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. s means it must be public. f you still have difficulties, here are some tips when writing programs

Explanation / Answer

Please find my 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)
        {
        // boolean array of size 45(initialized all elements to false) to track whether a randomly generated number is repeated or not
        bool[] repeated = new bool[45];
        int rand_num;
        // defining array of given size
        int[] array = new int[size];
        // while all elements are not generated
        for(int i=0; i<size; i++){
          // while unique random number is not generated, generate new random number again
          while(true){
           rand_num = rng.Next(1, 46);
           if(!repeated[rand_num-1]){
            repeated[rand_num-1] = true;
            array[i] = rand_num;
            break;
           }
          }
        }
        return array;
        }
       
        static void Main(string[] args)
        {
        // random array of size 35
   int[] array = InitializeArrayWithNoDuplicates(25);
   Console.WriteLine("Array of size {0}:",25);
   // printing array
   for(int i=0; i<array.Length; i++){
    Console.Write("{0} ", array[i]);
   }
    }
    }
}