15,396,239 members
See more:
i need to write a program to sort command line arguments (Numbers).

What I have tried:

C++
```#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
int i, j, t, n, pos, a[100];
n = argc - 1;
printf("\nNumber of elements in the input array is : %d\n", n);
printf("\nThe input array is\n\t");
for (i = 1; i <= n; i++)
{
a[i - 1] = atoi(argv[i]);
printf("%d ", a[i - 1]);
}
for (i = 0; i<n; i++)
{
pos = i;
for (j = i + 1; j<n; j++)
{
if (a[pos] > a[j])
{
pos = j;
}
}
if (pos != i)
{
t = a[i];
a[i] = a[pos];
a[pos] = t;
}
}
printf("\n\n The sorted array is \n\t");
for (i = 0; i<n; i++)
{
printf("%d ", a[i]);
}
return 0;
}```
Posted
Updated 31-Mar-17 6:38am
v2
Patrice T 30-Mar-17 19:58pm

And you have a question, a problem, an error ?
Mehedi Shams 30-Mar-17 20:21pm

Hi Member 13095321,

Your code is pretty perfect. It sorts the given range of numbers in the commandline perfectly. So, what is the problem, or where are you finding difficulty?
PIEBALDconsult 30-Mar-17 20:37pm

I prefer to sort the values from the command line as I copy them into the array rather than sorting the array once it has been filled.
So, [ 3 , 1 , 2 ] becomes
[ 3 ]
[ 1 , 3 ]
[ 1 , 2 , 3 ]

## Solution 1

There are different sorting algorhitms, so this tutorial at cprogramming gives you a fine lesson about sorting.