stuck on arrays...

I have written a program for entering 2 numbers, the program (tabs may not be lined up properly due to copy over) will time the calculation and printout the results. It uses functions and arrays. The input is entered at the same time as the file name. When my program asks user if they would like to run it again and user says yes, it just runs the same set of numbers. The program should accept if it is entered as:

./combinations -n 50 -r 8 //numbers are just examples
or
./combinations -r 8 -n 50
or
./combinations -n -r 50 8 // this part is not working as expected
or
./combinations -r -n 8 50

Also, Did I unnecessarily write code for the user to enter n and r. Because my program works with this result as well...

./combinations
enter a number for n:
50
enter a number for r:
8
calculations....time here
Do you want to try a new set of numbers y/n?

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<iostream> 
#include<sys/time.h> 
#include<cstdlib> 

using namespace std; 

     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); 

     const int MaxChars = 80; 

int main(int argc, char* argv[]) 
{ 
         int n = 0; //-n 
         int r = 0; //-r 
         char input[MaxChars]; //for the user input 
         char answer; //for the input from user to continue program 

      do //do while loop to continue program if user enters yes 
     { 
// checking for -n or -r 
         if (argc == 5) 
        { 
            if(argv[1][0] == '-' && argv[1][1] == 'n') 
            { 
                n = atoi(argv[2]); 

           if(argv[3][0] == '-' && argv[3][1] == 'r') 
                r = atoi(argv[4]); 

          else 
          { 
              cout << "Usage: ./combinations [-n <number> [-r <number>]" << endl; 

              return 0; 
          } 
     } 
// - r, - n 
     else if(argv[1][0] == '-' && argv[1][1] == 'r') 
    { 
          r = atoi(argv[2]); 
       
         if(argv[3][0] == '-' && argv[3][1] == 'n') 
               n = atoi(argv[4]); 
         else 
        { 
             cout<< "usage: ./combinations [-n<number>] [-r <number>]" << endl; 
             return 0; 
         } 
    } 
    else 
    { 
        cout<< "Usage: ./combinations[-n <number>] [-r <number>]" << endl; 
        return 0; 
     } 
  } 
// neither –r or -n 
   else if(argc == 1) 
   { 
         do 
         { 
             do 
             { 
// enter the values from the keyboard 
                    cout << "Enter n: "; 
                    cin.getline(input, MaxChars); 
                     n = atoi(input);
 
// if user left it blank 
                     if(input[0] == 0) 
                    { 
                            cout<< "Please enter a value for n: "<< endl; 
                            n = 0; 
                    }
 
//if user entered something other than a number 
                   else if(n == 0 && input[0] != '0') 
                   { 
                            cout << "Please enter a numeric value for n: " << endl; 
                            n = 0; 
                    } 

// if user enter a negative number 
                   else if (n < 0) 
                   { 
                          cout << "Please enter a positive value for n: "<< endl; 
                          n = 0; 
                    } 
             }while(n <= 0); 

//enter value for r 
              do 
              { 
                     cout << "Enter r: "; 
                     cin.getline(input, MaxChars); 
                     r = atoi(input); 

// if user left r blank 
                   if (input[0] == 0) 
                  { 
                         cout << "Please enter a value for r: "<< endl; 
                         r = 0;  
                   }
 
// if user enter something other than a number 
                 else if (r == 0 && input[0] != '0') 
                 { 
                        cout<< "Please enter a numeric value for r: " << endl; 
                        r = 0; 
                  } 
// if user entered a negative number 
                 else if (r <=0) 
                 { 
                          cout << "Please enter a positive numeric value for r: "<< endl; 
                          r = 0; 
                 } 
           }while(r <= 0); 

// check if n is less than r 
             if (n < r) 
                     cout << " n is less than r "<< endl; 
        }while(n < r); 
    } 
     else 
     { 
             cout << "Usage: ./combinations [-n <number>] [-r <number>]" << endl; 
              return 0; 
      } 
//calling myfunction 
   
           myfunction(n, r); 

// code to continue program if user enters yes. 

           cout << "Would you like to try again with a new set of numbers, (y/n)?" <<endl; 
           cin >> answer; 
      }while (answer == 'y');
 
      return 0; 
}
Last edited on
What's the problem?
Oh, sorry. the problem is that the program should be able to do this option:

./combinations -n -r 50 8 //numbers are examples
or
./combinations -r -n 8 50


It doesn't do the above when I run the program as above. This is the result output:
(if I enter ./combinations -n -r 50 8): output is this:

