Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My code is computing crazy numbers!

+7 votes
asked Dec 30, 2023 by Colin Ferguson (190 points)

In the snippet below I put a breakpoint at the first line. The  local variables generated at start follow. Note that iGenerationMax is huge even though it is set to 99. I need help figuring out why it changed. Any help proffered will be much appreciated. :)

======== snippet=====================   

     int iGenerationMax = 99;
    double fProbability = 0.500;

    while (!Found && (i < iGenerationMax)){
        double t = i;
//        if (k <= n && k > 0)
     {  cur = PosteriorDensity(t, mu, k, n);
        CumProb = CumProb + (prev + cur) / 2.0; }
    if ((CumProb > fProbability) && !Found)
    {   if (abs(PrevCumProb - fProbability) < abs(CumProb - fProbability)) {
            iGeneration = i - 1;}
        else {
            iGeneration = i;
            Found = true; } }
    prev = cur;
    PrevCumProb = CumProb;
    Found = true;
    i = i + 1; }
return iGeneration; }

=======Local Variables=============

VariableValue
prev0
cur0
CumProb0
PrevCumProb0
Foundfalse
i1
iGeneration1305670058
iGenerationMax1071124578
fProbability0

1 Answer

0 votes
answered Jan 6 by Peter Minarik (86,240 points)

Your variable i is probably not initialized so it contains some random value.

Please, share the entire code for verification, not just a snippet.

commented Jan 6 by xDELLx (10,500 points)
As Peter suggested, sharing partial information is not going to help you.
If the break point was set @ "    int iGenerationMax = 99; "
I assume the function stack(& all other locals) are not yet initialized & are bound to have Garbage Values in them.You should check thier values after assignment.
commented Jan 8 by Colin Ferguson (190 points)
Hey guys, thank you for the suggestions. I did not post all my code because it seemed like too much of an ask.
commented Jan 9 by Peter Minarik (86,240 points)
Sometimes it's better to post your entire code, even if it seems a lot (you can provide link to your project with the SHARE button, 3 buttons right of the RUN button), as some trickier questions may need all the information possible to solve.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...