Calling a Function

This is a discussion on Calling a Function within the C Programming forums, part of the General Programming Boards category; I have to write a program to find out n combination r (mathematics) and i have written it but when ...

1. Calling a Function

I have to write a program to find out n combination r (mathematics) and i have written it but when i try to call the function it doesnt work. Any help would be greatly appreciated.

Code:
```//Edward Grant, 02159511, Assignment 5, 159.101

#include <stdio.h>
int factorial (int x);
int n, r, result;
int main() {
n=-1;
r=13;
while ((n<0)||(n>12)) {
printf("Please enter a value for n\n");
printf("It must be between 0 and 12\n");
scanf("%i", &n);
}
while ((r>n)||(r<0)) {
printf("Please enter a value for r\n");
printf("It must be less than your value for n and greater than 0\n");
scanf("%i", &r);
}
result = factorial (n);
printf("The answer for %i Combination %i is %i", n, r, result);
}
int factorial (int x) {
int counter, temp1, temp2;
counter = 0;
temp1 = 1;
temp2 = x - counter;
while (temp2 > 0) {
temp1=temp1*x;
counter=counter+1;
}
return temp1;
}```

2. re: Calling a Function

on the line in main where i call the function, there should actually be... result = factorial (n) / (factorial (n-r) - factorial (r));

3. >>it doesnt work.
3 magic words that don't mean too much. Can you be more descriptive please

4. when it gets to the point when it should be calling the function, it stops and does absolutely nothing at all...just sits there

5. >it stops and does absolutely nothing at all...just sits there
Code:
```while (temp2 > 0) {
temp1=temp1*x;
counter=counter+1;
}```
Now tell me where temp2 is changed so that the loop condition can be met any time before the heat death of the universe.

6. Are you sure it doesn't call it?

Look at your loop within the factorial() function. What's stopping it going around forever?

7. just put a line in the loop to change temp2 by x - counter

8. > Now tell me where temp2 is changed so that the loop condition can be met any time before the heat death of the universe
LOL - now that's what I said the last time he tried to write factorial

9. Yeah it doesn't look like you will come out of the loop. Why don't you try using breakpoints to debug your program to figure it out. Microsoft Visual C++ 6.0 will help you big time. Good luck.