Hello,

I am currently working off the sticky thread filled with practice problems etc. I have got a couple of the practice problems out of the way, and I am currently working on a program that will find the factoral of a number entered.
So far i have an array that will be filled with the numbers 1 - the number entered, I found a post from Narue that showed me how to find the length of the array, But how can i use that to multiply each element of the array by the next in order to get my factoral?

#include <iostream>
#define length(a) ( sizeof ( a ) / sizeof ( *a ) )

using namespace std;

void fact(int number);

int main() {
    int number = 0;
    cout << " Please enter a number to see its factoral: "<< endl;
    cin >> number;
    cin.ignore();
    fact(number);
    cin.get();
    return 0;
}


void fact(int number){
    int factorals[number];
    int temp_total= 0;
    int total = 0;
    int total_facts = 0;
    for(int i =0; i < number; i++){
            factorals[i] = i;
            }
    total_facts = length (factorals);
    for (int j=1; j <= total_facts; j++) {
            temp_total = factorals[j] * (factorals[j]-1);
            total = total + temp_total;
            }
    cout << " the Factoral is: " << total;
}

My problem is line 29 down. Obviously my math is way off, and i am getting completely wrong answers. any ideas?

Thanks for the help!

line 27: what is that length function you are calling? In your program number is the length of the array so there is no need to call another function just to get that. You don't need total_facts at all, just replace it with number on line 28.

There really is no need for that array at all. Just use the loop counter on line 29 because it contains the same values as that array.

This is all you need

int n = 1;
    for(int i = 1; i <= number; i++)
    {
       n *= i;
    }
Comments
Thank you for the help!

line 27: what is that length function you are calling? In your program number is the length of the array so there is no need to call another function just to get that. You don't need total_facts at all, just replace it with number on line 28.

There really is no need for that array at all. Just use the loop counter on line 29 because it contains the same values as that array.

This is all you need

int n = 1;
    for(int i = 1; i <= number; i++)
    {
       n *= i;
    }

Ancient Dragon, Thank you so much.
The function i was calling was #define length(a) ( sizeof ( a ) / sizeof ( *a ) ) but i didn't need it because i already had the size. And of course this program was much simpler than i had ever imagined.

What exactly is n *= i; I am under the impression it is the same thing as n = n * i; is that correct?


Looks like i have a long way to go before i am as good as the rest of you guys here!

Thanks again

Is there a way to change the title of the thread so that it more accurately reflects the contents?

Is there a way to change the title of the thread so that it more accurately reflects the contents?

Yes, but why now that it's been marked solved ?

Just thought it might be useful to change the name of the thread to reflect the Factoral problem since others were looking for a solution to that as well.

Either way works for me!

This question has already been answered. Start a new discussion instead.