sample code: For this exercise, you are asked to write a method called Initializ
ID: 3714149 • Letter: S
Question
sample code:
For this exercise, you are asked to write a method called InitializeArraywithNoDuplicates (), 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 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 ?????? w ch s great 1 an orequa imnalean arra ess 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 4. lude 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 or your MS m arks as only your InitialzeArrayN?thNoDuplicates This means it must be public. If you still have difficulties, here are some tips when writing programs for AMS: method will be run. 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.Explanation / Answer
Below is the solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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)
{
int minValue = 1; //declare min value
int maxValue = size; //declare max value
int[] random1 = new int[size]; //declare random1 array with 45 element size
Random randNum = new Random(); //random class
for (int i = 0; i < random1.Length; i++)
{
random1[i] = randNum.Next(minValue, maxValue); //generate random array of each unique
}
return random1; //return random1 array
}
static void Main(string[] args)
{
int[] randomArray = new int[45]; //create random array
randomArray = InitializeArrayWithNoDuplicates(45);
for(int i=0; i< randomArray.Length; i++)
{
Console.WriteLine((i+1)+ "="+randomArray[i]); //print the all generated random array
}
Console.ReadKey();
}
}
}
sample output:
1=2
2=15
3=16
4=5
5=33
6=19
7=39
8=21
9=7
10=19
11=35
12=10
13=32
14=36
15=4
16=25
17=10
18=23
19=36
20=21
21=32
22=13
23=12
24=40
25=28
26=13
27=41
28=39
29=2
30=13
31=11
32=43
33=37
34=4
35=35
36=17
37=39
38=23
39=25
40=43
41=20
42=25
43=9
44=40
45=9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.