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

C# LAB: This problem is about using two static methods of class Array: Sort and

ID: 3687147 • Letter: C

Question

C# LAB:

This problem is about using two static methods of class Array: Sort and Reverse.  The Sort method sorts elements in ascending order, while the Reverse method reverses the order of the elements.  Write a program that generates 10 random integers between 0 and 99 (include 0 and 99).  Store the 10 integers in an array.  Display the elements.  Sort the elements in descending order.  Display the elements.  The following is an example.

Initial array values:

23 80 14 13 18 41 63 81 85 91

Array values sorted:

91 85 81 80 63 41 23 18 14 13

Press any key to continue . . .

Explanation / Answer

main.cs

using System;
namespace ArrayApplication
{
   class ArrayWorld
   {
       static void Main(string[] args)
        {
            int Min = 0;
            int Max = 99;
            int[] A = new int[10];
            Random randNum = new Random();
            for (int i = 0; i < A.Length; i++)
            {
              A[i] = randNum.Next(Min, Max);
            }
              Console.WriteLine("Initial array values:");
            foreach (int i in A)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadLine();
            Array.Sort(A);
            Array.Reverse(A);
              Console.WriteLine("Array values sorted::");
            foreach (int i in A)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadLine();
        }
    }
  
}

Output :