Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started