Usage: ./combinations [-n <number> [-r <number>]

(if I enter ./combinations -r -n 8 50) output is this:

usage: ./combinations [-n <number>] [-r , <number>]

Along with what it currently does:

1
2
3
./combinations -n 50 -r 8

and ./combinations-r 8 -n 50



So, it should be able to run in the following ways:

./combinations -n 50 -r 8 //it does run

and
./combinations-r 8 -n 50 //it does run

and

./combinations -n -r 50 8 //it doesn't run

and

./combinations -r -n 8 50 // it doesn't run

It is also suppose to ask user if the want to try new set of numbers. Which it does do if I run program as:
./combinations

And then user inputs values while program is running which is fine, but like I was saying, it also has to ask user same thing if the numbers are enter as:

./combinations -n 50 -r 8 //it does run

and
./combinations-r 8 -n 50 //it does run

and

./combinations -n -r 50 8 //it doesn't run

and

./combinations -r -n 8 5

And I don't even know if it is possible to make that happen......
Last edited on
Parsing a command line is always a chore. There's a standard getopt() library that GNU programs ought to use, but it won't solve your problem. Let's work thru it.

You expect an arg followed eventually by a value. So whenever you see an arg, you need to queue the expected value. A possible algorithm looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
foreach arg
    if (arg == -n)
        push expect n
    else if (arg == -r)
        push expect r
    else value is a number
        if (queue empty)
            ERROR: not expecting a number
        else
            pop queue
            if (expecting n)
                n = number
            else if (expecting r)
                r = number
            endif
        endif
    else
        ERROR: expect number or option
    endif


Does that make sense to you?
Last edited on
I think so:

the code you posted.....would that replace the if/else code I have that prompts user or input and does the error checking for that input?

Also I just realized why i am getting the printout message:

Usage: ./combinations [-n <number>] [-r <number>].......because I told it too in line 128

so let me reword that cout statement that is more understandable to a user running the program. then re run the code to see what is and isn't working:

I still think that the

./combinations -r -n 8 50
and
./combinations -n -r 50 8

will not work. Let me see...........
Yes, that would replace your parsing algorithm.

If you follow the algorithm and want to use it, but need help with the implementation, please ask.
Okay so I did discover the following when I reworded:

1. properly runs when:

./combinations -r 8 -n 50 //but doesn't error check for negatives, I don't even
know if it should?
./combinations -n 50 -r 8 //again doesn't error check for negatives, I don't
even know if it should?
if -r or -n or both are left blank //gets proper message of how to run the
program

2. doesn't run when:

./combinations -r -n 8 50

or

./combinations -n -r 50 8

**as a side note, I wanted to try to have program ask user if they wanted to try with new set of numbers, but until I get above problems fixwed I wont worry about that
Last edited on
I misread the requirements the user is NOT supposed to be able to enter as:

./combinations -r -n number number
or
./combinations -n -r number number

but still I do have the problem of the code not error checking for the negative numbers, it is running without checking.....

1. properly runs when:

./combinations -r 8 -n 50 //but doesn't error check for negatives, I don't even
know if it should?

./combinations -n 50 -r 8 //again doesn't error check for negatives, I don't
even know if it should?

if -r or -n or both are left blank //gets proper message of how to run the
program

it is also not recognizing when n is less than r
Last edited on
Also I did some edits and comment out everything (if else statements) from lines 60 to 125.

and program still runs, however with the problem of not checking for negatives or if n is less than r and not error checking for non number values
Last edited on
This is what I have at this point: (and doesn't compile)

1. Added a do while loop to check if n < r //not working
2. need to error check if r is negative and/or n is negative
3. need to error check if r is not a number and/or n is not a number

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 #include<iostream>
  #include<sys/time.h>
  #include<cstdlib>
 
  using namespace std;
 
  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);
 
  const int MaxChars = 80;
 
  int main(int argc, char* argv[])
  {
          int n = 0;   //-n
          int r = 0;   //-r
        //  char input[MaxChars];
 
  // checking for -n or -r
 
   do
   {
          if (argc == 5)
          {
              do  // to check for negative and non numbers
               {
                  if(argv[1][0] == '-' && argv[1][1] == 'n')
                     n = atoi(argv[2]);
               }while (n < 0 || n == 0 && input[0] != '0'); //check for negative and none number 
 
             do  // to check for negative and non numbers
             { 
                  if(argv[3][0] == '-' && argv[3][1] == 'r')
                     r = atoi(argv[4]);
             }while (r < 0 || r == 0 && input[0] != '0'); //check for negative and none number 
 
                  else
                     {
                          cout << "Please enter the values in the following format: ./combinations [-n (number) -r (number)]" << endl;
                          return 0;
                     }
          }
          else if(argv[1][0] == '-' && argv[1][1] == 'r')
          {
             r = atoi(argv[2]);
 
             if(argv[3][0] == '-' && argv[3][1] == 'n')
               n = atoi(argv[4]);
             else
             {
                  cout<< "Please enter values in this format: ./combinations [-r <number>] [-n <number>]" << endl;
 
                  return 0;
             }
          }
         else
         {
                  cout<< "Please enter values in this format: ./combinations[-n <number>] [-r <number>]" << endl;
 
                  return 0;
         }
          
 
            if (n < r)
                    cout << " n is less than r "<< endl;
   
           
             else
             {
                 cout << "Please enter the values in this format: ./combinations [-n <number>    ] [-r <number>]" << endl;

                 return 0;
            }
   }while(n < r);        

         myfunction(n, r);

         return 0;
 }

 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;

                 gettimeofday(&start, NULL);

 result2 = recursive_func(n, r);
                 cout<< "Recursive total= "<< result2 << 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(r < 0 || r > n)
            return 0;
         if (r < 1 || n == r)
            return 1;
         else
            return recursive_func(n-1, 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;
 }
Last edited on
I need to change the do while loops to if/ else statements , because if user enters wrong values etc, the program should end . for the user to start over.
Topic archived. No new replies allowed.