C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-22-2009, 11:00 AM   #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?
Ciaran789 is offline   Reply With Quote
Old 11-22-2009, 11:59 AM   #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
nadroj is offline   Reply With Quote
Old 11-22-2009, 01:17 PM   #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.
Ciaran789 is offline   Reply With Quote
Old 11-22-2009, 04:32 PM   #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.
nadroj is offline   Reply With Quote
Old 11-22-2009, 05:24 PM   #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?
Ciaran789 is offline   Reply With Quote
Old 11-22-2009, 05:52 PM   #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.
nadroj is offline   Reply With Quote
Old 11-22-2009, 06:22 PM   #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.
Ciaran789 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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