I'm using poisson's process for probability. This is the equation I'm using : P(x)= ((lamda^x)*e^(-lamba))/x!

it's the factorial part I'm not getting right though. I can get the first 3 probabilities right (0,1,2) but everything after that starts to be off by half and so on. Look I know the code isn't perfect by any means and I'm missing some things, but this is the meat and where the problem lies. I'm new to this so please help me out.

for (x = 0.0; x <= 10.0; x = x + 1.0)  // three control expressions of the loop
        
      
  {
		  for(i=x;i>=1;i--)
  {       
	      fact=fact*i;}

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

		  e=d/fact;

Recommended Answers

All 7 Replies

Missing your fact initialization!

Also in your code, you need the entire function because
a is missing, etc. And why the second loop if it is based upon the outer loop?

for ( int x = 0; x <= 10; x = x + 1) 
{
     int fact = 1;
     for(i=x;i>=1;i--)
     {       
          fact=fact*i;
      }

I've seen this question posted many times! You need a factorial for each step in the run!
!0 = 1 !1 = 1 !2 = 2 !3 = 6, etc.

Since this will contain concepts probably not taught yet, I suggest all of you use it as a basis to fix your programs. This is untested but it should be essentially what you're looking for!

void ProbProc( unsigned int nQty )
{
   double a = (double)nQty;
   double b, c, d, e;
   const unsigned int nRun = 10;
   double fact[ nRun + 1 ];

     // build your factorial table
   fact[0] = 1.0;
   for ( unsigned int x = 1; x <= nRun; x++) 
   {
      fact[x] = fact[x-1] * (double)x;
   }
       // Now crunch the result. Loop for each factorial!
   for ( unsigned int x = 0; x <= nRun; x++) 
   {
      b = pow( a, x );
      c = pow( 2.71828, -a );
      d = b * c;
      e = d / fact[x];

      // Display result here!
   }
}

Of course you don't need the table lookup, but the idea isn't to do your homework for you. And if you do hand in this as your assigment, your professor or their TA will automatically know!

I guess I wasn't clear. he's the whole code. I don't get why you need to change the whole thing. the inner loop should be more then enough. but i don't understand why the factorial loop isn't working. everything works fine. btw thanks for the lecture about using other people's work for my own. I just want some advice because my professor is no help.

#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;
  double i,n;

 

  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 = 0.0; x <= 10.0;x++)  // three control expressions of the loop
        
      
  {
		  for(i=x;i>=1;i--)
  {       fact = 1;
		  
	      fact=fact*i;
			  
			  
		  
		  }

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

		  e=d/fact;
		  
		  cout << setw(21) << a << setw(15) << x << setiosflags(ios::fixed)<< setiosflags(ios::showpoint)
			  << setprecision(4)<< setw(12) << e << endl;
 }
  return 0;

}

You inserted fact = 1 in the wrong place. If you use two loops, it goes between two loops.

for (x = 0.0; x <= 10.0;x++)          
 {
       fact = 1; 
     for(i=x;i>=1;i--)
     {
        fact=fact*i;

Also you're comparing floating point >= 1, where you are expecting the 1.0 match. NEVER compare floating points values for an exact match. They are exponent based and not exact values unlike integers. Use integers for your loop counting and cast to float or double when used in an equation!

1 != 1.0000000001

X++

What is 3.0 ++ ?
++ is an integer increment. has nothing to do with floating-point. Again, Change your math to integer's except for the calculation work! Same for --

If you insist on using floating-point then change ++ ro += 1.0 and the -- to -= 1.0;
You'll still have an accuracy issue but it just might fix your problem!

ok I'll give it a shot. thanks for your help. sorry about jumping on your case earlier. I'm into this programming thing 3 weeks now and concepts aren't quite there. plus the book doesn't really help. I'm sure this is a practice thing, but i wish there was a inclusive list of how to for math functions. thanks again.

You inserted fact = 1 in the wrong place. If you use two loops, it goes between two loops.

for (x = 0.0; x <= 10.0;x++)          
 {
       fact = 1; 
     for(i=x;i>=1;i--)
     {
        fact=fact*i;

Also you're comparing floating point >= 1, where you are expecting the 1.0 match. NEVER compare floating points values for an exact match. They are exponent based and not exact values unlike integers. Use integers for your loop counting and cast to float or double when used in an equation!

1 != 1.0000000001

X++

What is 3.0 ++ ?
++ is an integer increment. has nothing to do with floating-point. Again, Change your math to integer's except for the calculation work! Same for --

If you insist on using floating-point then change ++ ro += 1.0 and the -- to -= 1.0;
You'll still have an accuracy issue but it just might fix your problem!

Not a problem. I have a bit more programming time then that and it is a practice thing. Learn new concepts, practice them, and then hang them on your tool belt.

Just like mathematics. Everything you learn now, is based upon what you learned yesterday!

Let us know if fixing the x++ , i-- thing, or going to integer loops fixes your problem.

The cut'n'paste dialogue wasn't meant for you. Lots of people read these posts whom have similar problems looking for quick solutions. It was meant for them.

THANK YOU GOD!
that was tough...i couldn't even sleep last night because of this code. thanks for the help in finding my errors. i knew I was close but due to inexperience and ignorance, i got a little wrapped around the axel. good problem though, I had to think a lot about it. i just wish my text book was a little better.

You inserted fact = 1 in the wrong place. If you use two loops, it goes between two loops.

for (x = 0.0; x <= 10.0;x++)          
 {
       fact = 1; 
     for(i=x;i>=1;i--)
     {
        fact=fact*i;

Also you're comparing floating point >= 1, where you are expecting the 1.0 match. NEVER compare floating points values for an exact match. They are exponent based and not exact values unlike integers. Use integers for your loop counting and cast to float or double when used in an equation!

1 != 1.0000000001

X++

What is 3.0 ++ ?
++ is an integer increment. has nothing to do with floating-point. Again, Change your math to integer's except for the calculation work! Same for --

If you insist on using floating-point then change ++ ro += 1.0 and the -- to -= 1.0;
You'll still have an accuracy issue but it just might fix your problem!

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.