I am trying to write a C++ program to do this but nobody on Stackoverflow can seem to help me so I thought I'd try to do it myself with some help from you guys.
My post on Stackoverflow can be found here: https://stackoverflow.com/questions/22727196/calculating-a-conditional-probability-that-is-similar-to-a-binomial-sum
My exact question would be, if I had 6 people where the probabilities of each person saying yes is given respectively by $(\frac{1}{2},\frac{1}{3},\frac{1}{4},\frac{1}{5},\frac{1}{6},\frac{1}{7})$ respectively, what is the closed form summation form of the probability that 2 of them say yes?
I can calculate the probability of this directly by hand but I am having difficulty finding a closed form expression.
Let us first generalize, so we don't have to deal with actual numbers. I assume the numbers in your question are just examples, and not the actual numbers you want to use.
Let us say you have $n$ persons. Let the probability that person $i$ says yes be $p_i$. E.g., the probability of person 1 saying yes is $p_1$. Of course, it must follow that the probability of person $i$ saying no is $1 - p_i$. We want to know the probability of exactly $k$ people saying yes.
We can describe all scenarios by a sequence of zeros and ones, where the $i$:th digit is zero if person $i$ says no and one if person $i$ says yes.
E.g., if we have three persons and person 1 says yes, person 2 says no and person 3 says yes, this can be described as the configuration $(1,0,1)$ or ("yes", "no", "yes"). The probability of the configuration $(1,0,1)$ happening in this case is $p_1(1-p_2)p_3$.
Thus, in general, if we are given a configuration $\bar x = (x_1,x_2,\dots,x_n)$, where as before $x_i$ is either 0 or 1, we can easily compute the probability of this configuration happening. Let the probability of a configuration $\bar x$ be $P(\bar x)$.
Now, mathematically we can define the function $l(\bar x) = \sum_{i=1}^n x_i$ to be the number of ones in the configuration $\bar x$ and give the solution: $$p(\textrm{exactly $n$ people say yes}) = \sum_{\bar x: l(\bar x) = k} P(\bar x)$$ i.e., we sum the probabilities of the configurations where we have exactly $k$ ones.
Programmatically, we can use a divide-and-conquer approach. Let us say we want to know the probability of exactly $k$ people saying yes. The first person can say either yes or no (unless $p_1 = 1$). The same for the second person, and so on. Also, if $n-k$ persons have said no, the only way $k$ persons will say yes is if the remaining persons say yes, so in this case we can just compute $p_{n-k}p_{n-k+1}\cdots p_n$.
This can be expressed in code (general function but with your example as example):
Special case: $p_1 = p_2 = \dots = p_n$
In the special case where all the probabilities are the same $p_1 = p_2 = \dots = p_n$ (and independent of each other) we can call the probability that a person answers yes just $p$.
In this case we have a sequence of Bernoulli trials, which are well-studied in probability theory. The probability of exactly $k$ successes (people answering yes) is: $$\binom{n}{k}p^k(1-p)^{n-k}$$ where $\binom{n}{k}$ is a binomial coefficient.