how can i change my output for lambda=2??

Pages: 12
Oct 20, 2010 at 1:28pm
I need to change my output for lambda=2

for (int lambda = 2; lambda <= 3; ++lambda) {
cout << "Lambda = " << lambda << endl;
output<<" Lambda = "<< lambda<<endl;
p=pow(lambda,x)*exp(-lambda)/x;


for (int t = 1; t <= 10; t++) {

double prob=pow(lambda,t)*exp(-lambda)/t;
cout << " The probability of " << t << " customer arriving in any one minute is " << prob << endl;
output<<" The probability of " << t << " customer arriving in one any minute is " << prob << endl;



}
}

return 0;
}
output looks like this:
Lambda = 2 i need to change this one for :"The probability of # plane attempting to land in one any minute is #."
The probability of 1 customer arriving in one any minute is 0.270671
The probability of 2 customer arriving in one any minute is 0.270671
The probability of 3 customer arriving in one any minute is 0.360894
The probability of 4 customer arriving in one any minute is 0.541341
The probability of 5 customer arriving in one any minute is 0.866146
The probability of 6 customer arriving in one any minute is 1.44358
The probability of 7 customer arriving in one any minute is 2.4747
The probability of 8 customer arriving in one any minute is 4.33073
The probability of 9 customer arriving in one any minute is 7.69907
The probability of 10 customer arriving in one any minute is 13.8583
Lambda = 3
The probability of 1 customer arriving in one any minute is 0.149361
The probability of 2 customer arriving in one any minute is 0.224042
The probability of 3 customer arriving in one any minute is 0.448084
The probability of 4 customer arriving in one any minute is 1.00819
The probability of 5 customer arriving in one any minute is 2.41965
The probability of 6 customer arriving in one any minute is 6.04913
The probability of 7 customer arriving in one any minute is 15.5549
The probability of 8 customer arriving in one any minute is 40.8316
The probability of 9 customer arriving in one any minute is 108.884
The probability of 10 customer arriving in one any minute is 293.988

thanks
Oct 20, 2010 at 1:58pm
if
Oct 20, 2010 at 2:01pm
could u explain me how? (writing the program). thanks
Oct 20, 2010 at 2:11pm
There are much neater ways (ternary operator), but you probably want something like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (int lambda = 2; lambda <= 3; ++lambda) {
  cout << "Lambda = " << lambda << endl;
  output<<" Lambda = "<< lambda<<endl;
  p=pow(lambda,x)*exp(-lambda)/x;

  for (int t = 1; t <= 10; t++)
  {
    double prob=pow(lambda,t)*exp(-lambda)/t;
    if ( lambda == 2 )
    {
      cout << " The probability of " << t << " plane attempting to land in any one minute is " << prob << endl;
      output << " The probability of " << t << " plane attempting to land in any one minute is " << prob << endl;
    }
    else
    {
      cout << " The probability of " << t << " customer arriving in any one minute is " << prob << endl;
    output<<" The probability of " << t << " customer arriving in one any minute is " << prob << endl;
    }

  }
}
Last edited on Oct 20, 2010 at 2:12pm
Oct 20, 2010 at 2:21pm
Thanks so much .....
Oct 20, 2010 at 4:53pm
professor said that my results are wrong anybody could help me please and the problem is on
c++ engineers and scientist third edition page 288 problem #2 a and b
thats the link for the book:
http://books.google.com/books?id=5-WDYaUQV1UC&pg=PA288&lpg=PA288&dq=c%2B%2B+for+statement+probability&source=bl&ots=HnvOB-kuvT&sig=8Ryz5gWxORmlGxLcU0EJJf9aNfI&hl=en&ei=X-G-TJe3KcT58Abeuti8Bg&sa=X&oi=book_result&ct=result&resnum=3&sqi=2&ved=0CB4Q6AEwAg#v=onepage&q=c%2B%2B%20for%20statement%20probability&f=false
anybody please ...
Oct 20, 2010 at 5:55pm
How is it wrong?

Link won't work for me.
Oct 20, 2010 at 5:59pm
professor is saying that my results are wrong, i have no idea why.... just copy and paste the link
the link is the question on the book


thats what he said :-Your results are not correct. Probabilities are between Zero and One.

