Code:
#include <stdio.h>
#include<time.h> // for time();
#include<stdlib.h> //for rand();
void selectionSort(int arr[], int *p_arr[], int *);
void insertionSort(int arr[], int *q_array[], int *x);
void printArray(int arr[], int *p_arr[], int *q_array[], int *x);
void blah(int arr[], int *p_arr[], int *q_array[], int *x);
void generate(int arr[], int *x);
int main()
{
int arr[25];
int *p_arr[25];
int *q_array[25];
int x;
srand(time(NULL)); //random number generator
printf("Please enter the number of elements in array: \n");
scanf("%d", &x);
generate(arr, &x);
selectionSort(arr, p_arr, &x);
insertionSort(arr, q_array, &x);
blah(arr, p_arr, q_array, &x);
printArray(arr, p_arr, q_array, &x);
return 0;
}
//Function to randomly generate an array of integer numbers
void generate(int arr[], int *x)
{
int c;
for(c = 0; c < *x; c++)
{
arr[c] = rand()%100;
}
}
void blah(int arr[], int *p_arr[], int *q_array[], int *x)
{
int i;
for(i = 0; i < *x; i++)
{
p_arr[i] = &arr[i];
}
for(i = 0; i < *x; i++)
{
q_array[i] = &arr[i];
}
}
//Function to sort one of the arrays of pointers to point to the integer numbers in ascending order
void selectionSort(int arr[], int *p_arr[], int *x)
{
int c, d, position, swap;
//selection sort for double array[]
for (c = 0; c < *x; c++)
{
//*p_arr[c] = (rand()%100); // storing a random number between 0 and 100
printf("%d\n", *p_arr[c]);
}
for (c = 0; c < (*x - 1); c++)
{
position = c;
for (d = c + 1; d < *x; d++)
{
if (*p_arr[position] > *p_arr[d])
position = d;
}
if (position != c)
{
swap = *p_arr[c];
*p_arr[c] = *p_arr[position];
*p_arr[position] = swap;
}
}
}
//Function to sort one of the arrays of pointers to point to the integer numbers in descending order
void insertionSort(int arr[], int *q_array[], int *x)
{
int e,f,temp;
//prints all the elements that have been generated into array 2
printf("\n");
printf("Elements in array 2: \n");
printf("\n");
for(f = 0; f < *x; f++)
{
for(e = f + 1; e < *x ; e++)
{
if(*q_array[f] < *q_array[e])
{
temp = *q_array[f];
*q_array[f] = *q_array[e];
*q_array[e] = temp;
}
else
break;
}
}
}
//Function to print the array
void printArray(int arr[], int *p_arr[], int *q_array[], int *x)
{
int c,e;
printf("Ascending Order: \n");
for(c = 0; c < *x; c++)
{
selectionSort(arr, p_arr, x);
printf("%d\n", *p_arr[c]);
}
printf("Original Array: \n");
for(c = 0; c < *x; c++)
{
generate(arr, x);
printf("%d\n", arr[c]);
}
printf("Descending Order: \n");
for(c = 0; c < *x; c++)
{
insertionSort(arr, q_array, x);
printf("%d\n", *q_array[e]);
}
}