Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reverse an array containing random numbers. How?

My assignment is to create an array with random numbers and then reverse the order in C#.

I managed to generate the random array, but how do i reverse it?

For example the output should look like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

15334
43351

int[] TalArray = new int[5];
var arr = new int[TalArray.Length];
for (int i = 0; i < TalArray.Length; i++)

{
    Random random = new Random();
    int num = random.Next(TalArray.Length);

    Console.WriteLine(num);
} 

Thanks in advance! 🙂

>Solution :

Here is a solution with Array.Reverse:

var arr = new int[5];
for (int i = 0; i < arr.Length; i++)
  {
     arr[i] = new Random().Next(0, arr.Length);
  }
var revArr = arr.Reverse();
Console.WriteLine(String.Join("", arr ) + string.Join("", revArr));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading