For school I have to write a program that calculates the probability of 0 to 10 planes attempting to land in any-minute period at an airport during peak arrival times.

I think I don't understand the equation:

a(exponent x) e(exponent -x)
over
x!

x= the number of plane arrivals per minute
a= average number of arrivals per minute
e= Euler's number (2.71828)

EDIT: Assume the average number of arrivals per minute is 3

I know the first number is 0.0498 and the program gets that one right.
The second number should be 0.1494 and the tenth number should be 0.0008 , but my program doesn't get those right.

here is my source code so far, any help would be greatly appreciated.

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
double c, sum=0,a=3, b, x, d, e, f;
double long long fact =1, ;

cout<<"Probability Table\n";
cout<<"*****************\n";
for(x=0; x<=10; x++)//x goes from 1 to 10
{
for (e=1; e<=x; e++ )

sum=sum+x;
fact *= e;
d=pow(3, x);
c=pow(2.71828, -a);
b=d*c;

f=b/fact;

cout<<"\nProbability of "<<x<<" planes arriving is "<<setprecision(3)<<f;





}


system ("Pause");
return 0;

}

Recommended Answers

All 12 Replies

First, indent your code. It is more readable in that way.

Second, how will you find the answer with a pencil and a paper? Answering this question will lead to the structure of your program. Understand the equation first and compute it after!

Don't hesitate to use functions to wrap complicated computations. And be careful with arithmetic operations with different types.

that's the problem I don't understand the equation

So I guess I do understand the equation, my code just isn't figuring out the correct factorial.

Rather the factorial it is figuring out isn't changing.

I will suggest you this:

Write a function that takes an integer and returns its factorial. (Use the power of recursion)

Test it (IMPORTANT!) and after, call it in your program. That will simply your code.

I will suggest you this:

Write a function that takes an integer and returns its factorial. (Use the power of recursion)

Test it (IMPORTANT!) and after, call it in your program. That will simply your code.

That was the very first thing I did, I wrote a seperate program that figures factorials.

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int n, fact = 1, x;
    cout<<"Enter Number\n";
    cin>>x;

for ( n = 1; n <= x; n++ )
{
	 fact *= n;
}
cout<<"The factorial of "<<x<<" is "<<fact<<"\n";

    system ("Pause");
    return 0;
}

Then I integrated it into this other program, I think the problem is that the program is figuring the same factorial for every run. Where it should be starting with the 0! and go all the way to 10!.

What I call a function is something that is not inside a int main(). If you do it with a int factorial() function, you will be able to use it more easily in an another program than a int main().

Putting all the treatment in a function will eliminate the problem of the same factorial for every run.

If you really want to not use function, just set fact to 1 after the computation, but I strongly suggest the use of a factorial() function.

In the code if I change the out to the screen to show the factorials I can see that it is not figuring out the factorial for 0. It should show the result of 1 twice since the factorial of 1 and 0 are 1.

1
2
6
24
120
720
5040
40320
362880
3628800
39916800

Creating a recursive function that returns the factorial will solve this problem.

if 0, then return 1
if 1, then return 1
else do n * fact (n - 1)

This is not a tail-recursive version (sorry for the programmming purists), but it will do for your needs.

A function for factorial will be something like this

int factorial(int n) //you are passing the value n fromyour main
{
   int x=1;
   for(int i=2;i<=n;i++)
   {
      x*=i;
   }
return x;
}

This should calculate the factorial.

Thank you for pointing me in the right direction. This is the finished code if anyone is interested.

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
 
long factorial (long z)
{
  if (z > 1)
   return (z * factorial (z-1));  //creating factorial function
  else
   return (1);
}
 
 
int main()
{
    int x;
    float c,avg, euler=2.71828;
    long double b, d, fact, prob;
 
 
    cout<<"Probability of planes landing in any one minute-\n";
    cout<<"************************************************\n";
 
    cout<<"Enter the average number of arrivals\n";
    cout<<"   (per minute) at your airport: ";
    cin>>avg;
 
     for(x=0; x<=10; x++)//x goes from 1 to 10
     {
 
      d=pow(avg, x);
      c=pow(euler, -avg);
      fact=factorial (x);
      b=d*c;
      prob=b/fact;
	  cout<<"\nProbability of "<<setw(2)<<setiosflags(ios::right)<<x; 
      cout<<" planes arriving is "
      <<setiosflags(ios::fixed)<<setprecision(4)<<prob;
}
 
    cout<<"\n";
    system ("Pause");
    return 0;
 
}
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.