Homework Help Question & Answers

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
Next > < Previous
ReportAnswer #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 Homework Help Answer
Add Answer of:
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: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions