User defined function problem

This is a discussion on User defined function problem within the C Programming forums, part of the General Programming Boards category; i just finished writing a function to solve the factoral of a user inputted int. In my main loop i ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    23

    User defined function problem

    i just finished writing a function to solve the factoral of a user inputted int. In my main loop i need to display the function and let the user enter as many numbers as they want. After the user enters the numbers they want, they enter a key to quit the program and the factoral of all of the inputted numbers are displayed.

    I cannot think right now and don't know what to do. Any tips are appreciated. My code i have so far is below:

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    #define ot (float)1/3
    
    void factoral (long n)
    {
    
    	double power;
    	double dividen;
    	double sqrt_div;
    	double num;
    	double denom;
    	long answer;
    
    	printf("\n\nPlease enter the number you want to know the factoral of: ");
    	scanf("%ld", &n);
    
    	power = pow(n , n);
    	dividen = (2 * n + ot) * PI;
    	sqrt_div = sqrt(dividen);
    	num = power * sqrt_div;
    	denom = exp(n);
    	answer = ceil(num / denom);
    
    	printf("\nThe factoral of %ld is approximately: %ld\n\n", n, answer);
    
    	return;
    }
    
    #include <stdio.h>
    
    void main(void)
    {
    	
    	double num;
    
    	factoral(num);
    
    	return;
    }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Hmm. Seems to work OK for me. That is, I get the right answers for n=2,3,4,5. What problem are you getting?

    Don't use void main. Use int main, and return zero to signal that the program has finished correctly.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What's the point of passing the variable n into your function if the first thing you do is trample all over it with user input?

    Moving *everything* into a function is not the way to go... In your case I would get my user input in Main() then pass the user's value into the function. Not only does this make more efficient use of your code, it allows for expansion with menus, other functions etc.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    i know the main is pointless so far, but in the main i need to make it so the user can enter as many values into the function that they want, and once they enter a key i select like "Q" to quit it prints all the factorals of the numbers they choose.

    The function works, i just don't know how to let the user enter as many values as they want and once they quit it displays the factorals

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    what u meant by this program? is it the factors of the number?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 337higgin View Post
    i know the main is pointless so far, but in the main i need to make it so the user can enter as many values into the function that they want, and once they enter a key i select like "Q" to quit it prints all the factorals of the numbers they choose.

    The function works, i just don't know how to let the user enter as many values as they want and once they quit it displays the factorals
    As I said... the first step is to get the user input out of the function itself. Move it into main and get it calling the function for you after the user makes an entry. Get it displaying the correct answers. After that you can put the input function in main in a loop and you should be off to the races...

    A properly written function does only one thing. The idea is to hand it a value and get back a result...

    Think in smaller blobs!

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    I could not figure out how to loop this without using a goto which my teacher does not like. The program works like i want but i really wish i didn't use a goto. Also, im using longs and doubles but my program gives me a large negative number when you enter any value 13 and above.

    Here is my code. Do you see any errors? Thanks in advance:

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    #define ot (float)1/3
    
    void factoral (long n)
    {
    
    	double power;
    	double dividen;
    	double sqrt_div;
    	double num;
    	double denom;
    	long answer;
    
    	power = pow(n , n);
    	dividen = (2 * n + ot) * PI;
    	sqrt_div = sqrt(dividen);
    	num = power * sqrt_div;
    	denom = exp(n);
    	answer = ceil(num / denom);
    
    	printf("\nThe factorial of %ld is approximately: %ld", n, answer);
    
    	return;
    }
    
    #include <stdio.h>
    
    void main(void)
    {
    	long num;
    x:
    	printf("\nPlease enter the number you want to know the factorial of. ");
    	printf("\nEnter 'Q' if you wish to quit: ");
    	scanf("%ld", &num);
    
    if(getchar() != 'Q')
    {
    	if(num > 0)
    	{
    		while(num)
    		{
    			factoral(num);
    			goto x;
    		}
    	}
    	else
    	printf("You cannot find the factorial of a negative number. Try again.\n");
    }
    
    	printf("\n");
    	return;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    goto is to be avoided and is helpful only in very rare cases... and certainly not from inside a nest of if() statements...

    Code:
    #include <stdio.h>
    
    int main(void)  
    {
       long num;
       do
          {
            printf("\nPlease enter the number you want to know the factorial of. ");
            printf("\nEnter '-1' if you wish to quit: ");
            if (scanf("%ld", &num) == 1 && num > 0 )
              factoral(num);
        }
        while(num > 0);
       return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    Thanks. I never understand when i should use certain loops like while, for, or do. Thanks for the help

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 337higgin View Post
    Thanks. I never understand when i should use certain loops like while, for, or do. Thanks for the help
    Grab some tutorials and read up on what the various loops do... This is core programming skill, you can't let this one slide...

    while() loops execute while their entry condition is true ... Use them when coming into a loop with a possibly unknown state...
    Code:
    int i = ???
    while ( i < 11 )
     {  // whatever
    }
    Enter the loop and continue as long as i is less than 11... if i is > 10 on entry the loop never executes.


    do - while() loops execute at least once, then exit when the condition is untrue...
    Code:
    int i = ???
    
    do 
       { // whatever
       }
    while ( i < 11);
    This loop is guaranteed to execute at least once and will continue until i is not < 11 ...


    for() loops start at the stated value, run until the condition is false, managing the loop value as they go...
    Code:
    for (int i = 2367; i > 0; i -= 2)
      { //whatever
    }
    This loop starts at 2367 and will run until i is less than 1, subtracting 2 from i on each repetition.
    (For loops are very flexible... read up on these very carefully... )

    duzathelp?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 03:40 AM
  2. Replies: 9
    Last Post: 10-19-2009, 05:46 PM
  3. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 05:21 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 01:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21