Question

Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....

Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit status should be non-zero in all other cases. Use pseudo-code if necessary, but try to use valid C or bash syntax wherever possible. You may write the program and test it on UNIX. Write or copy/paste your code here.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
C Program
#include<stdio.h>

int main(int numargs, char **argv)
{
    // check if the number of command line arguments is 3 or not
    if(numargs == 3)
    {
        // convert the arguments to integers
        int x = atoi(argv[1]);
        int y = atoi(argv[2]);
        // compare the numbers
        if( x > y )
           printf("%d is greater than %d\n", x, y );
        else if(y > x)
           printf("%d is less than %d\n", x, y );
           // else they are equal
        else
        {
           printf("%d is equal to %d\n", x, y );
           return 0;
        }
    }
    // outer else
    else
        printf("Invalid number of arguments supplied!");

    return 1;
}

OUTPUT

C\Program Workspace\CPP Workspace\compare.exe 70 is equal to 70 Process exited after 0.6899 seconds with return value 0 Press

CAProgram Workspace\CPP Workspace\compare.exe 34 is greater than 5 Process exited after 0.02706 seconds with return value 1 P

Add a comment
Know the answer?
Add Answer to:
Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....
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
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