0

I am learning c++ and having a bit of trouble with my homework.

We have to write code using the double variable type and use two variables to calculate the number of permutations of the potential team arrangements…

The question specifies that there are 18 people in the group and you want to divide the group into teams of 3 members.

This is my current code:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int fact(int n) {
    if (n == 0) return 1;
    if (n > 0) return n * fact(n - 1);
}

int main()
{
    double n = 18.0;
    double r = 3.0;
    double answer = fact(n) / (fact(r) * fact(n - r));

    cout << “The number of arrangements = “ << answer << endl;

    system(“pause”);
    return 0;
}

When I run the code I am receiving “The number of arrangements = 1”

This is not the correct answer. Can someone please help me figure out what I am doing wrong?

Thanks!

GooseCake
  • 33
  • 8
  • Your function does not in a theoretical cases return cleanly. Use an `else` or otherwise cover the last possibilities. – Yunnosch Sep 04 '22 at 14:54
  • 3
    If you used a debugger to see what `fact(18)` actually returns, the surprising answer would immediately point you in the right direction, and the bug becomes obvious. And your compiler's warning message would also be a helpful clue. If you don't know how to use a debugger, this is a golden opportunity for you to learn how to use one, and learn how to find and fix bugs in your own code, all by yourself, without the need to ask anyone else for help. Does that make sense? – Sam Varshavchik Sep 04 '22 at 14:54
  • The formula is incorrect btw: You're calculating the ways to compose a single team, when you're supposet do calculate the possiblilities of assembling all 6 teams. You're looking for [something other than `n` choose `r`](https://en.wikipedia.org/wiki/Combination#Number_of_ways_to_put_objects_into_bins) – fabian Sep 04 '22 at 15:03
  • **18!** is 6402373705728000. Your maximum int is probably 2147483647, assuming a 32-bit int (fairly common these days). – Eljay Sep 04 '22 at 15:19

1 Answers1

0

Your fact function is returning an int, not a double.

Since your factorial function is returning an int, fact(n) is an int, and fact(r) * fact(n - r) is also an int.

Together, they will perform integer division, i.e. floor division, not true division.

Edit: Looked at the code again, I realized the problem wasn't integer division, it was the fact that int overflowed, where as double wouldn't overflow until 171!.

Kaze Kuroyuki
  • 39
  • 2
  • 8
  • 1
    Actually, for this specific math formula, integer division is fine, because (assuming 0 <= r <= n) the division will always yield a whole number (ignoring things like integer overflow). – Drew McGowen Sep 04 '22 at 15:03
  • I changed it to: double fact(double n) { if (n == 0) return 1; if (n > 0) return n * fact(n - 1); } And it now produces: “The number of arrangements = 816” But I’m not sure if this is correct either… – GooseCake Sep 04 '22 at 15:16
  • @GooseCake • You could check the math by hand. **18 × 17 × 16 ÷ 3 ÷ 2 ⇒ 816** – Eljay Sep 04 '22 at 15:30