-4

Before anything, here's my code:

#include <stdio.h>
#include <conio.h>
#include <math.h>

main()
{
    float Exp, Act, Px, Facto_Act;
    printf("\n This is Poisson Distribution Calculator!");
    printf("\n Enter the expected value of success in a time period:");
    scanf("%f",& Exp);
    printf("\n Enter the actual or calculated value of success in a time period:");
    scanf("%f",& Act);
    Px=pow(M_E,-Exp)*pow(Exp,Act)/Facto_Act;
    printf("\n Poisson probability is:%f", Px);
    getch();
    return 0;
}

Facto_Act(float Act)
{
    float c;
    float result=1;
    for(c=1;c<=Act;c++)
        result=result*c;
    return result;
}

Further explanation:

Poisson equation looks like this:

P(x)= (e^-Lambda)(Lambda^x)/(x!)

Exp: Expected number of events in a given time(Lambda) Act: Actual number of events in a given time(x) Px: Probability of an event occuring in a given time( P(x) ) Facto_Act: Factorial of Actual number of events in a given time(x!)

When I figured out how to do factorials for integer in C, I will try to add factorials for positive decimals too. But #1.INF00 is not a value I expect.

When I compile the code, there are no more coding errors shown. But when I enter the expected value of successes in a period, then the actual value of succesess in a period, I always end up with #1.INF00. I am very noobful of C, and while this site has helped me improved my programs by a bit, I can't understand the '#1.INF00' means.

I decided not to make Facto_Act a function

I decided to circumvent the entire Facto_Act function problem by not making Facto_Act a function, then trying to call it. It seems that factorials can be performed without making a new function for it. Thus Facto_Act is now a variable. This is my new code:

#include <stdio.h>
#include <conio.h>
#include <math.h>


main()
{
    double Exp, Px;
    int c, Act, Act2, Facto_Act=1;
    printf("\n This is Poisson Distribution Calculator!");
    printf("\n Enter the expected value of success in a time period:");
    scanf("%lf",& Exp);
    printf("\n Enter the actual or calculated value of success 
    \n in a time period(must be an integer!):");
    scanf("%d",& Act);
    /*My factorial starts here*/
    for (c=1;c<=Act;c++)
        Facto_Act=Facto_Act*c;
    /*My factorial ends here*/
    Px=(pow(M_E,-Exp))*(pow(Exp,Act))/Facto_Act;
    printf("\n Poisson probability is:%lf", Px);
    getch();
    return 0;
}

I thank you all for helping me out.

  • 1
    `Px=pow(M_E,-Exp)*pow(Exp,Act)/Facto_Act;`...please don't tell me this compiles fine. – Sourav Ghosh Dec 17 '15 at 10:31
  • 1
    "When I figured out how to do factorials for integer in C, I will try to add factorials for positive decimals" Factorials for decimals? And how are they defined mathematically? I think you are misinterpreting Poisson events. They must be an integer number. You can for example try to predict the number of car crashes on a given road in a given time frame. You can have 0, 1, 2, or more; you can't have 0.7 or 2.345 crashes. They must be integers. The average can be decimal, but the number of events must be integer. – Fabio says Reinstate Monica Dec 17 '15 at 10:36
  • Sourav Ghosh, Actually it did not. I had errors when compiling that says Facto_Act somehow can't be divided. If I recall correctly, float/double(double). But rearranging Facto_Act to the bottom somehow removes the error being declared. – Aakmal Yusop Dec 17 '15 at 10:43
  • Fabio Turati, well I think I'm wrong. I'm not very good at statistics. But some questions I do did give 2.4 malfunctions in an hour, or 4.2 phone calls a day, but yeah, such events are supposed to be discrete. – Aakmal Yusop Dec 17 '15 at 10:44
  • Anyway `M_E` is not declared and doesn't compile. And even if you declared it, it would be empty. What is its value? And what is `Facto_Act`, is it a variable or a function? I think you want a function. But you are declaring it, and using it, as a variable. – Fabio says Reinstate Monica Dec 17 '15 at 10:45
  • I found on the net that M_E is supposed to be the Euler's number under the library of . I thought it's already in the library. I try to create Facto_Act as a factorial of Act. Thus I want to be a function but can still be used in my 'Poisson' equation. – Aakmal Yusop Dec 17 '15 at 10:49
  • 1
    The webpage I refer: http://www.lemoda.net/c/maths-constants/ – Aakmal Yusop Dec 17 '15 at 10:51
  • Oh, I see. I have searched this and I've discovered that [M_E is not defined on all platforms](http://stackoverflow.com/a/6984493/3982001). I have just tried the online compiler Ideone and it doesn't work there, but I guess it works on your machine, so it's fine. Sorry. – Fabio says Reinstate Monica Dec 17 '15 at 11:18

1 Answers1

1

You declared a variable named FactoAct of type float. Since it is an extern variable with no initialisation, it has a value of 0.

Later you define a function Facto_Act(float Act) with an implicit return type of "int".

Your division xxx / FactoAct divides xxx by the variable FactoAct, which is zero. That's where your INF result comes from.

When you had the function at the top, when the compiler saw xxx / FactoAct, FactoAct was not the result of a call to the function, it was the function itself. You can't divide a number by a function. It doesn't make sense. The only thing you can do with a function is take its address, or call it.

You probably want FactoAct (x) or something like that.

PS. Don't use float instead of double, unless you have a reason that you can put into clear words why in your specific case float is better than double.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I decided to escape the function problem. But if I want to call a function as a numerator next time, how should I do it? – Aakmal Yusop Dec 18 '15 at 01:29