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
  • Unix classes Write a bash script that take in three integer numbers from the command line...

    Unix classes Write a bash script that take in three integer numbers from the command line and print out the sum and the average. Name the script awesum.sh hb1170uxb4:~$ ./awesum.sh 100 200 300 sum: 600 average: 200 hb1170uxb4: ~S

  • Write a bash script question.sh that accepts one command line argument which is supposed to be...

    Write a bash script question.sh that accepts one command line argument which is supposed to be a positive integer n. The script should print all odd integers from 1 through n. Write a C or C++ program question.c(pp) to read from stdin as many integers as there are available and then print the squares of all these integers. You should test your code by “./question.sh <n> | ./question” assuming the compiled C/C++ program is question. See the following for a...

  • write the bash script Write a script compress_large_files.sh. This script accepts one or more command line...

    write the bash script Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; let’s call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects the...

  • NIT1202 Operating Systems UNIX Script You are required to write a Bourne Again Shell Script (bash)...

    NIT1202 Operating Systems UNIX Script You are required to write a Bourne Again Shell Script (bash) to manage a menu driven program. When executed the user should be presented with a menu with 4 (four) options: Print the following user information: the home directory, path, userid , login shell together with the current date and time. Ask a user a range and it displays eight random number between the range. [Range difference must be 100. Example Enter a range: 200...

  • Write a bash shell script to print the number of command line arguments and list of...

    Write a bash shell script to print the number of command line arguments and list of command line arguments 3. 12]

  • Write a Bash script called hello that uses command line arguments to allow the user to...

    Write a Bash script called hello that uses command line arguments to allow the user to put two strings after the command name, when the script is being executed. These strings should represent a first and last name. The script should then write out a greeting to the user that includes the first and last name. Here is an example of how the script might work (the first line represents what the user types to launch the script): [user@HAL] hello...

  • Write a bash shell script called direct.sh. This script will take an arbitrary number of command...

    Write a bash shell script called direct.sh. This script will take an arbitrary number of command line arguments. Your script should create directories starting with the first argument, then the second directory inside the first one, and then the next one inside the second one, and so on.

  • Write a Bash script called move that could replace the UNIX command mv. 'move' tries to...

    Write a Bash script called move that could replace the UNIX command mv. 'move' tries to rename the source file (using the UNIX command mv), but if the destination file exists, appends an index number, a sort of version number, to the destination file. So if the user types: move a.txt b.txt and b.txt already exists, move will rename the file to b.txt.1. If b.txt.1 already exists, move must rename the file to be b.txt.2, and so on, until the...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Write a Unix Shell Script to print all Armstrong Numbers between 1 and 1000. Your Program...

    Write a Unix Shell Script to print all Armstrong Numbers between 1 and 1000. Your Program should include following key points: Program should have Header Comments. Program should print all Armstrong numbers between 1 and 1000: 1 153 370 371 407

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