Thread: factorial table problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19

    factorial table problem

    Code:
    #include <stdio.h>
    
    void main()
    {
    	int maxfactorial=0, minfactorial=0, line=0, factorial=0, i=0;
    	
    	printf("enter max factorial\n");
    	scanf("%d", &i);
    	printf("enter min factorial\n");
    	scanf("%d", &minfactorial);	
    	printf("--------------------------------------------------------------------");
    	while (line<=0){
    	printf("\nFactorial Value        Factorial expression            Result");
    	line=line+1;
    	}
    	maxfactorial=i;
    	factorial=i;
    	while ( factorial>0){
    	printf("\n%d" ,maxfactorial);
    	maxfactorial=maxfactorial-1;
    	factorial=factorial-1;
    	}
    
    
    }
    i have wrote that to form part of a factorial table. i can not, however, get the max factorial to only print as far as the value entered for the minimum factorial. can anyone help me out here?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I dont understand what your asking. Say I entered "5" as max and "3" as min, should it print 5*4*3 and stop because it is at the minimum?

    A few questions/points about your code:
    - I dont see a need for the "i" variable--just read the max value directly into the max variable, like you do with min. If you follow the code, "i" is not needed (at least not in the above code).
    - Also:
    Code:
    	maxfactorial=i;
    	factorial=i;
    	while ( factorial>0){
    	printf("\n%d" ,maxfactorial);
    	maxfactorial=maxfactorial-1;
    	factorial=factorial-1;
    Since "i" is always 0, you assign maxfactorial and factorial to "i", which means i = maxfactorial = factorial (3 variables storing the same value). Then in the while loop you decrease maxfactorial by one, same with factorial. So, at all times maxfactorial=factorial... why?

    Stepping back for a minute, I dont know what your even trying to do. You have a print statement that appears to be the heading of the table for factorial value, expression and result. What are these values? Going back to my example input above for "max" = 5 and "min = 3", is "Factorial value" = "5", "Factorial Expression" = "5*4*3" and "Result" = "60" (which, of course, is not 5! = 120). If this is the sample output you want then you print "max", then a tab character or however many spaces to separate, then you loop from max to min, and each time print "max*", and have another variable that stores the result.

    So we iterate from max = 5 down to 3
    set result = 1;

    iteration 1:
    print max
    result *= max
    max--

    iteration 2:
    print max
    result *= max
    max--

    etc

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    yes. it is ment to print 5*4*3 etc untill it gets to the minimum. the factorial value is the number the factorial starts at. the expression is the 5*4*3*2*1 and the result is the answer. this is my first assignment in college and im only very new to C so i will have numerous errors in this. the variable factorial is simply used as a control for loops in the code. it serves no other purpose than to dictate when the loops stop.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Ok well start with what I described above, as you say I was understanding the problem, so what I described should work. When/if you run into problems, post your code and any compiler errors/warnings along with any description of what specifically is the problem.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    with your help i have now managed to get it to stop printing when it reaches the minimum. but i am confused about how to print the Factorial expression in the form of 5*4*3*2*1 etc using iteration. could you explain your method for doing this more?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    See my second post as I described the steps in there.

    You already have a loop that iterates until it reaches the minimum. All you have to do is add a print statement to print the current value. i.e.
    Code:
    int max = 5;
    printf max and tab
    int min = 3;
    int result = 1;
    for (; max >= min; max--)
    {
      result *= max;
      printf max;
    }
    printf result
    Of course this is a combination of code/pseudocode, but this is basically the solution.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    i think i see what to do now. i will give it a try when i get back on to my main computer after college. thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASP.NET question (looping inside a table)
    By Rainbowinblack in forum C# Programming
    Replies: 0
    Last Post: 03-06-2008, 03:10 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. virtual memory
    By sweets in forum C Programming
    Replies: 6
    Last Post: 11-06-2004, 06:55 AM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM