Question

Write a C program such that … the program receives ( following argv[0] ) one or...

  • Write a C program such that …
    • the program receives ( following argv[0] ) one or more integer values as command line arguments
    • the program prints (to standard output) …
      • the command line arguments, with each appearing on a separate line
      • the sum of the integer values that follow argv[0]
  • Hints
    • http://www.cplusplus.com/reference/cstdlib/strtol

THE CORRECT OUTPUT OF THE TEST CASE :

The command line arguments are ...

argv[0] ... "./a.out"

argv[1] ... "2"

argv[2] ... "9"

argv[3] ... "-100"

argv[4] ... "6"

The sum of the integer values following argv[0] is ... -83.

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

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int sum = 0,temp;
for (int i = 1; i < argc; ++i)
{
printf("Arg %d %s\n",i,argv[i]);
temp = strtof(argv[i], NULL);
sum+=temp;
}
printf("The sum of the integer values following argv[0] is %d",sum);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C program such that … the program receives ( following argv[0] ) one or...
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
  • Write a C program that … reads (from standard input) five words, with each of the...

    Write a C program that … reads (from standard input) five words, with each of the words being entered on a separate line prints (to standard output) the words in reverse alphabetical order Hints http://www.cplusplus.com/reference/cstring/strcmp

  • Write a C program that determines the sum of the decimal digits that appear in the...

    Write a C program that determines the sum of the decimal digits that appear in the command line arguments (excluding argv[0]) of the main function. The sum should be zero if there are no command line arguments following argv[0], or if the command line arguments following argv[0] do not include any decimal digits. Example If the command line arguments (following argv[0]) were 7M3 3DoorsDown Sum41 U2 The4Seasons Maroon5 TheB52s then the sum would be 7 + 3 + 3 +...

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

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

  • Using network sockets, write a C program called client that receives three command-line arguments in the...

    Using network sockets, write a C program called client that receives three command-line arguments in the form: client host port file and sends a request to a web server. The command-line arguments are hostRepresents the web server to connect to port Represents the port number where a request is sent. Normally an HTTP request is sent over port 80, but this format allows for custom ports file Represents the file requested from the web server Your program should create a...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • ***Program must compile under Ubuntu! (35 pts) Write a C++ program A4p3.cpp with a class that...

    ***Program must compile under Ubuntu! (35 pts) Write a C++ program A4p3.cpp with a class that is a derived class of your class from problem 2. Add a private intmember variable var2 to this class. Initialize the member variables var and var2 with values between 1 and 50 using a constructor that takes two integer parameters. Add a public member function called getsp that should print out the sum and product of var and var2. In your main function, create...

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

  • What is the output of the following code given the program was called using the command-line:...

    What is the output of the following code given the program was called using the command-line: a.out input.txt output.txt int main(int argc, char* argv[]) { cout << argc; return 0; } Select one: a. 1 b. 2 c. 3 d. 6 which asnwer it is

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