Question

In: Computer Science

write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments....
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that can be added. Use the file name sum.c for your program. Test your program with sum 5 -10 3 and sum 8 and sum 2b) Write a C program that reads in a file and outputs it in reverse order. Use recursion. Test your program with the file reverse.txt...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments w, x, y, and z. Define a boolean variable whose value is true if the four values are either in strictly ascending order (w < x < y < z) or strictly descending order (w > x > y > z), and false otherwise. Then, display the boolean variable value. NOTE 1: Do not use if statements on this program. NOTE 2: Assume that...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Write the line x = 0:2:20; in the Command Window of MATLAB and then create a...
Write the line x = 0:2:20; in the Command Window of MATLAB and then create a Simulink model that first loads x from the Workspace, then creates a vector y such that y = 2.5x + ex , and finally sends the vector y back to the Workspace. You will need a From Workspace block, a To Workspace block, two Constant blocks, a Product block, and a Sum block. Note that there is a sample time associated with the From...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT