-1

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

  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Mar 13 '19 at 10:01
  • @Raedwald nice q&a, though flagging as duplicate isnt that helpful. I mean you could flag 90% of questions as duplicate of "how to use a debugger" but that is unlikely to help anybody – 463035818_is_not_an_ai Mar 13 '19 at 10:04
  • @user463035818 If the OP has evidently not used a debugger and says "Any help is appreciated", telling them how to use a debugger **is** an answer to their question. – Raedwald Mar 13 '19 at 10:05
  • @user463035818 "I mean you could flag 90% of questions as duplicate of "how to use a debugger"". Sadly, you could. "but that is unlikely to help anybody": I strongly disagree. – Raedwald Mar 13 '19 at 10:07
  • @Raedwald i'd argue that "Any help is appreciated" is not a good question to begin with (also the actual bug is just a typo), though I see your point – 463035818_is_not_an_ai Mar 13 '19 at 10:08

1 Answers1

1

Looks like you've just misplaced your initialization of the total variable, it should be

int calculateBadness (int candidate) {
  int total = 1;

  for (int factor = 2; factor < candidate; factor++) {
    if (isDivisor (factor, candidate)) {
      total += factor;
    }
  }

  int badness = candidate - total;
  return badness;
}

In general it's a good idea to combine declaration with initialization. instead of declaring in one place and initializing in another.

john
  • 85,011
  • 4
  • 57
  • 81