Having some difficulties with a C++ assignment.

I am needing help writing a c++ program that takes two variables, n and k, and calculates all the possible permutations from the number of choices, n, and the total number of combinations using the number of picks, k, from those choices. So you don't think I am trying to cheat and get someone to do my work for me I will show you what I have so far. This is a 101 class so it needs to use the simplest form for this program. It has to include functions.

#include <iostream>

using namespace std;

long permut ( long n, long temp1)
{
return temp1*n;
}
long precombo ( long k, long temp2)
{
return temp2*k;
}

int main()
{
long n, k;
long t1, t2;
long combo, precombo, permut;
long temp1, temp2;

cout << "Please enter the number of possible choices: ";
cin >> n;

cout << "Please enter the number of items to select: ";
cin >> k;

for (temp1 = n; n > 1; n--)
{
t1 = permut(temp1, n);
}
for (temp2 = k; k > 1; k--)
{
t2 = precombo(temp2, k);
}
combo = permut / precombo;

cout << "The number of possible permutations is: " << permut << endl;
cout << "The number of possible combinations is: " << combo << endl;

return 0;
}
[code] "Please use code tags" [/code]
¿what is the difference between permut() and precombo() ? They just multiply two numbers
Your formula is incorrect, you better use http://en.wikipedia.org/wiki/Tartaglia%27s_triangle
Permutations is calculating n*(n-1)(n-2)(n-3)*......*2 (one is understood because 1 is reduntant. The formula the teacher has given (despite what the correct formula is this is what the teacher wants, is the calculations of the permutation / k(k-1)(k-2).....*2.

I am trying to make a for loop where the multiplication continues until n=1. I am trying to use the same formula for k. Then what the teacher wants for combinations is basically the permutation (n!) divided by what I have as the precombo (k!).

Here is the actual assignment:

You are to write a C++ program to prompt for and read two integers: n and k. The number n is the number of items in a collection - like 52 for a deck of cards. The number k is the number of items to select from the collection - like 5 for a poker hand.

Your goal is print the number of permutations of n things taken k at a time and the number of combinations of n things taken k at a time. The number of permutations is generally greater since combinations ignore order.

Let's support you are dealing cards. Then there are 52 choices for the first card. After dealing the first card there are 51 choices for the second card. After dealing 2 cards there are 50 choices for the third card, ...

So we see that there are P = 52*51*50*49*48 possible permutations.

Now if we don't care about the order of the dealing there are few possible choices (combinations). You would divide P by the number of different ways you can order 5 things. The number of different orderings of 5 things is 5*4*3*2*1.

So we get the number of combinations: C = 52*51*50*49*48/(5*4*3*2*1)

You need to write functions to compute the number of permutations and the number of combinations.

You will have a moderately small limit on n and k if you use int for the types for your variables. You can compute larger values using the C++ type long which is an 8 byte integer.
I am aware I am probably way off though. I am so lost! I have been searching google, reading over the chapters in the book, the power point presentations from the teacher, and the assignment for hours. It just doesn't seem to be sinking in.
Everything I have found on google is far more complicated than anything we have learned so far in my class. I have redone this program 20+ times now so far and I can't get the loop to work right. This is what I have now, starting from scratch, just trying to get the first loop to work, to calculate n!. Trying to make a for loop where we start with temp1 = n. Then we enter the loop and do n-- (or n-1), then multiply temp1 * the new n, or n(n-1). In the loop I want to reassign temp1 = temp1 * n, aka temp1 = n(n-1). Then the loop should repeat itself and subtract another 1 from n and multiply that times the new temp1, and so on and so forth until n = n-k. The loop never terminates though.

#include <iostream>

using namespace std;

int main()
{
long n, k;
long permutation, combinations;
long t1, t2;
long temp1, temp2;

cout << "Please enter the number of choices for n: ";
cin >> n;

cout << "Please enter the number of picks from those choices for k: ";
cin >> k;

for (temp1=n; ; n--)
{
temp1 = temp1 * n;
cout << n;
}

permutation = temp1;

cout << "The number of permutations is: " << permutation << endl;
return 0;
}
Then the loop should repeat itself and subtract another 1 from n and multiply that times the new temp1, and so on and so forth until n = n-k. The loop never terminates though.


1
2
3
long permutations = 1 ;
for ( temp1= n; temp1 != n-k ; --temp1)
     permutations *= temp1 ;
OMG! Thankyou so much! From that little bit of help I was able to construct my for loop for the combinations formula, and the program works! Thankyou thankyou thankyou! I was ready to put my head through a wall from frustration last night lol.
Topic archived. No new replies allowed.