Hi,
I have to write a program which accepts a set of numbers and sort them in descending order and if "-r" is included in the command line then it should sort the numbers in ascending order.The program i wrote isn't working plz tell me what is wrong with it and how to correct it
Code:
# include <stdio.h>
main (argc, argv)
int argc;
char *argv[];
{
int ar[100];
int i, j, n, flag, temp;
printf ("usage: a.out for descending order\n");
printf ("usage: a.out -r for ascending order\n");
printf ("enter the number of elements in array\n");
scanf ("%d", &n);
printf ("enter the elements of array\n");
for (i = 0; i < n; i++)
scanf ("%d", &ar[i]);
if (*argv == "-r")
{
for (i = 0; i < n; i++)
{
for (j = 0; j < (n-i-1); j++)
{
if (ar[j] > ar[j+1])
{
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
}
}
}
printf ("the sorted array is \n");
for (i = 0; i < n; i++)
printf ("%d\n", ar[i]);
}
else
{
for (i = 0; i < n; i++)
{
for (j = 0; j < (n-i-1); j++)
{
if (ar[j] < ar[j+1])
{
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
}
}
}
printf ("the sorted array is \n");
for (i = 0; i < n; i++)
printf ("%d\n", ar[i]);
}
}