Question

Write a program that will accept two integers on command line, use the swap subprogram to...

Write a program that will accept two integers on command line, use the swap subprogram to swap them, and then print the two numbers out. For example, I enter a.out 10 15 and the output that would print is 15 10. Please note that if exactly two numbers are not provided on command line, your program should display an error message and end with no further action. The programming language is C Compiler.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to swap the two numbers using command line arguments

#include<stdio.h>

#include<stdlib.h> /*atoi*/

// Function to swap the two numbers that is 'x' and 'y'

void swap(int x, int y)

{

x=x+y;

y=x-y;

x=x-y;

printf("%d %d",x,y);   

}

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

// Check if length of args array is 1

if (argc==1)

{

printf("No command line arguments found. \n");

}

else

{

// Get the command line argument and Convert it from string type to integer type using function "atoi( argument)"
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

swap(num1, num2); // Swap the numbers and print it

}

return 0;

}

Steps to execute program using command line:

1. First install tmd gcc compiler

2. Open cmd and go through the folder where you saved your c program. In my case i have created a folder named HomeworkLib at Desktop, inside this folder, swap.c is my c program

3. Now type : gcc swap.c and press enter

4. After successfully running this line on cmd, a.exe file is created in folder

5. Now type : a.exe 15 25 and press enter.

6. You will get output as 25 15

Screenshot of the folder created :

Screenshot of cmd :

Add a comment
Know the answer?
Add Answer to:
Write a program that will accept two integers on command line, use the swap subprogram to...
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
  • 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

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Write a full program that will accept exactly  three command line arguments that can be interpreted as...

    Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program. Suppose your program is called Add.java. Then running the command java Add 1 2 3 will output 6, and running java Add, for example, will cause the program to shut down. Consider the following recursive method: public static int mystery (int n)...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • In C Programming Language, write a command line argument(s) program. The line should have program_name.exe input_file1.txt...

    In C Programming Language, write a command line argument(s) program. The line should have program_name.exe input_file1.txt “char_string”. The program should print an error message if there are not exactly three arguments on the line. input_file1.txt will have a word in it different than the “char_string”. The program will print on one line the char_string concatenated with input_file1.txt followed by the program name. Upon success print on new line, “Program finished!”

  • In C please Create a program that takes two integers as command line arguments (num, length)...

    In C please Create a program that takes two integers as command line arguments (num, length) and outputs a list of the first length multiples of num. num should be included in the returned array. For example: ./a.out 7 5 -> [7, 14, 21, 28, 35] ./a.out 12, 10 -> [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] ./a.out 17, 6 -> [17, 34, 51, 68, 85, 102] Ma Word starts with a vowel add "yay" to the...

  • 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...

  • 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...

  • Develop a program that accepts integers from command line, sorts the integers into ascending order, computes...

    Develop a program that accepts integers from command line, sorts the integers into ascending order, computes the sum of the integers, and counts the number of even numbers and the number of odd numbers. Solve in C programing language?

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

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