I'm currently doing an assignment and I can't figure out why I'm getting an incorrect output, this is the assignment description:
Level 1: Begin by writing a program that prints perfect numbers (badness 0) up to values less than 10,000. Each number should be separated by a single space. For example, quitegood 100 should print 6 28.
Level 2: Extend the program so that the badness limit can be specified as a second command-line parameter. For example, quitegood 100 3 will print 2 3 4 6 8 10 16 18 20 28 32 6
I'm currently having trouble with level 2.
Code:
int calculateBadness (int candidate);
bool isDivisor (int factor, int candidate);
bool isDivisor (int factor, int candidate) {
if (candidate % factor == 0) {
return true;
} else {
return false;
}
}
int calculateBadness (int candidate) {
int total;
for (int factor = 2; factor < candidate; factor++) {
total = 1;
if (isDivisor (factor, candidate)) {
total += factor;
}
}
int badness = candidate - total;
return badness;
}
int main (int argc, char* argv []) {
const int limit = argc > 1 ? atoi (argv [1]) : 1000;
const int badnessLimit = argc > 2 ? atoi (argv [2]) : 0;
for (int candidate = 2; candidate < limit; candidate++) {
int badness = calculateBadness (candidate);
if (badness < badnessLimit) {
cout << candidate << endl;
}
}
}
With the input asked (quitegood 100 3) I keep getting the output
2
3
Any help is appreciated :)