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;

}

Dani AI

Generated

The goal in this thread (as started by ) is the Poisson probability for 0…10 arrivals given a lambda. Posts from and show the usual factorial loop and correctly points out factorials are integer-valued; calls out that this is a Poisson problem. Common mistakes that cause “only the first couple of values” to look right are: starting the loop at 1 instead of 0 (so P(0) is never printed), using a hand-typed e value like 2.71828 instead of exp(), and mishandling/resetting the factorial when trying nested loops — or hitting integer overflow on factorials.

A robust, simple alternative that avoids explicit factorials and pow calls is to compute P(0) = exp(-lambda) and then update by the recurrence P(k+1) = P(k) * lambda / (k+1). It is faster, numerically stable, and easy to loop for k = 0..10:

#include <iostream>
#include <cmath>
#include <iomanip>

int main() {
    double lam;
    std::cin >> lam;
    double p = std::exp(-lam); // P(0)
    std::cout << "Arrivals  Probability\n";
    for (int k = 0; k <= 10; ++k) {
        std::cout << std::setw(8) << k << std::setw(13)
                  << std::fixed << std::setprecision(4) << p << "\n";
        p *= lam / (k + 1); // move to P(k+1)
    }
}

Notes and troubleshooting: prefer an integer loop index (int k) rather than a double loop variable to avoid floating-point step issues; call std::exp(-lam) from <cmath> instead of pow(2.71828,-lam); if an explicit factorial is still used, initialize the factorial accumulator inside the outer loop and use an unsigned long long (20! fits in 64-bit; 21! overflows). Avoid repeatedly setting stream flags inside the loop — set formatting once before printing. These changes address the main gaps in earlier replies and produce reliable Poisson probabilities.

Recommended Answers

All 6 Replies

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

Found your problem on-line.

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!!" ??

Information about code-tags is spread all over this website :
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled "Read Me: Read This Before Posting"
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.