# Thread: Help with C programming!

1. ## Help with C programming!

Here is what we started off with in class, but I have NO CLUE where to go from here...this is one of the first homeworks that I am having difficulty with, and if someone can help me out...it would be greatly appreciated!

Thanks!

Code:
```int fac(int z)
{
int i;
int res =1;
for(i=1; i<=z; i++){
res = res * i;
}
return res;
}
int select(int n, int k)
{
return fac(n)/(fac(k)*fac(n-k));
}```

2. Also, yes. I have read the homework policy and I am new to the forums!

But I wouldn't take time to post a question on here if I haven't struggled with it for a while now!

:[

3. Thanks for reading the homework policy and understanding that, while I can't give you any code, I can hopefully clarify a general approach to solving this problem.

Okay, so we have a factorial function and a select function. Your select function represents the tall parentheses in that formula, with the two stacked numbers. The n on top and k on the bottom. I will use (n k) to represent this in my post.

The teacher is asking to you calculate the total for this function, which he gives the formula for. So for starters, you're going to need a function that calculates the total (I will refer to this as 'calc_total'). It it going to need the parameter n so it knows how far to take this calculation. It will need a loop (we'll use the variable k for this) to iterate from 0 to n. Each iteration will need to call select with the right n and k, and also take care of the +/- sign (-1^some exponent) and the coefficient.

Note that the formula gives the final term as (-1)^n * (n+1) * (n n). Replace the appropriate n's with k's, thinking about which parts of the formula change from term to term and which stay the same.

Then your main function will call calc_total for the requested values and print the results.

Hope that helps.

4. Originally Posted by anduril462
Thanks for reading the homework policy and understanding that, while I can't give you any code, I can hopefully clarify a general approach to solving this problem.

Okay, so we have a factorial function and a select function. Your select function represents the tall parentheses in that formula, with the two stacked numbers. The n on top and k on the bottom. I will use (n k) to represent this in my post.

The teacher is asking to you calculate the total for this function, which he gives the formula for. So for starters, you're going to need a function that calculates the total (I will refer to this as 'calc_total'). It it going to need the parameter n so it knows how far to take this calculation. It will need a loop (we'll use the variable k for this) to iterate from 0 to n. Each iteration will need to call select with the right n and k, and also take care of the +/- sign (-1^some exponent) and the coefficient.

Note that the formula gives the final term as (-1)^n * (n+1) * (n n). Replace the appropriate n's with k's, thinking about which parts of the formula change from term to term and which stay the same.

Then your main function will call calc_total for the requested values and print the results.

Hope that helps.

Are you saying there should only be a for loop and the equation to the nth term?

I think what I don't understand is the notation of the parenthesis part...?

5. I think what I don't understand is the notation of the parenthesis part...?
Are you referring to the tall parentheses with the two stacked numbers? If so, that's your select function or n combinations taken k at a time, often abbreviated nCk (or nCr).

Are you saying there should only be a for loop and the equation to the nth term?
Not quite. You're right that you do only need one for loop (excluding what's already defined in your fac function), but you need the general equation for the k-th term, to compute each time through the loop, not only the last, or n-th, term.

Your for loop will run from 0 to whatever number you specify (according to the problem, this is 'n', which can be 2, 4, 6 or 8). To keep with the terminology of your mathematical equations and the function parameters to select, I would call the loop variable 'k'.

Each iteration of the loop (the loop body) will contain basically 3 calculations that you will combine. The clue to these calculations comes from the last term in your "total = (n 0)..." equation. It's a little confusing, since in the last term, k is the same as n and your prof gave you everything in terms of n. If you think about it though, the top number of (n ?) is always n. It's the bottom number that changes/increments each time. Also, the number in front changes/increments each time through the loop.

So you have 3 parts to each term. The first is the sign (notice the + - + - pattern). This is your (-1)^? part. The second is the coefficient. This is your (n + 1) or (k + ?) term. The last is the stacked parenthetical thing. This is just a call to your select function. For each iteration through the loop, you calculate each of these accordingly and multiply the 3 parts.

Hope that's a little clearer.

6. Originally Posted by anduril462
Are you referring to the tall parentheses with the two stacked numbers? If so, that's your select function or n combinations taken k at a time, often abbreviated nCk (or nCr).

Not quite. You're right that you do only need one for loop (excluding what's already defined in your fac function), but you need the general equation for the k-th term, to compute each time through the loop, not only the last, or n-th, term.

Your for loop will run from 0 to whatever number you specify (according to the problem, this is 'n', which can be 2, 4, 6 or 8). To keep with the terminology of your mathematical equations and the function parameters to select, I would call the loop variable 'k'.

Each iteration of the loop (the loop body) will contain basically 3 calculations that you will combine. The clue to these calculations comes from the last term in your "total = (n 0)..." equation. It's a little confusing, since in the last term, k is the same as n and your prof gave you everything in terms of n. If you think about it though, the top number of (n ?) is always n. It's the bottom number that changes/increments each time. Also, the number in front changes/increments each time through the loop.

So you have 3 parts to each term. The first is the sign (notice the + - + - pattern). This is your (-1)^? part. The second is the coefficient. This is your (n + 1) or (k + ?) term. The last is the stacked parenthetical thing. This is just a call to your select function. For each iteration through the loop, you calculate each of these accordingly and multiply the 3 parts.

Hope that's a little clearer.

I think that just confused me even more!!!

Ahhhh! This program is killing me! Im spending way too much time on it!

7. Sorry! Can you please take a stab (even if it's a really wild one) at this program? Then post the results so I can better see where your thinking is at and help you.

8. You've got the basics, now you need to use them to build up more and more until you have a complete solution. The next step is to make a function that evaluates a single term. Each term has this structure:

sign * multiplier * select(number number)

there is a sign, a multiplier, and two numbers that go into the select function
the sign is based on what n-th term it is, it starts at +1, then alternates between -1 and +1.
the multiplier is one more than what the term number is.
the top number is one of the number you are given from the assignment, (2,4,6, or 8),
and the bottom number is the term number

Code:
```int term(int n, int i) {
int sign = i % 2 ? -1 : 1;
int multiplier = i + 1;
// use the variables in combination with your select function to finish this fuction;
}

so term(4,3) will equal this  -4(4 3)  <-- the parenthesis are the select, with the first number on top```
After this, the next step is to make the whole equation. Each equation is made up of several terms, you need to have a for loop that adds together several terms together.