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

In C programming: -- Implement Selection into an array and implement Insertion i

ID: 3689926 • Letter: I

Question

In C programming:

-- Implement Selection into an array and implement Insertion into an array.

-- A template for array, provided.

The following functions are provided:

·       fillRand - Fills the array with the specified number of non- repeating, random values.

·       print - prints the array

·       main - Includes basic code to call the appropriate functions.

Array Template

#include

void fillRand(int arr[], int size, int ceil)

{

   int i, tmp;

   int used[size];

   for(i = 0; i < size; i++)

       used[i] = 0;

   for(i = 0; i < size; i++)

   {

       tmp = rand() % ceil;

       while(used[tmp])

           tmp = rand() % ceil;

       used[tmp] = 1;

       arr[i] = tmp;

   }

   return;

}

void selSort(int arr[], int size)

{

   //Your code here!

}

void insSort(int arr[], int size)

{

   //Your code here!

}

void print(int arr[], int size)

{

   int i;

   for(i = 0; i < size; i++)

   {

       printf("%d ", arr[i]);

   }

   printf(" ");

   return;

}

int main()

{

   srand(time(0));

   int arr[10];

   fillRand(arr, 10, 10);

   printf("Unsorted list... ");

   print(arr, 10);

   //--------------------

   //----Choose----------

  

   //selSort(arr, 10);

   //insSort(arr, 10);

   //--------------------

   printf("Sorted list... ");

   print(arr, 10);

   return 0;

}

Explanation / Answer

#include

void fillRand(int arr[], int size, int ceil)

{

   int i, tmp;

   int used[size];

   for(i = 0; i < size; i++)

       used[i] = 0;

   for(i = 0; i < size; i++)

   {

       tmp = rand() % ceil;

       while(used[tmp])

           tmp = rand() % ceil;

       used[tmp] = 1;

       arr[i] = tmp;

   }

   return;

}

void selSort(int arr[], int size)

{

   //Your code here!

           int position, d, c, swap;
           for ( c = 0 ; c < ( size - 1 ) ; c++ )
           {
           position = c;
             
           for ( d = c + 1 ; d < size ; d++ )
           {
           if ( arr[position] > arr[d] )
           position = d;
           }
           if ( position != c )
           {
           swap = arr[c];
           arr[c] = arr[position];
           arr[position] = swap;
           }
           }

}

void insSort(int arr[], int size)

{

   //Your code here!

       int c, d, t;
       for (c = 1 ; c <= size - 1; c++) {
       d = c;
         
       while ( d > 0 && arr[d] < arr[d-1]) {
       t = arr[d];
       arr[d] = arr[d-1];
       arr[d-1] = t;
         
       d--;
       }
       }

}

void print(int arr[], int size)

{

   int i;

   for(i = 0; i < size; i++)

   {

       printf("%d ", arr[i]);

   }

   printf(" ");

   return;

}

int main()

{

   srand(time(0));

   int arr[10];

   fillRand(arr, 10, 10);

   printf("Unsorted list... ");

   print(arr, 10);

   //--------------------

   //----Choose----------

  

selSort(arr, 10);

insSort(arr, 10);

   //--------------------

   printf("Sorted list... ");

   print(arr, 10);

   return 0;

}