Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ we can specify the following for main: int main(int argc, char *argv[]) These parameters are used when the program is invoked from thecommand

In C++ we can specify the following for main:

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

These parameters are used when the program is invoked from thecommand line. The parameter argc tells us how many arguments areused in invoking the program. This includes the program nameitself. For example if we run the program using:

./a.out

Then argc would equal 1 because the only parameter is theprogram name of ./a.out. If we run the program using:

./a.out 45 10

Then argc would equal 3 because there are three parameters(./a.out, 45, and 10). The argv parameter is an array of charpointers (i.e. cstrings). argv[0] points to a cstring thatrepresents the first argument (e.g. ./a.out), argv[1] points to acstring that represents the second argument (e.g. 45) and so on.Here is an example program that outputs all of the command linearguments. We haven't completely covered pointers yet, but you canthink of this as basically an array of strings. You will probablywant to run this on the unix machine or some other place where itis easy to supply command line arguments:

#include

#include

using namespace std;

int main(int argc, char* argv[])
{
for (int i = 0; i < argc; i++)
{
cout << argv[i]<< endl;
}
return 0;
}

Modify this program so that it multiplies all of the numberssent in on the command line. It should take an arbitrary number ofvalues. You can assume they are all integers. For example, here isa sample run:

sebastian@transformer:~/CSCE_A211$ ./a.out 2 4 6 48

To convert a cstring into an integer, you can use the atoifunction. For example, atoi("20") returns back the integer 20.

Any help is appreciated, thank you in advance.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

8.5 Identify several causes of an unfavorable labor rate variance.

Answered: 1 week ago