- Code: Select all
#include <iostream>
#include <cstdlib>
using namespace std;
int gcd(int a,int b);
int main(int argc,char *argv[])
{
int a, b;
if (argc != 3) {
cout << "SYNTAX: " << argv[0] << " <num1> <num2>" << endl;
cout << "Returns the greatest common divisor of num1 and num2." << endl;
return -1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
cout << "The greatest common divisor of " << a << " and " << b;
cout << " is: " << gcd(a,b);
return 0;
}
int gcd(int a,int b)
{
int t;
while(b) {
t = b;
b = a % b;
a = t;
}
return a;
}
Starting from the top, there is a "*" before argv[], what does that do? I know that argc is the number of arguments, and I understand that there will be 3 arguments for this code, gcd, then two numbers. the if (argc != 3) part is making sure the user entered gcd and then 2 numbers (or two arguments), but after that...what does atoi do? other than this I understand the code...I think.


