| | |
Factorial program using an array.
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 7
Reputation:
Solved Threads: 0
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.
c Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int iFac[10] = {0}; int iTotal = 0; int iCount = 0; for (iCount = 0; iCount < 10; iCount++) iFac[iCount] = iCount; iTotal = (iCount * (iCount - 1)) * iCount; for (iCount = 0; iCount <= 10; iCount++) printf("\nThe value of the factorial is %d\n", iTotal); return 0; }
Last edited by stymiee; Nov 8th, 2007 at 10:58 am. Reason: please use code tags
>Well, the math formula reads, for example:
Actually, it's more like:
, 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:
Actually, it's more like:
c Syntax (Toggle Plain Text)
int fac[10]; int i; for ( i = 0; i < 10; i++ ) fac[i] = factorial ( i ); for ( i = 0; i < 10; i++ ) printf ( "%d\n", fac[i] );
I'm here to prove you wrong.
>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:
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:
C Syntax (Toggle Plain Text)
int prod = 1; int k = 1; while ( k <= n ) prod *= k;
I'm here to prove you wrong.
>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:
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:
C Syntax (Toggle Plain Text)
prod = prod * k;
I'm here to prove you wrong.
![]() |
Similar Threads
- factorial using a for loop (C++)
- Array in Dice Program (Java)
- Array without twice the same number? (C)
- Factorial program (Java)
- How do I create a program using an Array ? (C++)
- Using an Array to declare multiple bitmap handlers (C)
Other Threads in the C Forum
- Previous Thread: read data file in C
- Next Thread: depth of childs of binary tree
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






