Question

Write a C program, named sortit, that reads three integers from the command line argument and...

Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance,

>sortit 3 5 7

>The sorted list is 3, 5, 7.

>sortit 4 0 9

>The sorted list is 0, 4, 9

>sortit 1 p 9

>sortit: invalid input p.

>sortit

>usage: sortit num1 num2 num3!


1. The source code of a c file

2. Screenshots that show successful execution of your testing cases including 1. invalid input, 2. wrong usage (not enough arguments), 3. correct input and output as expected.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

int isNumber(char *s) {
    int i = 0;
    while(s[i] != '') {
        if (!isdigit(s[i])) {
            return 0;
        }
        i++;
    }
    return 1;   
}

int main(int argc, char *argv[]){

    if(argc != 4) {
        printf("usage: sortit num1 num2 num3!");
        exit(0);
    }

    if(!isNumber(argv[1])) {
        printf("sortit: invalid input %s
", argv[1]);
        return 1;
    }   
    if(!isNumber(argv[2])) {
        printf("sortit: invalid input %s
", argv[2]);
        return 1;
    }   
    if(!isNumber(argv[3])) {
        printf("sortit: invalid input %s
", argv[3]);
        return 1;
    }

    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    int c = atoi(argv[3]);


    int lo = min(min(a, b), c);
    int hi = max(max(a, b), c);
    int mid = (a + b + c) - (lo + hi);

    printf("The sorted list is %d, %d, %d.
", lo, mid, hi);

    return 0;
}

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a C program, named sortit, that reads three integers from the command line argument and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that reads three positive integers (> 0) from the command line (one...

    C++ Write a program that reads three positive integers (> 0) from the command line (one at a time), then computes and prints the smallest entered number. Use if-else statements for three integer comparison. Example: Enter an integer: 5 Enter an integer: 23 Enter an integer: 7 The smallest number is: 5 Example 2: Enter an integer: 3 Enter an integer: 3 Enter an integer: 6 The smallest number is: 3 "The input must be positive number. The program should...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • Problem Description: Write s Java program to find and display the product of three integer values...

    Problem Description: Write s Java program to find and display the product of three integer values based on the rue mentioned below it should display the product of the three values except when one of thevalues is 7n that case 7 should not be ingluded in the product and the values to its left also shouid not be included If there is only one value to be considered display that value tself f no values can be included in the...

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • 1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether...

    1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether there is a command line argument of “-amount” followed by an amount number. It will print out that number amount. For all other cases, it will print out -1. For example, if you run this program with correct arguments as follows, it will print out 1.99, 0.75 and 1.1 respectively CheckAmount -amount 1.99 CheckAmount -help -amount 0.75 CheckAmount -help -check -amount 1.1 -verbose And...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT