I am trying to compose the largest possible number out of a set of integers. The code only works if the input is single digits otherwise it gives me wrong answer. for example, if the input is 2, 21 it gives me 212 instead of 221. I have tried to edit the code but I got stuck. I had an idea to convert any number the user enters to digits i.e 210 -> 2, 1, 0. but I don't know how to implement it.
int IsGreaterOrEqual(int n1, int n2)
{
}
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element
// with the first element
swap(&arr[min_idx], &arr[i]);
}
}
int main()
{
int n; //number of elements
scanf("%d", &n);
int ints[n], sol[n];
int size = sizeof(ints)/sizeof(ints[0]);
for(int i = 0; i < n; i++){ //taking the inputs from the user
scanf("%d", &ints[i]);
}
ints[n] = selectionSort(ints, size); //sort the array in ascending order
for(int i = n-1; i >= 0; i--){ //here is where I am stuck
}
}
I am was going to use IsGreaterOrEqual function to append the 2 numbers forward and backward and return the greatest number. for example, append 2 and 21 -> 221 and 212. How can I edit the code to work for any set of positive integers?