Write each for loop separate. Make all variables as double.
Oct 20, 2010 at 6:21pm
Well, follow his advice.

Write each loop seperately (have two for loops, rather than one nested inside the other - don't control lambda with a for).

If the numbers you're getting are wrong, then your math is wrong somewhere.

Edit to add: as I said, the link won't work for me, and if you want help you should probably show a little more willingness to learn/try/explain what you need.

Random aside: Hometime for me, yay.
Last edited on Oct 20, 2010 at 6:23pm
Oct 20, 2010 at 6:26pm
i being fighting with this program for 3 days now, can`t get it right... and how am i going to write each loop separate??
Last edited on Oct 20, 2010 at 6:28pm
Oct 21, 2010 at 7:23am
Well, as I said, if the numbers you're getting are wrong, it's a problem with the math you're using. Since I can't see the question, and you're obviously unwilling to post it here, then I can't help you with that.

To have two for loops, just have two for loops in your main function. So something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
  int lambda;

  lambda = 2;
  for ( int i = 1; i <= 10; i++ )
  {
    // do the lambda=2 output
  }
  lambda = 3;
  for ( int i = 1; i <= 10; i++ )
  {
    // do the lambda=3 output
  }

  return 0;
}


Obviously, there's better ways to do this, involving creating a function and such, but I think that'd be out of the scope of your question.
Last edited on Oct 21, 2010 at 7:25am
Oct 21, 2010 at 11:52am
(probability) a. the arrival rate of customers in a new york bank can be estimated by using the Poisson probability function : p(x)(lambda^x*e^-lambda)/x!
x # of customer arrivals per minute
lambda average # arrivals per minute
e= exp
now lambda is 3.
p(x=1)=(3^1*exp^-3)/1!=0.149561
and the probability of two customers arriving in any one minute is the following
p(x=2)=(3^2*exp-3)/2!=0.224454
using the Poisson probability function, write c++ program that calculates and displays the probability of 1 to 10 customer arrivals in any one minute when the average arrival is 3 customers per minute.

b. the formula given in exercise a is also applicable for estimating the arrival rate of planes at a busy airport ( this situation an arriving customer is an incoming plane) using the same formula modify the program toi determine the probability of 0 to 10 planes attempting to land at an airport in any one minute period . assume that the average arrival rate for peak arrival times in two planes per minute.

that s the question and i need to put a and b together
Last edited on Oct 21, 2010 at 11:56am
Oct 21, 2010 at 12:01pm
Read this line again:

p(x)=(lambda^x*e^-lambda)/x!


And look at what you're actually doing. Hint: the exclamation mark isn't there for dramatic effect.
Oct 21, 2010 at 12:04pm
thats what i put for the formula
double prob = pow((double)lambda, t) * exp(-lambda) / factorial(t);
Oct 21, 2010 at 12:10pm
Where? In the code posted above, you haven't.

And you probably want to surround pow and exp with brackets, otherwise I think the exp/t bit is being evaluated first.
Last edited on Oct 21, 2010 at 12:10pm
Oct 21, 2010 at 12:44pm
i can`t get the program to run right
Oct 21, 2010 at 12:56pm
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int factorial (int n)
{ factorial=n*1;

}
int main ()
{
int i;
double lambda,prob,t;
ofstream output ("e:resulta10.dat");
cout <<" ... "<<endl;
output << " .... "<<endl;
for (int t=1;t<10;t++){
for (int lambda =2; lambda=3; lambda++){
double prob=(pow)(lambda,t)*(exp)(-lambda)/factorial(t) ;
cout<<".."<<t << prob<<endl;
output<<".."<< t << prob<<endl;
}
}
return 0;
}
Oct 21, 2010 at 1:10pm
What're you trying to do? Why are you putting (pow) and (exp) like that?

Also, your factorial function is wrong, doesn't return a value, etc.

I'm suprised that would even compile, let alone run.
Oct 21, 2010 at 1:13pm
so what can i do to fix this??
Oct 21, 2010 at 1:17pm
Write a proper factorial function that returns the correct value (if you google "c++ factorial" you'll find nothing but people asking how to make a factorial function in C++, with people giving them answers).

Make your math make sense. I'd suggest making a function that takes lambda and x as an argument and returns the probability.
Pages: 12