command line arguments......

I have written this code that times the calculation of certain values. It uses recursive and iterative functions. It order to compare the time taken using each method.

I need to edit this program to take in command line arguments for n and r values (supplied in any order). I need to use the options -r and -n to note when n and r are being entered. Also if the user omits one or both options then the program should prompt the user for the missing information.

I know I need to edit the int main to:
int main(int argc, char*argv[])

However,I am having trouble figuring out how to edit the program for the above adjustments(command line arguments ,etc)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
  #include <sys/time.h>
  #include <cstdlib>
  
  using std::cout;
  using std::endl;
 
  long double factorial(long double n);
  long double iterative_func(long double n, long double r);
  long double recursive_func(long double n, long double r);
  long double myfunction(long double n, long double r);
 
  int main()
  {
          myfunction(20,3);
          myfunction(1000,10);
  }
 
  long double myfunction(long double n, long double r)
  {
     long double result1, result2;
 
          typedef struct timeval time;
          time stop, start;
 
          gettimeofday(&start,NULL);
 
          result1 = iterative_func(n,r);
          cout<< "Iterative total = "<< result1 << endl;
 
          gettimeofday(&stop, NULL);
 
          if (stop.tv_sec > start.tv_sec)
             cout << "Time spent in seconds: " << stop.tv_sec - start.tv_sec << endl;
          else
             cout << "Time spent in microseconds: " << stop.tv_usec - start.tv_usec << endl;
          return 0;
  }
 
  long double factorial(long double n)
 {
          long double total = 1;
          long double i;
 
          for (i = 1; i < n; i ++)
          {
                  total *= i;
          }
          return total;
  }
 
  long double recursive_func(long double n, long double r)
  {
          if (n == r || r == 0)
             return 1;
          else
             return (n/r) * recursive_func(n-1, r-1);
  }
 
  long double iterative_func(long double n, long double r)
  {
          long double total = 1;
 
          total = factorial(n)/ (factorial(r) * factorial(n - r));
 
          return total;
  }
Ok so you have
int main(int argc, char*argv[])

your command line might look like

>command r 2 n 4
or
>command n 4 r 2

First I would find out how many arguments exist

1
2
3
4
5
6
7
if (argc == 1)
{// get R and N}
else if (argc == 3)
{// get R or N}
else if (argc == 5)
{// R & N assumed good, calculate
// Then you need to know which is R and which is N 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (argv[2]==n)
{argv[3]=N;}
else if (argv[4]==n)
{argv[5]=N;}
else
{// some error msg}

if (argv[2]==r)
{argv[3]=R;}
else if (argv[4]==r)
{argv[5]=R;}
else
{// some error msg}

}
else
cout << "not enough arguments";


Then I would change your
double n
double r
to something else, if your using n and r for your input.

I don't think you have to do so, but it could get confusing when reading/debugging.
Last edited on
Topic archived. No new replies allowed.