In: Computer Science
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal integer literals.
Each of these decimal integer literals will be expressed using decimal digits, only, and none of the decimal integer literals will be prefixed by a plus or minus sign.
None of the decimal integer literals will be greater than ULONG_MAX.
The program prints the integers in order, least-to-greates and also prints the sum of the integers.
If you need any corrections kindly comment
Please give a Thumps Up if you like the answer
Program
#include<stdio.h>
#include <stdlib.h>
// Taking argument as command line
int main(int argc, char *argv[])
{
int total = 0,arr[20];
int i,j=0,n=20,temp;
//Read command line arguments into an
array
for(i = 1; i < argc; i++)
{
arr[j]=atoi(argv[i]);
total+=arr[j];
j++;
}
printf("Numbers before sorting : \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
//Sorting the numbers
for (i = 0; i < n-1;
i++)
{
for (j = 0; j < n-i-1;
j++)
{
if
(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j]= arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\nNumbers after sorting : \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\nSum= %d\n", total);
return 0;
}
Command line arguments
1 2 3 5 8 56 78 12 98 534 9 23 986 1 7 54 90 45 42 3
Output
Numbers before sorting :
1 2 3 5
8 56 78 12
98 534 9 23
986 1 7 54
90 45 42 3
Numbers after sorting :
1 1 2 3
3 5 7 8
9 12 23 42
45 54 56 78
90 98 534 986
Sum= 2057