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

C# C# C# C# Randomly generate an array of 100 integer numbers ranging from 100 t

ID: 3883001 • Letter: C

Question

C# C# C# C#

Randomly generate an array of 100 integer numbers ranging from 100 to 999.

(A) Identify the indexes of 3 largest numbers in the array using 3 variables. ( Use only comparisons. Cannot use sorting method for this request.)

(B) Identify the original indexes of ascending-sorted numbers in the array. You may use a sorting algorithm and another array for 100 indexes.

Display the original numbers using 10 lines with 10 numbers in each line. Then (A) answer and then (B) answer.

==============Illustration===============

7 original random numbers: {23 14 36 72 15 18 12}

The original indexes: {0 1 2 3 4 5 6}

7 sorted numbers: {12 14 15 18 23 36 72}

The original indexes of sorted numbers: {6 1 4 5 0 2 3}

==============Output Example===============

Original Random numbers:

xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx

xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx

(A) The indexes of 3 largest numbers: xx, xx, xx

(B) The indexes of sorted numbers:

xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx
xx xx xx xx xx xx xx xx xx xx

Thank you :)

Explanation / Answer

using System;  
class Program  
{  
    static void Main()  
    {  
        int i,j,k=1;  
        int[] a = new int[101]; // Array Declaration  
        int[] idx = new int[101];
        int[] tmp = new int[101];
     int n;
        for (i = 1; i <= 100; )  
        {  
            n = Randfunc();
            if( n>=100 && n<=999 )
              {
                  a[i] = n;
                  tmp[i]=n;
                 i++;
              }
        }  
        
        Console.Write("Orginal Random Numbers : ");  
        for (i = 1; i <= 10; i++)  
        {
           for(j=1;j<=10;j++)
            Console.Write(a[k++]+" ");
           Console.Write(" ");
}

        //Bubble sort
        for (i = 1; i <= 100; i++)  
        {  
            for ( j = 1; j <= 100 - 1; j++)  
            {  
                if (a[j] < a[j + 1])  
                {  
                    int temp = a[j];  
                    a[j] = a[j + 1];  
                    a[j + 1] = temp;  
                }  
            }  
        }  

        for (i=1;i<=100;i++)
         {
            for (j=1;j<=100;j++)
             {
                if ( a[i] == tmp[j] )
                  {
                     idx[i] = j;
               if (i>=3 && a[i]==a[i-1])
                       continue;
                     else
                       break;  
                   }         
             }
         }
        Console.Write("The indexes of 3 largest numbers : " + idx[1] + " " + idx[2] + " " + idx[3] + " ");
Console.Write(" The indexes of sorted numbers: ");
        string s;
        for (k=1,i = 1; i <= 10; i++)  
        {
            for(j=1;j<=10; j++)
             {
              s=string.Format("{0,3}",idx[k++]);
              Console.Write(s+" ");
             }
   Console.Write(" ");
}
}
    static Random r = new Random();
    static int Randfunc()
    {
        int n = r.Next();
        return n;
    }
}