I need some help getting this factorial to work. I don't think I'm getting this to work right since only the first 2 work in the series. I probably could write a function to fix this but there has to be a way to loop it instead. This is what I have so far.

``````#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <iomanip>

using namespace std;

int main()

{

double a,b,c,d,e,f,g,x,sum=0,fact=1;

cout << "This finds probability at the airport." << endl;

cout << " ";

cout << endl << endl;

cout << "Enter the number of plane arrivals per minute (lamda). " << endl;
cin >> a;

cout << "Average Arrival's/min  # Arrival's/min   Probability\n"

<< "---------------------  ---------------   ------------" << endl;

cout << setiosflags(ios::fixed)

<< setiosflags(ios::showpoint)

<< setprecision(4);

for (x = 1.0; x <= 10.0; x = x + 1.0)  // three control expressions of the loop

{
b=pow(a,x);
c=pow(2.71828, -a);
d=b*c;

fact*=x;

e=d/fact;

cout << setw(21) << a << setw(15) << x << setiosflags(ios::fixed)<< setiosflags(ios::showpoint)
<< setprecision(4)<< setw(12) << e << endl;

}

return 0;

}``````
6
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by mvmalderen

you can find a factorial with

``````for (int x=number to get factorial of; x>0; x--)
{
fact *= x;
}``````

you seem to have some other stuff going on too, :D

We are to write a program that calculates and displays the probability of 0 to 10 planes attempting to land in any minute period at an airport during peak arrival times. We are supposed to use Poisson probability function. Your answer is posted elsewhere with a double loop..

Each plane has its own factorial up to the plane count summed!

what kind of other stuff? I'm very new to this so any help it welcome.

you can find a factorial with

``````for (int x=number to get factorial of; x>0; x--)
{
fact *= x;
}``````

you seem to have some other stuff going on too, :D

what kind of other stuff? I'm very new to this so any help it welcome.

Other stuff like this:

``````b=pow(a,x);
c=pow(2.71828, -a);
d=b*c;

fact*=x;

e=d/fact;``````

You have probabilities, you're using the value of e, etc., other stuff that may USE factorials, but which has nothing to do with computing the factorial itself. You named your thread "Factorial Help", which implies you're having a problem computing a factorial. What exactly are you trying to do and what exactly is the problem? If you're just computing the factorial, see post 2.

``double a,b,c,d,e,f,g,x,sum=0,fact=1;``

You may want to change fact to an integer since the factorial is the product of integers and therefore is an integer itself.

if you have to make a fact. of x

for(i=x;i>=1;i--)
{
fact=fact*i; // fact = 1
}
cout<<fact;

put this logic in your program..
it should be work..

if you have to make a fact. of x

for(i=x;i>=1;i--)
{
fact=fact*i; // fact = 1
}
cout<<fact;

put this logic in your program..
it should be work..

Check your profile, how many times do you want to hear saying us: "Use code tags!!" ??