Thread: Calling a Function

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    10

    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. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    10

    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. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>it doesnt work.
    3 magic words that don't mean too much. Can you be more descriptive please
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    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. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you sure it doesn't call it?

    Look at your loop within the factorial() function. What's stopping it going around forever?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    just put a line in the loop to change temp2 by x - counter

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM