I posted earlier today with my code dilemma, but was completely confused about what was supposed to be the resulting output and how the user was to enter the information
So, here are my problems
when user enters:
./combinations -r 8 -n 3
output is:
Iterative total = 0.00014881
Time spent in microseconds:97
Recursive total= 0
Time spent in microseconds: 5
**this is wrong it is suppose to error check for when n < r
when user enters:
./combinations -r 50 -n 8
output is:
Iterative total = 2.50543e+09
Time spent in microseconds:98
**It is not giving the recursive information
when user enters:
./combinations -r left blank -n 7
Output is:
Please enter the values in this format: ./combinations [-n <number>] [-r <number>]
** This is correct output
when user enters:
./combinations -r 9 -n left blank
output is:
Please enter the values in this format: ./combinations [-n <number>] [-r <number>]
** This is correct output
when user enters non numbers for either -r or -n program runs as if both were numbers.
How do I error check for:
negatives, non-numbers, and when n < r
How do I fix the part when r is less than n it printouts both iterative and recursive information
I would like to make the error messages as functions to avoid repetitive coding.
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
|
#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);
int main(int argc, char* argv[])
{
int n = 0; //-n
int r = 0; //-r
// 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 << "Please enter values in this format: ./combinations [-r <number>] [-n <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<< "Please enter values in this format: ./combinations [-r <number>] [-n <number>]" << endl;
return 0;
}
}
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 [-r <number>] [-n <number>]" << endl;
return 0;
}
//calling myfunction
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;
}
|