Factorial program using an array.

Thread Solved

Join Date: Nov 2007
Posts: 7
Reputation: DennisB is an unknown quantity at this point 
Solved Threads: 0
DennisB DennisB is offline Offline
Newbie Poster

Factorial program using an array.

 
0
  #1
Nov 8th, 2007
I am having problems trying to figure out exactly what is going wrong with my code. I am trying to create a factorial program using an array. I have tried, unsuccessfully, various ways to make it work but I can not figure it out. Any assistance would be appreciated.

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int iFac[10] = {0};
  6. int iTotal = 0;
  7. int iCount = 0;
  8. for (iCount = 0; iCount < 10; iCount++)
  9. iFac[iCount] = iCount;
  10. iTotal = (iCount * (iCount - 1)) * iCount;
  11. for (iCount = 0; iCount <= 10; iCount++)
  12. printf("\nThe value of the factorial is %d\n", iTotal);
  13. return 0;
  14. }
Last edited by stymiee; Nov 8th, 2007 at 10:58 am. Reason: please use code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Factorial program using an array.

 
0
  #2
Nov 8th, 2007
You're storing the factorials in an array, but that doesn't change how you calculate the value. Do you know how to calculate a factorial?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 7
Reputation: DennisB is an unknown quantity at this point 
Solved Threads: 0
DennisB DennisB is offline Offline
Newbie Poster

Re: Factorial program using an array.

 
0
  #3
Nov 8th, 2007
Well, the math formula reads, for example:

5! = 1 x 2 x 3 x 4 x 5 = 120
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Factorial program using an array.

 
0
  #4
Nov 8th, 2007
>Well, the math formula reads, for example:
Actually, it's more like: n! = \displaystyle\prod_{k=1}^nk, but yours is close enough. However, knowing the formula doesn't mean you can write the code to implement it. Can you write a function that when given a value of n, produces the factorial of n? My point is that calculating n! is independent of what you do with the result. If you have a function that gives you n!, you can store it in an array just as easily as printing it:
  1. int fac[10];
  2. int i;
  3.  
  4. for ( i = 0; i < 10; i++ )
  5. fac[i] = factorial ( i );
  6.  
  7. for ( i = 0; i < 10; i++ )
  8. printf ( "%d\n", fac[i] );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 7
Reputation: DennisB is an unknown quantity at this point 
Solved Threads: 0
DennisB DennisB is offline Offline
Newbie Poster

Re: Factorial program using an array.

 
0
  #5
Nov 8th, 2007
So the factorial equation is what I should enter in the code where you have written the word factorial?

fac[i] = factorial ( i );

I am unsure of how to make the factorial n! into a math equation when using it for code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Factorial program using an array.

 
0
  #6
Nov 8th, 2007
>what I should enter in the code where you have written the word factorial?
That's a function. You don't have to write a function, but you do have to loop from 1 to n and store the running product to calculate a factorial. Something like this is a translation of the formula into code:
  1. int prod = 1;
  2. int k = 1;
  3.  
  4. while ( k <= n )
  5. prod *= k;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 7
Reputation: DennisB is an unknown quantity at this point 
Solved Threads: 0
DennisB DennisB is offline Offline
Newbie Poster

Re: Factorial program using an array.

 
0
  #7
Nov 8th, 2007
OK, so the reference to factorial (i) is a declared function outside of main correct? In that declared factorial function is the loop you referenced to calculate the factorial? What does the operator *= mean? I am new at C so that's why I keep asking these questions.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Factorial program using an array.

 
0
  #8
Nov 8th, 2007
>What does the operator *= mean?
It means you should check your C reference. Not many people like to explain things that are easily discovered by looking in a book or searching google. In this case, *= means multiply and assign using the left hand operand as the left operand of the multiplication. It's functionally equivalent to this:
  1. prod = prod * k;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Factorial program using an array.

 
0
  #9
Nov 8th, 2007
Draw a flow chart or write some pseudo code on paper first to help you visualise what is going on.
Last edited by iamthwee; Nov 8th, 2007 at 6:03 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 7
Reputation: DennisB is an unknown quantity at this point 
Solved Threads: 0
DennisB DennisB is offline Offline
Newbie Poster

Re: Factorial program using an array.

 
0
  #10
Nov 9th, 2007
I do understand arrays, somewhat. I created a Fibonacci Sequence array and tried to apply the same thinking to the factorial program but could not figure it out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC